欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch查看模型weight與grad方式

 更新時(shí)間:2020年06月24日 08:56:26   作者:YongjieShi  
這篇文章主要介紹了pytorch查看模型weight與grad方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

在用pdb debug的時(shí)候,有時(shí)候需要看一下特定layer的權(quán)重以及相應(yīng)的梯度信息,如何查看呢?

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就可以查看梯度信息

補(bǔ)充知識(shí):查看Pytorch網(wǎng)絡(luò)的各層輸出(feature map)、權(quán)重(weight)、偏置(bias)

BatchNorm2d參數(shù)量

torch.nn.BatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
# 卷積層中卷積核的數(shù)量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)
# 參數(shù)數(shù)量= (輸入神經(jīng)元+1)*輸出神經(jīng)元
>>> 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 
# 這種方式還適合你想自己參考一個(gè)預(yù)訓(xùn)練模型寫一個(gè)網(wǎng)絡(luò),各層的參數(shù)不變,但網(wǎng)絡(luò)結(jié)構(gòu)上表述有所不同
# 這樣你就可以把param迭代出來,賦給你的網(wǎng)絡(luò)對(duì)應(yīng)層,避免直接load不能匹配的問題!
for layer,param in model.state_dict().items(): # param is weight or bias(Tensor) 
 print layer,param

feature map

由于pytorch是動(dòng)態(tài)網(wǎng)絡(luò),不存儲(chǔ)計(jì)算數(shù)據(jù),查看各層輸出的特征圖并不是很方便!分下面兩種情況討論:

1、你想查看的層是獨(dú)立的,那么你在forward時(shí)用變量接收并返回即可?。?/p>

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()順序容器中,這個(gè)麻煩些,主要有以下幾種思路:

# Method 1 巧用nn.Module.children()
# 在模型實(shí)例化之后,利用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,推薦使用這種方式,不用改變?cè)心P?
# torch.nn.Module.register_forward_hook(hook)
# hook(module, input, output) -> Nonemodel = models.alexnet(pretrained=True)
# 定義
def hook (module,input,output):
  print output.size()
# 注冊(cè)
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)

注:還可以通過定義一個(gè)提取特征的類,甚至是重構(gòu)成各層獨(dú)立相同模型將問題轉(zhuǎn)化成第一種

計(jì)算模型參數(shù)數(shù)量

def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)

以上這篇pytorch查看模型weight與grad方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論