Pytorch的named_children, named_modules和named_children

PyTorch 中,named_childrennamed_modulesnamed_parameters 是用于获取神经网络模型组件和参数的三种不同的方法。下面是它们各自的作用和区别:

  • named_parameters:递归地列出所有参数名称和tensor
  • named_modules:递归地列出所有子层,其中第一个返回值就是模型本身
  • named_children:列出模型的第一层级的子层,不往下进行深入递归

1. named_children:

  • named_children 返回一个生成器,它包含模型中所有直接子模块的名称和模块对。
  • 它只返回一层级的子模块,不递归到更深层次的子模块。
  • 这个方法通常用于迭代模型的直接子模块,并对其进行操作或检查。

示例:

from torchvision.models import resnet18

model = resnet18()
# Print each layer name and its module
# Note that named_children method only returns the first level submodules
for name, layer in model.named_children():
    print(name.ljust(10), '-->', type(layer))

输出:

conv1      --> <class 'torch.nn.modules.conv.Conv2d'>
bn1        --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
relu       --> <class 'torch.nn.modules.activation.ReLU'>
maxpool    --> <class 'torch.nn.modules.pooling.MaxPool2d'>
layer1     --> <class 'torch.nn.modules.container.Sequential'>
layer2     --> <class 'torch.nn.modules.container.Sequential'>
layer3     --> <class 'torch.nn.modules.container.Sequential'>
layer4     --> <class 'torch.nn.modules.container.Sequential'>
avgpool    --> <class 'torch.nn.modules.pooling.AdaptiveAvgPool2d'>
fc         --> <class 'torch.nn.modules.linear.Linear'>

2. named_modules:

  • named_modules 返回一个生成器,它包含模型中所有模块的名称和模块对,包括子模块的子模块。
  • 它递归地遍历整个模型,返回所有模块的名称和引用。
  • 这个方法适用于当你需要对模型中的所有模块进行操作或检查时,无论它们位于哪一层级。

示例:

from torchvision.models import resnet18

model = resnet18()
# The first layer is the model itself
for name, layer in model.named_modules():
    print(name.ljust(15), '-->', type(layer))

输出:

                --> <class 'torchvision.models.resnet.ResNet'>
conv1           --> <class 'torch.nn.modules.conv.Conv2d'>
bn1             --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
relu            --> <class 'torch.nn.modules.activation.ReLU'>
maxpool         --> <class 'torch.nn.modules.pooling.MaxPool2d'>
layer1          --> <class 'torch.nn.modules.container.Sequential'>
layer1.0        --> <class 'torchvision.models.resnet.BasicBlock'>
layer1.0.conv1  --> <class 'torch.nn.modules.conv.Conv2d'>
layer1.0.bn1    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer1.0.relu   --> <class 'torch.nn.modules.activation.ReLU'>
layer1.0.conv2  --> <class 'torch.nn.modules.conv.Conv2d'>
layer1.0.bn2    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer1.1        --> <class 'torchvision.models.resnet.BasicBlock'>
layer1.1.conv1  --> <class 'torch.nn.modules.conv.Conv2d'>
layer1.1.bn1    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer1.1.relu   --> <class 'torch.nn.modules.activation.ReLU'>
layer1.1.conv2  --> <class 'torch.nn.modules.conv.Conv2d'>
layer1.1.bn2    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer2          --> <class 'torch.nn.modules.container.Sequential'>
layer2.0        --> <class 'torchvision.models.resnet.BasicBlock'>
layer2.0.conv1  --> <class 'torch.nn.modules.conv.Conv2d'>
layer2.0.bn1    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer2.0.relu   --> <class 'torch.nn.modules.activation.ReLU'>
layer2.0.conv2  --> <class 'torch.nn.modules.conv.Conv2d'>
layer2.0.bn2    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer2.0.downsample --> <class 'torch.nn.modules.container.Sequential'>
layer2.0.downsample.0 --> <class 'torch.nn.modules.conv.Conv2d'>
layer2.0.downsample.1 --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer2.1        --> <class 'torchvision.models.resnet.BasicBlock'>
layer2.1.conv1  --> <class 'torch.nn.modules.conv.Conv2d'>
layer2.1.bn1    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer2.1.relu   --> <class 'torch.nn.modules.activation.ReLU'>
layer2.1.conv2  --> <class 'torch.nn.modules.conv.Conv2d'>
layer2.1.bn2    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer3          --> <class 'torch.nn.modules.container.Sequential'>
layer3.0        --> <class 'torchvision.models.resnet.BasicBlock'>
layer3.0.conv1  --> <class 'torch.nn.modules.conv.Conv2d'>
layer3.0.bn1    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer3.0.relu   --> <class 'torch.nn.modules.activation.ReLU'>
layer3.0.conv2  --> <class 'torch.nn.modules.conv.Conv2d'>
layer3.0.bn2    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer3.0.downsample --> <class 'torch.nn.modules.container.Sequential'>
layer3.0.downsample.0 --> <class 'torch.nn.modules.conv.Conv2d'>
layer3.0.downsample.1 --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer3.1        --> <class 'torchvision.models.resnet.BasicBlock'>
layer3.1.conv1  --> <class 'torch.nn.modules.conv.Conv2d'>
layer3.1.bn1    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer3.1.relu   --> <class 'torch.nn.modules.activation.ReLU'>
layer3.1.conv2  --> <class 'torch.nn.modules.conv.Conv2d'>
layer3.1.bn2    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer4          --> <class 'torch.nn.modules.container.Sequential'>
layer4.0        --> <class 'torchvision.models.resnet.BasicBlock'>
layer4.0.conv1  --> <class 'torch.nn.modules.conv.Conv2d'>
layer4.0.bn1    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer4.0.relu   --> <class 'torch.nn.modules.activation.ReLU'>
layer4.0.conv2  --> <class 'torch.nn.modules.conv.Conv2d'>
layer4.0.bn2    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer4.0.downsample --> <class 'torch.nn.modules.container.Sequential'>
layer4.0.downsample.0 --> <class 'torch.nn.modules.conv.Conv2d'>
layer4.0.downsample.1 --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer4.1        --> <class 'torchvision.models.resnet.BasicBlock'>
layer4.1.conv1  --> <class 'torch.nn.modules.conv.Conv2d'>
layer4.1.bn1    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
layer4.1.relu   --> <class 'torch.nn.modules.activation.ReLU'>
layer4.1.conv2  --> <class 'torch.nn.modules.conv.Conv2d'>
layer4.1.bn2    --> <class 'torch.nn.modules.batchnorm.BatchNorm2d'>
avgpool         --> <class 'torch.nn.modules.pooling.AdaptiveAvgPool2d'>
fc              --> <class 'torch.nn.modules.linear.Linear'>

3. named_parameters:

  • named_parameters 返回一个生成器,它包含模型中所有参数的名称和参数值对。
  • 它递归地遍历模型,返回所有可训练参数的名称和参数张量。
  • 这个方法用于获取和检查模型中的参数,例如在打印模型参数、保存模型或加载模型时使用。

示例:

from torchvision.models import resnet18

model = resnet18()
for name, param in model.named_parame

conv1.weight              --> torch.Size([64, 3, 7, 7])
bn1.weight                --> torch.Size([64])
bn1.bias                  --> torch.Size([64])
layer1.0.conv1.weight     --> torch.Size([64, 64, 3, 3])
layer1.0.bn1.weight       --> torch.Size([64])
layer1.0.bn1.bias         --> torch.Size([64])
layer1.0.conv2.weight     --> torch.Size([64, 64, 3, 3])
layer1.0.bn2.weight       --> torch.Size([64])
layer1.0.bn2.bias         --> torch.Size([64])
layer1.1.conv1.weight     --> torch.Size([64, 64, 3, 3])
layer1.1.bn1.weight       --> torch.Size([64])
layer1.1.bn1.bias         --> torch.Size([64])
layer1.1.conv2.weight     --> torch.Size([64, 64, 3, 3])
layer1.1.bn2.weight       --> torch.Size([64])
layer1.1.bn2.bias         --> torch.Size([64])
layer2.0.conv1.weight     --> torch.Size([128, 64, 3, 3])
layer2.0.bn1.weight       --> torch.Size([128])
layer2.0.bn1.bias         --> torch.Size([128])
layer2.0.conv2.weight     --> torch.Size([128, 128, 3, 3])
layer2.0.bn2.weight       --> torch.Size([128])
layer2.0.bn2.bias         --> torch.Size([128])
layer2.0.downsample.0.weight --> torch.Size([128, 64, 1, 1])
layer2.0.downsample.1.weight --> torch.Size([128])
layer2.0.downsample.1.bias --> torch.Size([128])
layer2.1.conv1.weight     --> torch.Size([128, 128, 3, 3])
layer2.1.bn1.weight       --> torch.Size([128])
layer2.1.bn1.bias         --> torch.Size([128])
layer2.1.conv2.weight     --> torch.Size([128, 128, 3, 3])
layer2.1.bn2.weight       --> torch.Size([128])
layer2.1.bn2.bias         --> torch.Size([128])
layer3.0.conv1.weight     --> torch.Size([256, 128, 3, 3])
layer3.0.bn1.weight       --> torch.Size([256])
layer3.0.bn1.bias         --> torch.Size([256])
layer3.0.conv2.weight     --> torch.Size([256, 256, 3, 3])
layer3.0.bn2.weight       --> torch.Size([256])
layer3.0.bn2.bias         --> torch.Size([256])
layer3.0.downsample.0.weight --> torch.Size([256, 128, 1, 1])
layer3.0.downsample.1.weight --> torch.Size([256])
layer3.0.downsample.1.bias --> torch.Size([256])
layer3.1.conv1.weight     --> torch.Size([256, 256, 3, 3])
layer3.1.bn1.weight       --> torch.Size([256])
layer3.1.bn1.bias         --> torch.Size([256])
layer3.1.conv2.weight     --> torch.Size([256, 256, 3, 3])
layer3.1.bn2.weight       --> torch.Size([256])
layer3.1.bn2.bias         --> torch.Size([256])
layer4.0.conv1.weight     --> torch.Size([512, 256, 3, 3])
layer4.0.bn1.weight       --> torch.Size([512])
layer4.0.bn1.bias         --> torch.Size([512])
layer4.0.conv2.weight     --> torch.Size([512, 512, 3, 3])
layer4.0.bn2.weight       --> torch.Size([512])
layer4.0.bn2.bias         --> torch.Size([512])
layer4.0.downsample.0.weight --> torch.Size([512, 256, 1, 1])
layer4.0.downsample.1.weight --> torch.Size([512])
layer4.0.downsample.1.bias --> torch.Size([512])
layer4.1.conv1.weight     --> torch.Size([512, 512, 3, 3])
layer4.1.bn1.weight       --> torch.Size([512])
layer4.1.bn1.bias         --> torch.Size([512])
layer4.1.conv2.weight     --> torch.Size([512, 512, 3, 3])
layer4.1.bn2.weight       --> torch.Size([512])
layer4.1.bn2.bias         --> torch.Size([512])
fc.weight                 --> torch.Size([1000, 512])
fc.bias                   --> torch.Size([1000])

总结来说,named_children 用于获取模型的直接子模块,named_modules 用于获取模型的所有模块(包括嵌套的子模块),而 named_parameters 用于获取模型中的所有参数。这些方法在模型调试、分析和优化时非常有用。

相关推荐
龙的爹2333几秒前
论文 | Model-tuning Via Prompts Makes NLP Models Adversarially Robust
人工智能·gpt·深度学习·语言模型·自然语言处理·prompt
工业机器视觉设计和实现13 分钟前
cnn突破四(生成卷积核与固定核对比)
人工智能·深度学习·cnn
醒了就刷牙15 分钟前
58 深层循环神经网络_by《李沐:动手学深度学习v2》pytorch版
pytorch·rnn·深度学习
985小水博一枚呀17 分钟前
【对于Python爬虫的理解】数据挖掘、信息聚合、价格监控、新闻爬取等,附代码。
爬虫·python·深度学习·数据挖掘
立秋678929 分钟前
Python的defaultdict详解
服务器·windows·python
萧鼎42 分钟前
Python第三方库选择与使用陷阱避免
开发语言·python
白拾1 小时前
使用Conda管理python环境的指南
开发语言·python·conda
我算是程序猿1 小时前
用AI做电子萌宠,快速涨粉变现
人工智能·stable diffusion·aigc
萱仔学习自我记录1 小时前
微调大语言模型——超详细步骤
人工智能·深度学习·机器学习
是刃小木啦~1 小时前
三维模型点云化工具V1.0使用介绍:将三维模型进行点云化生成
python·软件工程·pyqt·工业软件