当前位置:网站首页>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
边栏推荐
- Mumu, go all the way
- 激活函数的优缺点和选择
- cadence SPB17.4 - Active Class and Subclass
- MySQL installation process (steps for successful installation)
- Adobe Illustrator menu in Chinese and English
- Collation of errors encountered in the use of redis shake
- Deep learning - Super parameter setting
- 码住收藏▏软件测试报告模板范文来了
- adobe illustrator 菜單中英文對照
- HJ31 单词倒排
猜你喜欢
G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
让阿里P8都为之着迷的分布式核心原理解析到底讲了啥?看完我惊了
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
今日睡眠质量记录76分
Sorting and replying to questions related to transformer
【Leetcode-每日一题】安装栅栏
携号转网最大赢家是中国电信,为何人们嫌弃中国移动和中国联通?
Mumu, go all the way
网站压测工具Apache-ab,webbench,Apache-Jemeter
Functions (Part I)
随机推荐
Mysql database explanation (10)
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
Sword finger offer (2) -- for Huawei
setcontext getcontext makecontext swapcontext
Openstack theoretical knowledge
KNN, kmeans and GMM
cadence SPB17.4 - Active Class and Subclass
What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it
Basic operation of circular queue (Experiment)
Sword finger offer (1) -- for Huawei
Mysql database explanation (IX)
通过 PDO ODBC 将 PHP 连接到 MySQL
【backtrader源码解析18】yahoo.py 代码注释及解析(枯燥,对代码感兴趣,可以参考)
Have you really learned the operation of sequence table?
Mysql database explanation (VII)
JSON date time date format
Connectez PHP à MySQL via aodbc
Collation of errors encountered in the use of redis shake
redis-shake 使用中遇到的错误整理
T2 icloud calendar cannot be synchronized