在用pdb debug的时候,有时候需要看一下特定layer的权重以及相应的梯度信息,如何查看呢?
1. 首先把你的模型打印出来,像这样
2. 然后观察到model下面有module的key,module下面有features的key, features下面有(0)的key,这样就可以直接打印出weight了,在pdb debug界面输入p model.module.features[0].weight,就可以看到weight,输入 p model.module.features[0].weight.grad就可以查看梯度信息
补充知识:查看Pytorch网络的各层输出(feature map)、权重(weight)、偏置(bias)
BatchNorm2d参数量
torch.nn.BatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) # 卷积层中卷积核的数量C num_features – C from an expected input of size (N, C, H, W)
> import torch > m = torch.nn.BatchNorm2d(100) > m.weight.shape torch.Size([100]) > m.numel() AttributeError: 'BatchNorm2d' object has no attribute 'numel' > m.weight.numel() 100 > m.parameters().numel() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'generator' object has no attribute 'numel' > [p.numel() for p in m.parameters()] [100, 100]
linear层
> import torch > m1 = torch.nn.Linear(100,10) # 参数数量= (输入神经元+1)*输出神经元 > m1.weight.shape torch.Size([10, 100]) > m1.bias.shape torch.Size([10]) > m1.bias.numel() 10 > m1.weight.numel() 1000 > m11 = list(m1.parameters()) > m11[0].shape # weight torch.Size([10, 100]) > m11[1].shape # bias torch.Size([10])
weight and bias
# Method 1 查看Parameters的方式多样化,直接访问即可 model = alexnet(pretrained=True).to(device) conv1_weight = model.features[0].weight# Method 2 # 这种方式还适合你想自己参考一个预训练模型写一个网络,各层的参数不变,但网络结构上表述有所不同 # 这样你就可以把param迭代出来,赋给你的网络对应层,避免直接load不能匹配的问题! for layer,param in model.state_dict().items(): # param is weight or bias(Tensor) print layer,param
feature map
由于pytorch是动态网络,不存储计算数据,查看各层输出的特征图并不是很方便!分下面两种情况讨论:
1、你想查看的层是独立的,那么你在forward时用变量接收并返回即可!!
class Net(nn.Module): def __init__(self): self.conv1 = nn.Conv2d(1, 1, 3) self.conv2 = nn.Conv2d(1, 1, 3) self.conv3 = nn.Conv2d(1, 1, 3) def forward(self, x): out1 = F.relu(self.conv1(x)) out2 = F.relu(self.conv2(out1)) out3 = F.relu(self.conv3(out2)) return out1, out2, out3
2、你的想看的层在nn.Sequential()顺序容器中,这个麻烦些,主要有以下几种思路:
# Method 1 巧用nn.Module.children() # 在模型实例化之后,利用nn.Module.children()删除你查看的那层的后面层 import torch import torch.nn as nn from torchvision import modelsmodel = models.alexnet(pretrained=True)# remove last fully-connected layer new_classifier = nn.Sequential(*list(model.classifier.children())[:-1]) model.classifier = new_classifier # Third convolutional layer new_features = nn.Sequential(*list(model.features.children())[:5]) model.features = new_features
# Method 2 巧用hook,推荐使用这种方式,不用改变原有模型 # torch.nn.Module.register_forward_hook(hook) # hook(module, input, output) -> Nonemodel = models.alexnet(pretrained=True) # 定义 def hook (module,input,output): print output.size() # 注册 handle = model.features[0].register_forward_hook(hook) # 删除句柄 handle.remove()# torch.nn.Module.register_backward_hook(hook) # hook(module, grad_input, grad_output) -> Tensor or None model = alexnet(pretrained=True).to(device) outputs = [] def hook (module,input,output): outputs.append(output) print len(outputs)handle = model.features[0].register_backward_hook(hook)
注:还可以通过定义一个提取特征的类,甚至是重构成各层独立相同模型将问题转化成第一种
计算模型参数数量
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
以上这篇pytorch查看模型weight与grad方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]