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

相关推荐

  1. PyTorchTensorFlow简介

    2024-03-29 04:50:08       32 阅读
  2. TensorFlowPyTorch对比

    2024-03-29 04:50:08       9 阅读
  3. Pytorchresizereshape

    2024-03-29 04:50:08       30 阅读
  4. PyTorch Dataset、DataLoader enumerate()

    2024-03-29 04:50:08       40 阅读
  5. PyTorchAOTAutograd、PrimTorchTorchInductor

    2024-03-29 04:50:08       29 阅读
  6. PyTorch ,TensorFlowCaffe之间区别

    2024-03-29 04:50:08       41 阅读
  7. pytorchdatasetdataloader

    2024-03-29 04:50:08       20 阅读
  8. Pytorch框架下CNNRNN

    2024-03-29 04:50:08       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-29 04:50:08       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-29 04:50:08       20 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-29 04:50:08       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-29 04:50:08       20 阅读

热门阅读

  1. 【C++ RapidJson】使用RapidJson写入文件

    2024-03-29 04:50:08       21 阅读
  2. vue表单rules校验是动态的

    2024-03-29 04:50:08       22 阅读
  3. postcss安装和使用

    2024-03-29 04:50:08       20 阅读
  4. 蓝桥杯2019年第十三届省赛真题-数列求值

    2024-03-29 04:50:08       24 阅读
  5. 网络安全渗透测试工具

    2024-03-29 04:50:08       25 阅读
  6. 题目 2894: 肿瘤检测

    2024-03-29 04:50:08       22 阅读