当前位置:网站首页>Pytorch中named_parameters、named_children、named_modules函数
Pytorch中named_parameters、named_children、named_modules函数
2022-04-23 15:32:00 【遇到坎就得迈过去】
named_parameters函数
- 以迭代器的方式返回model中所有的参数,返回值是一个
字典
:包含参数的名称
和数值大小
; - 内部实现时使用了
递归算法
,所以对于嵌套的网络参数,会递归遍历
,输出最底层的参数
,参看下面的例子;
named_children()函数
- 该函数用来输出网络中的
第一层模块名称和实例对象
,只会展示最上层的模块名;
named_modules()函数
- 该函数用来递归输出网络中的
每一层模块名称和实例对象
,会显示所有层每一模块的名称
例子
import torch
import torch.nn as nn
class TestModel(nn.Module):
def __init__(self):
super(TestModel,self).__init__()
# 常规的卷积层,输入通道为3,输出通道为12
self.conv1 = nn.Conv2d(3, 12, kernel_size=3, padding=1)
self.layer1 = nn.Sequential(
nn.Conv2d(12, 6, kernel_size=3, padding=1),
nn.Conv2d(6, 6, kernel_size=3, padding=1),
nn.ReLU(inplace=True)
)
def forward(self, x):
x = self.conv1(x)
x = self.layer1(x)
model = TestModel()
named_parameters = model.named_parameters()
print('------------parameters-----------------')
for name, parameter in named_parameters:
print(name)
named_children = model.named_children()
print('------------parameters-----------------')
for name, children in named_children:
print(name)
named_modules = model.named_modules()
print('------------modules-----------------')
for name, module in named_modules:
print(name)
------------输出--------------------------
------------parameters-----------------
conv1.weight
conv1.bias
layer1.0.weight # 可以看到对于sequential模块中的模块会给一个数字编号
layer1.0.bias
layer1.1.weight
layer1.1.bias
------------parameters-----------------
conv1
layer1
------------modules-----------------
conv1
layer1
layer1.0
layer1.1
layer1.2
版权声明
本文为[遇到坎就得迈过去]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_43705697/article/details/124361476
边栏推荐
- Redis cluster principle
- Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
- Connect PHP to MSSQL via PDO ODBC
- Nacos program connects to mysql8 0+ NullPointerException
- T2 icloud calendar cannot be synchronized
- Llvm - generate if else and pH
- T2 iCloud日历无法同步
- G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
- fatal error: torch/extension.h: No such file or directory
- 推荐搜索 常用评价指标
猜你喜欢
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
Functions (Part I)
Advantages, disadvantages and selection of activation function
自主作业智慧农场创新论坛
Explanation 2 of redis database (redis high availability, persistence and performance management)
Tun model of flannel principle
Basic operation of circular queue (Experiment)
X509 certificate cer format to PEM format
Tun equipment principle
Reptile exercises (1)
随机推荐
[leetcode daily question] install fence
kubernetes之常用Pod控制器的使用
调度系统使用注意事项
网站压测工具Apache-ab,webbench,Apache-Jemeter
Sword finger offer (1) -- for Huawei
Tun model of flannel principle
PSYNC synchronization of redis source code analysis
My raspberry PI zero 2W toss notes to record some problems and solutions
Cookie&Session
Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
How did the computer reinstall the system? The display has no signal
激活函数的优缺点和选择
Nacos程序连接MySQL8.0+ NullPointerException
Deeply learn the skills of parameter adjustment
MySQL InnoDB transaction
Independent operation smart farm Innovation Forum
Example of time complexity calculation
T2 iCloud日历无法同步
The El tree implementation only displays a certain level of check boxes and selects radio
自主作业智慧农场创新论坛