pytorch系列8 --self.modules() 和 self.children()的区别
本文主要讲述:
- self.modue和self.children的区别与联系
说实话,我真的只想讲参数初始化方式,但总感觉在偏离的道路上越走越远。。。
在看一些pytorch文章讲述自定义参数初始化方式时,使用到了self.modules()
和self.children()
函数,觉得还是需要讲解一下的。
不如直接看一下代码:
import torch
from torch import nn# hyper parameters
in_dim=1
n_hidden_1=1
n_hidden_2=1
out_dim=1class Net(nn.Module):def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):super().__init__()self.layer = nn.Sequential(nn.Linear(in_dim, n_hidden_1), nn.ReLU(True))self.layer2 = nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2),nn.ReLU(True),)self.layer3 = nn.Linear(n_hidden_2, out_dim)# print(self.modules())print("children")for i, module in enumerate( self.children()):print(i, module)print("modules")for i, module in enumerate( self.modules()):print(i, module)def forward(self, x):x = self.layer1(x)x = self.layer2(x)x = self.layer3(x)return xmodel = Net(in_dim, n_hidden_1, n_hidden_2, out_dim)
网络结构解读:
这是一个三层的网络结构,将第一层的线性层和激活层放在一个nn.Sequential
层中,将第二层的线性层和激活函数放在第二个nn.Sequential
中,最后一个线性层作为单独第三层。
整个网络结构如下图所示:
接下来看一下代码__init__
的print函数的打印信息:
self.children()
out:
children
0 Sequential(
(0): Linear(in_features=1, out_features=1, bias=True)
(1): ReLU(inplace)
)
1 Sequential(
(0): Linear(in_features=1, out_features=1, bias=True)
(1): ReLU(inplace)
)
2 Linear(in_features=1, out_features=1, bias=True)
可以看出,self.children()
存储网络结构的子层模块,也就是net's children
那一层。
self.modules()
输出:
modules
第一层:
0 Net(
(layer): Sequential(
(0): Linear(in_features=1, out_features=1, bias=True)
(1): ReLU(inplace)
)
(layer2): Sequential(
(0): Linear(in_features=1, out_features=1, bias=True)
(1): ReLU(inplace)
)
(layer3): Linear(in_features=1, out_features=1, bias=True)
)
第二层:
1 Sequential(
(0): Linear(in_features=1, out_features=1, bias=True)
(1): ReLU(inplace)
)
2 Linear(in_features=1, out_features=1, bias=True)
3 ReLU(inplace)
第三层:
4 Sequential(
(0): Linear(in_features=1, out_features=1, bias=True)
(1): ReLU(inplace)
)
5 Linear(in_features=1, out_features=1, bias=True)
6 ReLU(inplace)
第四层:
7 Linear(in_features=1, out_features=1, bias=True)
可以看出,self.modules()采用深度优先遍历的方式,存储了net的所有模块,包括net itself
,net's children
, children of net's children
。
conclusion:
self.children()只包括网络模块的第一代儿子模块,而self.modules()包含网络模块的自己本身和所有后代模块。
参考:
https://discuss.pytorch.org/t/module-children-vs-module-modules/4551/3
pytorch系列8 --self.modules() 和 self.children()的区别相关推荐
- Pytorch中的model.modules()和model.children()的区别
Pytorch中的model.modules()和model.children()的区别 背景:最近在做网络模型中可视化的过程中,需要将网络结构中的某一层的特征进行输出.所以就遇到了这个问题,小小记录 ...
- self.modules() 和 self.children()的区别
代码: import torch import torch.nn as nninput_nc = 3 hide1_nc = 6 hide2_nc = 6 output_nc = 3class Net( ...
- PyTorch系列 (二): pytorch数据读取自制数据集并
PyTorch系列 (二): pytorch数据读取 PyTorch 1: How to use data in pytorch Posted by WangW on February 1, 2019 ...
- PyTorch系列入门到精通——GPU的使用
PyTorch系列入门到精通--GPU的使用
- PyTorch系列入门到精通——梯度消失与爆炸,损失函数
PyTorch系列入门到精通--梯度消失与爆炸
- PyTorch系列入门到精通——模型创建与nn.Module
PyTorch系列入门到精通--模型创建与nn.Module
- PyTorch系列入门到精通——BN、LN、IN and GN
PyTorch系列入门到精通--BN.LN.IN and GN
- PyTorch系列入门到精通——模型保存与加载
PyTorch系列入门到精通--模型保存与加载
- PyTorch系列入门到精通——生成对抗网络一瞥
PyTorch系列入门到精通--生成对抗网络一瞥
最新文章
- [Android工具]更新安卓百度云盘百度网盘资源搜索工具,安卓网盘搜索软件
- Python中安装bs4后,pycharm依然报错ModuleNotFoundError: No module named 'bs4'
- 计算机程序设计 期末考试题,《计算机编程ASP》期终习题A.doc
- Docker 实战:编写 Dockerfile
- python ==》 元组
- apache禁止訪问某些文件或文件夹的方法
- java进行图片和字符串的互相转换
- 从入门到入土:基于C语言采用UDP协议实现远程控制|详细说明|利用流套接字实现一个简单的远程控制系统|代码展示
- 《树莓派Python编程入门与实战(第2版)》——1.9 QA
- 使用iToolab UnlockGo for Mac如何绕过屏幕时间密码
- 托管代码 非托管代码
- 前端开发试用期工作总结
- 模拟器:思科 配置静态路由+下一跳如何配置,使三个路由器相互通信
- Redis使用Geo存储经纬度
- 期刊投稿状态_期刊投稿后的7种状态,如何应对
- 2018CSTC web2 writeup
- 「macOS无法验证此APP不包含恶意软件」的处理方式
- 深入分析事物以及什么是分布式事物?
- 【RC延迟电路与快速泄放电路】 multisim 14.0仿真 RC延迟电路与快速泄放电路
- Java 11,Spring Boot和JavaScript中的i18n