1 torch.nn.Modulenamed_parameters()parameters()

Pytorchtorch.nn.Module有两个比较相似的属性函数named_parameters()parameters()parameters()返回整个模型参数的迭代器,而named_parameters()可以返回参数层名称以及参数的迭代器,我们同时使用这两个函数查看模型参数以及修改模型属性,下面通过Pytorch预置的Resnet18来比较一下两者的区别。

1.1 parameters()

# -*- coding: utf-8 -*-
import torchvision.models

if __name__ == '__main__':
    resnet18 = torchvision.models.resnet18(pretrained=True)
    for params in resnet18.parameters():
        #print('params:', params)
        print('grad_requirs:', params.requires_grad)
        print('grad_value:', params.grad)
        print("==========")

输出

grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========
grad_requirs: True
grad_value: None
==========

1.2 named_parameters()

# -*- coding: utf-8 -*-
import torchvision.models

if __name__ == '__main__':
    resnet18 = torchvision.models.resnet18(pretrained=True)
    for name, params in resnet18.named_parameters():
        print('name:', name)
        #print('params:', params)
        print('grad_requirs:', params.requires_grad)
        print('grad_value:', params.grad)
        print("==========")

输出

name: conv1.weight
grad_requirs: True
grad_value: None
==========
name: bn1.weight
grad_requirs: True
grad_value: None
==========
name: bn1.bias
grad_requirs: True
grad_value: None
==========
name: layer1.0.conv1.weight
grad_requirs: True
grad_value: None
==========
name: layer1.0.bn1.weight
grad_requirs: True
grad_value: None
==========
name: layer1.0.bn1.bias
grad_requirs: True
grad_value: None
==========
name: layer1.0.conv2.weight
grad_requirs: True
grad_value: None
==========
name: layer1.0.bn2.weight
grad_requirs: True
grad_value: None
==========
name: layer1.0.bn2.bias
grad_requirs: True
grad_value: None
==========
name: layer1.1.conv1.weight
grad_requirs: True
grad_value: None
==========
name: layer1.1.bn1.weight
grad_requirs: True
grad_value: None
==========
name: layer1.1.bn1.bias
grad_requirs: True
grad_value: None
==========
name: layer1.1.conv2.weight
grad_requirs: True
grad_value: None
==========
name: layer1.1.bn2.weight
grad_requirs: True
grad_value: None
==========
name: layer1.1.bn2.bias
grad_requirs: True
grad_value: None
==========
name: layer2.0.conv1.weight
grad_requirs: True
grad_value: None
==========
name: layer2.0.bn1.weight
grad_requirs: True
grad_value: None
==========
name: layer2.0.bn1.bias
grad_requirs: True
grad_value: None
==========
name: layer2.0.conv2.weight
grad_requirs: True
grad_value: None
==========
name: layer2.0.bn2.weight
grad_requirs: True
grad_value: None
==========
name: layer2.0.bn2.bias
grad_requirs: True
grad_value: None
==========
name: layer2.0.downsample.0.weight
grad_requirs: True
grad_value: None
==========
name: layer2.0.downsample.1.weight
grad_requirs: True
grad_value: None
==========
name: layer2.0.downsample.1.bias
grad_requirs: True
grad_value: None
==========
name: layer2.1.conv1.weight
grad_requirs: True
grad_value: None
==========
name: layer2.1.bn1.weight
grad_requirs: True
grad_value: None
==========
name: layer2.1.bn1.bias
grad_requirs: True
grad_value: None
==========
name: layer2.1.conv2.weight
grad_requirs: True
grad_value: None
==========
name: layer2.1.bn2.weight
grad_requirs: True
grad_value: None
==========
name: layer2.1.bn2.bias
grad_requirs: True
grad_value: None
==========
name: layer3.0.conv1.weight
grad_requirs: True
grad_value: None
==========
name: layer3.0.bn1.weight
grad_requirs: True
grad_value: None
==========
name: layer3.0.bn1.bias
grad_requirs: True
grad_value: None
==========
name: layer3.0.conv2.weight
grad_requirs: True
grad_value: None
==========
name: layer3.0.bn2.weight
grad_requirs: True
grad_value: None
==========
name: layer3.0.bn2.bias
grad_requirs: True
grad_value: None
==========
name: layer3.0.downsample.0.weight
grad_requirs: True
grad_value: None
==========
name: layer3.0.downsample.1.weight
grad_requirs: True
grad_value: None
==========
name: layer3.0.downsample.1.bias
grad_requirs: True
grad_value: None
==========
name: layer3.1.conv1.weight
grad_requirs: True
grad_value: None
==========
name: layer3.1.bn1.weight
grad_requirs: True
grad_value: None
==========
name: layer3.1.bn1.bias
grad_requirs: True
grad_value: None
==========
name: layer3.1.conv2.weight
grad_requirs: True
grad_value: None
==========
name: layer3.1.bn2.weight
grad_requirs: True
grad_value: None
==========
name: layer3.1.bn2.bias
grad_requirs: True
grad_value: None
==========
name: layer4.0.conv1.weight
grad_requirs: True
grad_value: None
==========
name: layer4.0.bn1.weight
grad_requirs: True
grad_value: None
==========
name: layer4.0.bn1.bias
grad_requirs: True
grad_value: None
==========
name: layer4.0.conv2.weight
grad_requirs: True
grad_value: None
==========
name: layer4.0.bn2.weight
grad_requirs: True
grad_value: None
==========
name: layer4.0.bn2.bias
grad_requirs: True
grad_value: None
==========
name: layer4.0.downsample.0.weight
grad_requirs: True
grad_value: None
==========
name: layer4.0.downsample.1.weight
grad_requirs: True
grad_value: None
==========
name: layer4.0.downsample.1.bias
grad_requirs: True
grad_value: None
==========
name: layer4.1.conv1.weight
grad_requirs: True
grad_value: None
==========
name: layer4.1.bn1.weight
grad_requirs: True
grad_value: None
==========
name: layer4.1.bn1.bias
grad_requirs: True
grad_value: None
==========
name: layer4.1.conv2.weight
grad_requirs: True
grad_value: None
==========
name: layer4.1.bn2.weight
grad_requirs: True
grad_value: None
==========
name: layer4.1.bn2.bias
grad_requirs: True
grad_value: None
==========
name: fc.weight
grad_requirs: True
grad_value: None
==========
name: fc.bias
grad_requirs: True
grad_value: None
==========