当前位置:网站首页>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
边栏推荐
- kubernetes之常用Pod控制器的使用
- [leetcode daily question] install fence
- 网站压测工具Apache-ab,webbench,Apache-Jemeter
- [backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)
- Deep learning - Super parameter setting
- Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
- 控制结构(二)
- Knn,Kmeans和GMM
- 推荐搜索 常用评价指标
- Common interview questions of operating system:
猜你喜欢
Byte interview programming question: the minimum number of K
服务器中毒了怎么办?服务器怎么防止病毒入侵?
About UDP receiving ICMP port unreachable
TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2
Krpano panorama vtour folder and tour
Functions (Part I)
群体智能自主作业智慧农场项目启动及实施方案论证会议
我的树莓派 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
setcontext getcontext makecontext swapcontext
让阿里P8都为之着迷的分布式核心原理解析到底讲了啥?看完我惊了
随机推荐
携号转网最大赢家是中国电信,为何人们嫌弃中国移动和中国联通?
Analysis of common storage types and FTP active and passive modes
Machine learning - logistic regression
The El tree implementation only displays a certain level of check boxes and selects radio
Sword finger offer (2) -- for Huawei
控制结构(一)
自动化测试框架常见类型▏自动化测试就交给软件测评机构
通過 PDO ODBC 將 PHP 連接到 MySQL
php函数
Openstack command operation
Functions (Part I)
KNN, kmeans and GMM
Node.js ODBC连接PostgreSQL
YML references other variables
Tun model of flannel principle
电脑怎么重装系统后显示器没有信号了
[leetcode daily question] install fence
fatal error: torch/extension. h: No such file or directory
字节面试 transformer相关问题 整理复盘
[backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)