QCFS-related work

一、代码

def replace_maxpool2d_by_avgpool2d(model): # 将模型中的所有MaxPool2d层替换为AvgPool2d层。
    for name, module in model._modules.items(): # 函数使用递归方式遍历模型的所有模块,通过model._modules.items()获取模型的子模块以及它们对应的名称。
        if hasattr(module, "_modules"): # 对于每个子模块,首先检查是否有进一步的子模块,如果有,则递归调用replace_maxpool2d_by_avgpool2d函数对其进行替换。
            model._modules[name] = replace_maxpool2d_by_avgpool2d(module)
        if module.__class__.__name__ == 'MaxPool2d': # 然后,检查当前模块的类名是否为MaxPool2d,如果是,则将该模块替换为一个具有相同参数的AvgPool2d层。
            model._modules[name] = nn.AvgPool2d(kernel_size=module.kernel_size,
                                                stride=module.stride,
                                                padding=module.padding)
    return model

_modules 是一个特殊的属性,用于访问模型对象中的子模块。
model._modules 是一个字典,其中键是子模块的名称,值是对应的子模块对象。items() 是字典的方法之一,它返回一个由键-值对组成的元组列表,表示字典中的所有项。每个元组的第一个元素是子模块的名称,第二个元素是对应的子模块对象。

比如:
在这里插入图片描述
换完之后:
在这里插入图片描述

modelVGG16有5个Sequential模块和1个Classifer模块。(Sequential模块本身不是一层,而是一种容器,用于将多个层按顺序组合成一个模块)

  • 打印module.__class__如下:
    <class ‘torch.nn.modules.container.Sequential’> × 6
  • 打印module.__class__.__name__如下:
    Sequential × 6

sequential容器内部还有很多的如Conv2d等子模块,即对于<class ‘torch.nn.modules.container.Sequential’>来说hasattr(module, "_modules")成立,还有子模块所以要继续递归到每个最小子模块,才能判断

二、

torch.ones_like(x) 是一个函数,它返回一个与输入张量 x 具有相同形状的张量,且所有元素的值都设置为1。

三、

spikes_number = {}

def save_spikes_number(module, inputdata, output):
    global spikes_number
    if not module in spikes_number:
        spikes_number[module] = {}
        #字典套字典
       {
    		"module1": {
        		"1": 10,
        		"2": 100
    		},
    		......		
       }
        spikes_number[module]["1"] = torch.sum(torch.abs(output))
        spikes_number[module]["2"] = output.numel()
    else:
        spikes_number[module]["1"] = spikes_number[module]["1"]+torch.sum(torch.abs(output))
        
def add_IF_hook(model):
    children = list(model.named_children())
    for name, child in children:
        is_need = False
        if isinstance(child, ScaledNeuron):
            model._modules[name].register_forward_hook(save_spikes_number)
            is_need = True
        if not is_need:
            add_IF_hook(child)   

五、

can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

六、

cifar五万个训练集,一万个测试集(79*128 = 10112(最后一个bs不满))

七、并行训练

参考文章1
参考文章2
代码+教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值