天天看點

pytorch中的model.named_parameters()與model.parameters()

參考連結:https://www.cnblogs.com/yqpy/p/12585331.html

model.named_parameters()

疊代列印model.named_parameters()将會列印每一次疊代元素的名字和param。

model = DarkNet([1, 2, 8, 8, 4])
for name, param in model.named_parameters():
    print(name,param.requires_grad)
    param.requires_grad = False
           

輸出結果為

conv1.weight True
bn1.weight True
bn1.bias True
layer1.ds_conv.weight True
layer1.ds_bn.weight True
layer1.ds_bn.bias True
layer1.residual_0.conv1.weight True
layer1.residual_0.bn1.weight True
layer1.residual_0.bn1.bias True
layer1.residual_0.conv2.weight True
layer1.residual_0.bn2.weight True
layer1.residual_0.bn2.bias True
layer2.ds_conv.weight True
layer2.ds_bn.weight True
layer2.ds_bn.bias True
layer2.residual_0.conv1.weight True
layer2.residual_0.bn1.weight True
layer2.residual_0.bn1.bias True
....
           

并且可以更改參數的可訓練屬性,第一次列印是True,這是第二次,就是False了

model.parameters()

疊代列印model.parameters()将會列印每一次疊代元素的param而不會列印名字,這是它和named_parameters的差別,兩者都可以用來改變requires_grad的屬性。

for index, param in enumerate(model.parameters()):
    print(param.shape)
           

輸出結果為

torch.Size([32, 3, 3, 3])
torch.Size([32])
torch.Size([32])
torch.Size([64, 32, 3, 3])
torch.Size([64])
torch.Size([64])
torch.Size([32, 64, 1, 1])
torch.Size([32])
torch.Size([32])
torch.Size([64, 32, 3, 3])
torch.Size([64])
torch.Size([64])
torch.Size([128, 64, 3, 3])
torch.Size([128])
torch.Size([128])
torch.Size([64, 128, 1, 1])
torch.Size([64])
torch.Size([64])
torch.Size([128, 64, 3, 3])
torch.Size([128])
torch.Size([128])
torch.Size([64, 128, 1, 1])
torch.Size([64])
torch.Size([64])
torch.Size([128, 64, 3, 3])
torch.Size([128])
torch.Size([128])
torch.Size([256, 128, 3, 3])
torch.Size([256])
torch.Size([256])
torch.Size([128, 256, 1, 1])
....
           

将兩者結合進行疊代,同時具有索引,網絡層名字及param

for index, (name, param) in zip(enumerate(model.parameters()), model.named_parameters()):
		print(index[0])
		print(name, param.shape)
           

輸出結果為

0
conv1.weight torch.Size([32, 3, 3, 3])
1
bn1.weight torch.Size([32])
2
bn1.bias torch.Size([32])
3
layer1.ds_conv.weight torch.Size([64, 32, 3, 3])
4
layer1.ds_bn.weight torch.Size([64])
5
layer1.ds_bn.bias torch.Size([64])
6
layer1.residual_0.conv1.weight torch.Size([32, 64, 1, 1])
7
layer1.residual_0.bn1.weight torch.Size([32])
8
layer1.residual_0.bn1.bias torch.Size([32])
9
layer1.residual_0.conv2.weight torch.Size([64, 32, 3, 3])
           

繼續閱讀