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 用于获取模型中的所有参数。这些方法在模型调试、分析和优化时非常有用。

相关推荐
MO2T15 分钟前
使用 Flask 构建基于 Dify 的企业资金投向与客户分类评估系统
后端·python·语言模型·flask
慢热型网友.18 分钟前
用 Docker 构建你的第一个 Python Flask 程序
python·docker·flask
Naiva18 分钟前
【小技巧】Python + PyCharm 小智AI配置MCP接入点使用说明(内测)( PyInstaller打包成 .exe 可执行文件)
开发语言·python·pycharm
云动雨颤23 分钟前
Python 自动化办公神器|一键转换所有文档为 PDF
运维·python
moonless022228 分钟前
🌈Transformer说人话版(二)位置编码 【持续更新ing】
人工智能·llm
小爷毛毛_卓寿杰28 分钟前
基于大模型与知识图谱的对话引导意图澄清系统技术解析
人工智能·llm
梅孔立35 分钟前
yum update 报错 Cannot find a valid baseurl for repo: centos-sclo-rh/x86_64 等解决办法
linux·python·centos
聚客AI39 分钟前
解构高效提示工程:分层模型、文本扩展引擎与可视化调试全链路指南
人工智能·llm·掘金·日新计划
喝过期的拉菲1 小时前
使用 Pytorch Lightning 时追踪指标和可视化指标
pytorch·可视化·lightning·指标追踪
前端付豪1 小时前
13、你还在 print 调试🧾?教你写出自己的日志系统
后端·python