[Pytorch系列-48]:如何查看和修改预定义神经网络的网络架构、网络参数属性

2023-11-01

作者主页(文火冰糖的硅基工坊):文火冰糖(王文兵)的博客_文火冰糖的硅基工坊_CSDN博客

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/121342500


目录

第1章 FineTuning、Transfer Trainning的理论基础与深度解析

第2章 查看预定义神经网络的网络架构

2.1 前置条件

2.2 生成预定义网络实例

2.3 显示网络结构

2.4 查看网络的内部特定结构以及对应的名称

第3章 查看预定义神经网络的参数

3.1 查看模型参数的名称以及结构

3.2 查看模型全部参数的结构以及当前的数值

3.3 无名查看模型参数的是否可训练的属性

3.4 有名查看模型参数的是否可训练的属性

第4章 修改网络的结构与参数

4.1 # 锁定网络参数的训练

4.2 替换全连接网络

4.3 显示全连接网络


第1章 FineTuning、Transfer Trainning的理论基础与深度解析

[人工智能-深度学习-46]:FineTuning(微调)、Transfer Trainning(迁移学习)的理论基础与深度解析_文火冰糖(王文兵)的博客-CSDN博客第1张 前言:常见的工程诉求与特点(1)数据集欠缺个人的数据集小,无法提供向ImageNet这样的大数据集,但又想利用在ImageNet上训练的模型好的模型,为我所用,即基于在一些知名的数据集上训练好的模型,在进一步的训练,以满足自己的应用场景的需求,而无需重头开始训练。(2)分类数多变个人特定的应用,分类的种类与Image(1000种分类)等知名数据集的分类的种类不同,我们想在已经训练好模型的基础上,做适当的重新训练,以支持我们自己的分类数目,如100分类。(3)防止过度训练,即过..https://blog.csdn.net/HiWangWenBing/article/details/121312417

第2章 查看预定义神经网络的网络架构

2.1 前置条件

#环境准备
import numpy as np              # numpy数组库
import math                     # 数学运算库
import matplotlib.pyplot as plt # 画图库
import time as time

import torch             # torch基础库
import torch.nn as nn    # torch神经网络库
import torch.nn.functional as F
import torchvision.datasets as dataset  #公开数据集的下载和管理
import torchvision.transforms as transforms  #公开数据集的预处理库,格式转换
import torchvision.utils as utils 
import torch.utils.data as data_utils  #对数据集进行分批加载的工具集
from PIL import Image #图片显示
from collections import OrderedDict
import torchvision.models as models

print("Hello World")
print(torch.__version__)
print(torch.cuda.is_available())
print(torch.version.cuda)
print(torch.backends.cudnn.version())

2.2 生成预定义网络实例

# 定义升级网络
model = models.resnet101()

2.3 显示网络结构

# 显示网络的全部架构
print(model)
ResNet(
  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (relu): ReLU(inplace=True)
  (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
  (layer1): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer2): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (3): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer3): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (3): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (4): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (5): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (6): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (7): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (8): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (9): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (10): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (11): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (12): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (13): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (14): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (15): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (16): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (17): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (18): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (19): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (20): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (21): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (22): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer4): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
  (fc): Linear(in_features=2048, out_features=1000, bias=True)
)

2.4 查看网络的内部特定结构以及对应的名称

# 查看网络的内部特定结构以及对应的名称,以后续替换相应的层
print(model.conv1)   #显示conv1层的信息
print(model.bn1)     #显示bn1层的信息
print(model.relu)
print(model.maxpool) #显示maxpool层的信息
print(model.avgpool) #显示avgpool层的信息
print(model.fc)      #显示fc层的信息
print(model.fc.in_features)   #显示fc层的输入特征的个数
print(model.fc.out_features)  #显示fc层的输出特征的个数
Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
ReLU(inplace=True)
MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
AdaptiveAvgPool2d(output_size=(1, 1))
Linear(in_features=2048, out_features=1000, bias=True)
2048
1000

备注:

默认输出是1000分类

第3章 查看预定义神经网络的参数

3.1 查看模型参数的名称以及结构

for name, parameters in model.named_parameters():
    print(name, ':', parameters.size())
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, 1, 1])
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.0.conv3.weight : torch.Size([256, 64, 1, 1])
layer1.0.bn3.weight : torch.Size([256])
layer1.0.bn3.bias : torch.Size([256])
layer1.0.downsample.0.weight : torch.Size([256, 64, 1, 1])
layer1.0.downsample.1.weight : torch.Size([256])
layer1.0.downsample.1.bias : torch.Size([256])
layer1.1.conv1.weight : torch.Size([64, 256, 1, 1])
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])
layer1.1.conv3.weight : torch.Size([256, 64, 1, 1])
layer1.1.bn3.weight : torch.Size([256])
layer1.1.bn3.bias : torch.Size([256])
layer1.2.conv1.weight : torch.Size([64, 256, 1, 1])
layer1.2.bn1.weight : torch.Size([64])
layer1.2.bn1.bias : torch.Size([64])
layer1.2.conv2.weight : torch.Size([64, 64, 3, 3])
layer1.2.bn2.weight : torch.Size([64])
layer1.2.bn2.bias : torch.Size([64])
layer1.2.conv3.weight : torch.Size([256, 64, 1, 1])
layer1.2.bn3.weight : torch.Size([256])
layer1.2.bn3.bias : torch.Size([256])
layer2.0.conv1.weight : torch.Size([128, 256, 1, 1])
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.conv3.weight : torch.Size([512, 128, 1, 1])
layer2.0.bn3.weight : torch.Size([512])
layer2.0.bn3.bias : torch.Size([512])
layer2.0.downsample.0.weight : torch.Size([512, 256, 1, 1])
layer2.0.downsample.1.weight : torch.Size([512])
layer2.0.downsample.1.bias : torch.Size([512])
layer2.1.conv1.weight : torch.Size([128, 512, 1, 1])
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])
layer2.1.conv3.weight : torch.Size([512, 128, 1, 1])
layer2.1.bn3.weight : torch.Size([512])
layer2.1.bn3.bias : torch.Size([512])
layer2.2.conv1.weight : torch.Size([128, 512, 1, 1])
layer2.2.bn1.weight : torch.Size([128])
layer2.2.bn1.bias : torch.Size([128])
layer2.2.conv2.weight : torch.Size([128, 128, 3, 3])
layer2.2.bn2.weight : torch.Size([128])
layer2.2.bn2.bias : torch.Size([128])
layer2.2.conv3.weight : torch.Size([512, 128, 1, 1])
layer2.2.bn3.weight : torch.Size([512])
layer2.2.bn3.bias : torch.Size([512])
layer2.3.conv1.weight : torch.Size([128, 512, 1, 1])
layer2.3.bn1.weight : torch.Size([128])
layer2.3.bn1.bias : torch.Size([128])
layer2.3.conv2.weight : torch.Size([128, 128, 3, 3])
layer2.3.bn2.weight : torch.Size([128])
layer2.3.bn2.bias : torch.Size([128])
layer2.3.conv3.weight : torch.Size([512, 128, 1, 1])
layer2.3.bn3.weight : torch.Size([512])
layer2.3.bn3.bias : torch.Size([512])
layer3.0.conv1.weight : torch.Size([256, 512, 1, 1])
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.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.0.bn3.weight : torch.Size([1024])
layer3.0.bn3.bias : torch.Size([1024])
layer3.0.downsample.0.weight : torch.Size([1024, 512, 1, 1])
layer3.0.downsample.1.weight : torch.Size([1024])
layer3.0.downsample.1.bias : torch.Size([1024])
layer3.1.conv1.weight : torch.Size([256, 1024, 1, 1])
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])
layer3.1.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.1.bn3.weight : torch.Size([1024])
layer3.1.bn3.bias : torch.Size([1024])
layer3.2.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.2.bn1.weight : torch.Size([256])
layer3.2.bn1.bias : torch.Size([256])
layer3.2.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.2.bn2.weight : torch.Size([256])
layer3.2.bn2.bias : torch.Size([256])
layer3.2.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.2.bn3.weight : torch.Size([1024])
layer3.2.bn3.bias : torch.Size([1024])
layer3.3.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.3.bn1.weight : torch.Size([256])
layer3.3.bn1.bias : torch.Size([256])
layer3.3.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.3.bn2.weight : torch.Size([256])
layer3.3.bn2.bias : torch.Size([256])
layer3.3.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.3.bn3.weight : torch.Size([1024])
layer3.3.bn3.bias : torch.Size([1024])
layer3.4.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.4.bn1.weight : torch.Size([256])
layer3.4.bn1.bias : torch.Size([256])
layer3.4.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.4.bn2.weight : torch.Size([256])
layer3.4.bn2.bias : torch.Size([256])
layer3.4.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.4.bn3.weight : torch.Size([1024])
layer3.4.bn3.bias : torch.Size([1024])
layer3.5.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.5.bn1.weight : torch.Size([256])
layer3.5.bn1.bias : torch.Size([256])
layer3.5.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.5.bn2.weight : torch.Size([256])
layer3.5.bn2.bias : torch.Size([256])
layer3.5.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.5.bn3.weight : torch.Size([1024])
layer3.5.bn3.bias : torch.Size([1024])
layer3.6.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.6.bn1.weight : torch.Size([256])
layer3.6.bn1.bias : torch.Size([256])
layer3.6.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.6.bn2.weight : torch.Size([256])
layer3.6.bn2.bias : torch.Size([256])
layer3.6.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.6.bn3.weight : torch.Size([1024])
layer3.6.bn3.bias : torch.Size([1024])
layer3.7.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.7.bn1.weight : torch.Size([256])
layer3.7.bn1.bias : torch.Size([256])
layer3.7.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.7.bn2.weight : torch.Size([256])
layer3.7.bn2.bias : torch.Size([256])
layer3.7.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.7.bn3.weight : torch.Size([1024])
layer3.7.bn3.bias : torch.Size([1024])
layer3.8.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.8.bn1.weight : torch.Size([256])
layer3.8.bn1.bias : torch.Size([256])
layer3.8.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.8.bn2.weight : torch.Size([256])
layer3.8.bn2.bias : torch.Size([256])
layer3.8.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.8.bn3.weight : torch.Size([1024])
layer3.8.bn3.bias : torch.Size([1024])
layer3.9.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.9.bn1.weight : torch.Size([256])
layer3.9.bn1.bias : torch.Size([256])
layer3.9.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.9.bn2.weight : torch.Size([256])
layer3.9.bn2.bias : torch.Size([256])
layer3.9.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.9.bn3.weight : torch.Size([1024])
layer3.9.bn3.bias : torch.Size([1024])
layer3.10.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.10.bn1.weight : torch.Size([256])
layer3.10.bn1.bias : torch.Size([256])
layer3.10.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.10.bn2.weight : torch.Size([256])
layer3.10.bn2.bias : torch.Size([256])
layer3.10.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.10.bn3.weight : torch.Size([1024])
layer3.10.bn3.bias : torch.Size([1024])
layer3.11.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.11.bn1.weight : torch.Size([256])
layer3.11.bn1.bias : torch.Size([256])
layer3.11.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.11.bn2.weight : torch.Size([256])
layer3.11.bn2.bias : torch.Size([256])
layer3.11.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.11.bn3.weight : torch.Size([1024])
layer3.11.bn3.bias : torch.Size([1024])
layer3.12.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.12.bn1.weight : torch.Size([256])
layer3.12.bn1.bias : torch.Size([256])
layer3.12.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.12.bn2.weight : torch.Size([256])
layer3.12.bn2.bias : torch.Size([256])
layer3.12.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.12.bn3.weight : torch.Size([1024])
layer3.12.bn3.bias : torch.Size([1024])
layer3.13.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.13.bn1.weight : torch.Size([256])
layer3.13.bn1.bias : torch.Size([256])
layer3.13.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.13.bn2.weight : torch.Size([256])
layer3.13.bn2.bias : torch.Size([256])
layer3.13.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.13.bn3.weight : torch.Size([1024])
layer3.13.bn3.bias : torch.Size([1024])
layer3.14.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.14.bn1.weight : torch.Size([256])
layer3.14.bn1.bias : torch.Size([256])
layer3.14.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.14.bn2.weight : torch.Size([256])
layer3.14.bn2.bias : torch.Size([256])
layer3.14.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.14.bn3.weight : torch.Size([1024])
layer3.14.bn3.bias : torch.Size([1024])
layer3.15.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.15.bn1.weight : torch.Size([256])
layer3.15.bn1.bias : torch.Size([256])
layer3.15.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.15.bn2.weight : torch.Size([256])
layer3.15.bn2.bias : torch.Size([256])
layer3.15.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.15.bn3.weight : torch.Size([1024])
layer3.15.bn3.bias : torch.Size([1024])
layer3.16.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.16.bn1.weight : torch.Size([256])
layer3.16.bn1.bias : torch.Size([256])
layer3.16.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.16.bn2.weight : torch.Size([256])
layer3.16.bn2.bias : torch.Size([256])
layer3.16.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.16.bn3.weight : torch.Size([1024])
layer3.16.bn3.bias : torch.Size([1024])
layer3.17.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.17.bn1.weight : torch.Size([256])
layer3.17.bn1.bias : torch.Size([256])
layer3.17.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.17.bn2.weight : torch.Size([256])
layer3.17.bn2.bias : torch.Size([256])
layer3.17.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.17.bn3.weight : torch.Size([1024])
layer3.17.bn3.bias : torch.Size([1024])
layer3.18.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.18.bn1.weight : torch.Size([256])
layer3.18.bn1.bias : torch.Size([256])
layer3.18.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.18.bn2.weight : torch.Size([256])
layer3.18.bn2.bias : torch.Size([256])
layer3.18.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.18.bn3.weight : torch.Size([1024])
layer3.18.bn3.bias : torch.Size([1024])
layer3.19.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.19.bn1.weight : torch.Size([256])
layer3.19.bn1.bias : torch.Size([256])
layer3.19.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.19.bn2.weight : torch.Size([256])
layer3.19.bn2.bias : torch.Size([256])
layer3.19.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.19.bn3.weight : torch.Size([1024])
layer3.19.bn3.bias : torch.Size([1024])
layer3.20.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.20.bn1.weight : torch.Size([256])
layer3.20.bn1.bias : torch.Size([256])
layer3.20.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.20.bn2.weight : torch.Size([256])
layer3.20.bn2.bias : torch.Size([256])
layer3.20.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.20.bn3.weight : torch.Size([1024])
layer3.20.bn3.bias : torch.Size([1024])
layer3.21.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.21.bn1.weight : torch.Size([256])
layer3.21.bn1.bias : torch.Size([256])
layer3.21.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.21.bn2.weight : torch.Size([256])
layer3.21.bn2.bias : torch.Size([256])
layer3.21.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.21.bn3.weight : torch.Size([1024])
layer3.21.bn3.bias : torch.Size([1024])
layer3.22.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.22.bn1.weight : torch.Size([256])
layer3.22.bn1.bias : torch.Size([256])
layer3.22.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.22.bn2.weight : torch.Size([256])
layer3.22.bn2.bias : torch.Size([256])
layer3.22.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.22.bn3.weight : torch.Size([1024])
layer3.22.bn3.bias : torch.Size([1024])
layer4.0.conv1.weight : torch.Size([512, 1024, 1, 1])
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.conv3.weight : torch.Size([2048, 512, 1, 1])
layer4.0.bn3.weight : torch.Size([2048])
layer4.0.bn3.bias : torch.Size([2048])
layer4.0.downsample.0.weight : torch.Size([2048, 1024, 1, 1])
layer4.0.downsample.1.weight : torch.Size([2048])
layer4.0.downsample.1.bias : torch.Size([2048])
layer4.1.conv1.weight : torch.Size([512, 2048, 1, 1])
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])
layer4.1.conv3.weight : torch.Size([2048, 512, 1, 1])
layer4.1.bn3.weight : torch.Size([2048])
layer4.1.bn3.bias : torch.Size([2048])
layer4.2.conv1.weight : torch.Size([512, 2048, 1, 1])
layer4.2.bn1.weight : torch.Size([512])
layer4.2.bn1.bias : torch.Size([512])
layer4.2.conv2.weight : torch.Size([512, 512, 3, 3])
layer4.2.bn2.weight : torch.Size([512])
layer4.2.bn2.bias : torch.Size([512])
layer4.2.conv3.weight : torch.Size([2048, 512, 1, 1])
layer4.2.bn3.weight : torch.Size([2048])
layer4.2.bn3.bias : torch.Size([2048])
fc.weight : torch.Size([1000, 2048])
fc.bias : torch.Size([1000])

3.2 查看模型全部参数的结构以及当前的数值

# 查看模型全部参数的结构以及当前的数值
for parameters in model.parameters():
    print(parameters)
Parameter containing:
tensor([[[[ 0.0129,  0.0295,  0.0005,  ...,  0.0436, -0.0144,  0.0082],
          [ 0.0123,  0.0027, -0.0260,  ..., -0.0539, -0.0083, -0.0259],
          [ 0.0027, -0.0140,  0.0041,  ..., -0.0145,  0.0109, -0.0182],
          ...,
          [ 0.0128, -0.0022,  0.0388,  ..., -0.0116,  0.0571, -0.0283],
          [-0.0015, -0.0179, -0.0010,  ..., -0.0110,  0.0009,  0.0310],
          [ 0.0100, -0.0215,  0.0241,  ..., -0.0019, -0.0834, -0.0293]],

         [[ 0.0086,  0.0038,  0.0213,  ...,  0.0403,  0.0004, -0.0281],
          [-0.0243,  0.0175, -0.0021,  ..., -0.0457, -0.0118, -0.0098],
          [-0.0215,  0.0212,  0.0349,  ..., -0.0090, -0.0021, -0.0105],
          

.....................

3.3 无名查看模型参数的是否可训练的属性


# 无名查看模型参数的是否可训练的属性
for param in model.parameters():
    print(param.name, param.requires_grad)
None True
None True
None True
None True
None True
........

3.4 有名查看模型参数的是否可训练的属性

# 有名查看模型参数的是否可训练的属性
for name, parameters in model.named_parameters():
    print(name, ':', parameters.requires_grad)

......

layer4.2.bn1.weight : True
layer4.2.bn1.bias : True
layer4.2.conv2.weight : True
layer4.2.bn2.weight : True
layer4.2.bn2.bias : True
layer4.2.conv3.weight : True
layer4.2.bn3.weight : True
layer4.2.bn3.bias : True
fc.weight : True
fc.bias : True

此时全连接参数是可训练的。

第4章 修改网络的结构与参数

4.1 # 锁定网络参数的训练

# 锁定网络参数的训练
for param in model.parameters():
    param.requires_grad = False


# 有查看模型参数的是否可训练的属性
for name, parameters in model.named_parameters():
    print(name, ':', parameters.requires_grad)
layer4.2.conv1.weight : False
layer4.2.bn1.weight : False
layer4.2.bn1.bias : False
layer4.2.conv2.weight : False
layer4.2.bn2.weight : False
layer4.2.bn2.bias : False
layer4.2.conv3.weight : False
layer4.2.bn3.weight : False
layer4.2.bn3.bias : False
fc.weight : False
fc.bias : False

备注:所有参数不可训练

4.2 替换全连接网络

#替换升级网络的全连接层
model.fc = nn.Sequential(nn.Linear(in_features = 2048, out_features = 100))


# 有查看模型参数的是否可训练的属性
for name, parameters in model.named_parameters():
    print(name, ':', parameters.requires_grad)
layer4.2.conv1.weight : False
layer4.2.bn1.weight : False
layer4.2.bn1.bias : False
layer4.2.conv2.weight : False
layer4.2.bn2.weight : False
layer4.2.bn2.bias : False
layer4.2.conv3.weight : False
layer4.2.bn3.weight : False
layer4.2.bn3.bias : False
fc.0.weight : True
fc.0.bias : True

备注:只替换了全连接网络

4.3 显示全连接网络

print(model.fc)      #显示fc层的信息
Sequential(
  (0): Linear(in_features=2048, out_features=100, bias=True)
)

作者主页(文火冰糖的硅基工坊):文火冰糖(王文兵)的博客_文火冰糖的硅基工坊_CSDN博客

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/121342500

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[Pytorch系列-48]:如何查看和修改预定义神经网络的网络架构、网络参数属性 的相关文章

随机推荐

  • RabbitMQ应用之消息堆积、消息丢失、有序消费、重复消费

    文章目录 前言 一 消息堆积 1 消息堆积的产生与影响 2 消息堆积的解决方案 二 消息丢失 1 情景 2 解决方案 三 有序消费 1 情景 2 解决方案 四 重复消费 1 情景 2 解决方案 前言 最近接触了多线程和MQ等性能相关的内容
  • HTML 特殊符号编码对照表

    特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 Alpha 913 Beta 914 Gamma 915 Delta 916 Epsilon 917 Zeta 918 Eta 919 Thet
  • 工业互联网产业链全景图深度分析

    工业互联网领域有哪些投资机会 新基建 是与传统基建相对应 结合新一轮科技革命和产业变革特征 面向国家战略需求 为经济社会的创新 协调 绿色 开放 共享发展提供底层支撑的具有乘数效应的战略性 网络型基础设施 其中 新基建 包括5G基建 特高压
  • openwrt 修改feeds.conf.default为GitHub源

    lede和openwrt合并之后 lede官网挂了 git openwrt org 也访问不了 只好去github上找最新源码 git clone https github com openwrt openwrt git 复制代码 最新的l
  • 嵌入式linux 配置usb otg,嵌入式系统设计中的USB OTG方案

    速外设操作时最大为80mA TD1120整个芯片支持功率节省模式 包括主机控制器以及外设控制器的延缓模式以使功率消耗最小化www cechina cn 延长系统电池寿命 对于移动设备来说 电池寿命是很关键的性能 接口性能表现 USB数据传输
  • Bitmap之压缩方案

    文章目录 前言 1 基础知识 1 1色彩模式 1 2四种模式的区别 1 3具体对比 1 4bitmap内存占用大小计算方式 1 5图片存在的形式 1 6BitampFactory加载Bitmap对象的方式 2 压缩方案 2 1采样率压缩 2
  • Bugku 计算器

    首先打开题目链接 发现一个式子 但答案有三位数 而只能输入一个数字 直接F12查看原代码 发现maxlenthen 1 maxlenthen意思是文件域可接受的字符数量的上限 可输入字符串最大的长度 容质 所以把1改为3就好啦 然后得到fl
  • Redisson源码-多线程之首个获取锁的线程加解锁流程

    Redisson源码 多线程之首个获取锁的线程加解锁流程 简介 当有多个线程同时去获取同一把锁时 第一个获取到锁的线程会进行加解锁 其他线程需订阅消息并等待锁释放 以下源码分析基于redisson 3 17 6版本 不同版本源码会有些许不同
  • openEuler 20.03 LTS SP2以及SP3安装完gnome后,gdm登陆进入不了桌面问题

    一 问题原因 是由于CVE 2020 17489相关补丁引入的 暂不清楚是何原因造成 但除去该相关补丁之后 该问题消失 在网上查了下 CVE 2020 17489的问题是gnome shell的某些配置中会发现 注销账户时 登陆对话框中的密
  • SQLI-Labs(15-17)

    目录 15关 16关 17关 15关 看到这个那么我们可以首先尝试报错或者盲注 payload or length database 8 qwe 在这里我们发现and 会报错 跟之前我们利用and爆错不一样 那为什么这里我们在post传参时
  • Dubbo是什么

    Dubbo是什么 Dubbo是一个分布式服务框架 致力于提供高性能和透明化的RPC远程服务调用方案 以及SOA服务治理方案 简单的说 dubbo就是个服务框架 如果没有分布式的需求 其实是不需要用的 只有在分布式的时候 才有dubbo这样的
  • 统计oracle 数据库 lawpeople表lawtype字段多个值只统计一次问题,按照地区分类

    select temparea name case when lawtype like 501 then 501 when lawtype like 502 then 502 when lawtype like 503 then 503 w
  • CSR867x — 如何看懂一份psr文件

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XX 作 者 文化人 XX 联系方式 XX 版权声明 原创文章 欢迎评论和转载 转载时能告诉我一声就最好了 XX 要说的话 作者
  • Dubbo路由规则:静态标签的使用与扩展

    一 路由的流程 路由是通过互联网把信息从源地址传输到目的地址的过程 而决定路由目标地址的是路由规则 在Dubbo里 路由规则在发起一次RPC调用前起到过滤目标服务器地址的作用 过滤后的地址列表 将作为消费端最终发起RPC调用的备选地址 它能
  • LeetCode 62. 不同路径

    欢迎来到茶色岛独家岛屿 本期将为大家揭晓LeetCode 62 不同路径 做好准备了么 那么开始吧 一 题目名称 LeetCode 62 不同路径 二 题目要求 一个机器人位于一个 m x n 网格的左上角 起始点在下图中标记为 Start
  • git 导出版本之间差异文件

    查看 commit id 首先用 git log 查看版本库日志 找出需要导出的 commit id git log pretty oneline 456bcbccd91278f7fdf6bf11bc73c4e3a6193c7f HEAD
  • 基于深度神经网络的社交媒体用户级心理压力检测

    User Level Psychological Stress Detection from Social Media Using Deep Neural Network 基于深度神经网络的社交媒体用户级心理压力检测 ABSTRACT It
  • 软件anyconnec-win安装下载

    anyconnec win介绍 1 安装下载地址 http www drv5 cn sfinfo 14287 html softdown 找到适合自己操作系统的版本 下载并安装 2 直接安装下载点击next就ok了 需要注意的是 下载安装完
  • IDEA小技巧

    IDEA小技巧 常用快捷键 Alt Insert 可以自动生成get set toString方法 Alt Enter 可以帮助解决各种报错 抛个异常啊 导个包啊之类的 常见行操作 Shift Enter 添加空行 相比普通换行 不管光标在
  • [Pytorch系列-48]:如何查看和修改预定义神经网络的网络架构、网络参数属性

    作者主页 文火冰糖的硅基工坊 文火冰糖 王文兵 的博客 文火冰糖的硅基工坊 CSDN博客 本文网址 https blog csdn net HiWangWenBing article details 121342500 目录 第1章 Fin