Pytorch – torch.nn.Module的parameters()和named_parameters()
1 torch.nn.Module的named_parameters()和parameters()
Pytorch的torch.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
==========
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Pytorch – torch.nn.Module的parameters()和named_parameters()
原文链接:https://www.stubbornhuang.com/2343/
发布于:2022年09月06日 17:28:19
修改于:2023年06月21日 18:07:42
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论
51