代码:

import torch
import torch.nn as nninput_nc = 3
hide1_nc = 6
hide2_nc = 6
output_nc = 3class Net(nn.Module):def __init__(self, input_nc, hide1_nc, hide2_nc, output_nc):super().__init__()acti = nn.ReLU(inplace=True)layer_add = nn.Sequential(nn.Linear(hide1_nc, hide2_nc),nn.Linear(hide1_nc, hide2_nc))self.layer1 = nn.Sequential(nn.Linear(input_nc, hide1_nc),acti,layer_add,acti)self.layer2 = nn.Sequential(nn.Linear(hide1_nc, hide2_nc),nn.ReLU(inplace=True))self.layer3 = nn.Linear(hide2_nc, output_nc)print('self.children()测试')for i, j in enumerate(self.children()):print("第{}次循环\n{}".format(i, j))print('*' * 20)print('self.modules()测试')for i, j in enumerate(self.modules()):print("第{}次循环\n{}".format(i, j))print('*' * 20)def forward(self, x):x = self.layer1(x)x = self.layer2(x)x = self.layer3(x)return xmodel = Net(input_nc, hide1_nc, hide2_nc, output_nc)

网络结构:

运行结果:

self.children()测试
第0次循环
Sequential((0): Linear(in_features=3, out_features=6, bias=True)(1): ReLU(inplace)(2): Sequential((0): Linear(in_features=6, out_features=6, bias=True)(1): Linear(in_features=6, out_features=6, bias=True))(3): ReLU(inplace)
)
********************
第1次循环
Sequential((0): Linear(in_features=6, out_features=6, bias=True)(1): ReLU(inplace)
)
********************
第2次循环
Linear(in_features=6, out_features=3, bias=True)
********************
self.modules()测试
第0次循环
Net((layer1): Sequential((0): Linear(in_features=3, out_features=6, bias=True)(1): ReLU(inplace)(2): Sequential((0): Linear(in_features=6, out_features=6, bias=True)(1): Linear(in_features=6, out_features=6, bias=True))(3): ReLU(inplace))(layer2): Sequential((0): Linear(in_features=6, out_features=6, bias=True)(1): ReLU(inplace))(layer3): Linear(in_features=6, out_features=3, bias=True)
)
********************
第1次循环
Sequential((0): Linear(in_features=3, out_features=6, bias=True)(1): ReLU(inplace)(2): Sequential((0): Linear(in_features=6, out_features=6, bias=True)(1): Linear(in_features=6, out_features=6, bias=True))(3): ReLU(inplace)
)
********************
第2次循环
Linear(in_features=3, out_features=6, bias=True)
********************
第3次循环
ReLU(inplace)
********************
第4次循环
Sequential((0): Linear(in_features=6, out_features=6, bias=True)(1): Linear(in_features=6, out_features=6, bias=True)
)
********************
第5次循环
Linear(in_features=6, out_features=6, bias=True)
********************
第6次循环
Linear(in_features=6, out_features=6, bias=True)
********************
第7次循环
Sequential((0): Linear(in_features=6, out_features=6, bias=True)(1): ReLU(inplace)
)
********************
第8次循环
Linear(in_features=6, out_features=6, bias=True)
********************
第9次循环
ReLU(inplace)
********************
第10次循环
Linear(in_features=6, out_features=3, bias=True)
********************

由运行结果可知:self.children()只包括网络模块的第一代儿子模块;self.modules()包含网络模块的自己本身和所有后代模块。

  • self.children()存储网络结构的子层模块,即网络结构的第二列.
  • self.modules()采用深度优先遍历的方式,存储了net的所有模块.值得注意的是self.modules()运行结果的第7次循环并不是nn.ReLU, 因为layer1中的acti = nn.ReLU(inplace=True)是重复的模块,有单独的命名,是类nn.ReLU()的一个固定实例,对于这种重复模块self.modules()中只返回一次; 而从self.modules()运行结果的第5和6次循环可以看出, 认为这两个nn.Linear()不是重复的模块,估计是看成类nn.Linear()不同的实例了

参考博客:

https://blog.csdn.net/dss_dssssd/article/details/83958518

https://blog.csdn.net/LXX516/article/details/79016980

self.modules() 和 self.children()的区别相关推荐

  1. Pytorch中的model.modules()和model.children()的区别

    Pytorch中的model.modules()和model.children()的区别 背景:最近在做网络模型中可视化的过程中,需要将网络结构中的某一层的特征进行输出.所以就遇到了这个问题,小小记录 ...

  2. pytorch系列8 --self.modules() 和 self.children()的区别

    本文主要讲述: self.modue和self.children的区别与联系 说实话,我真的只想讲参数初始化方式,但总感觉在偏离的道路上越走越远... 在看一些pytorch文章讲述自定义参数初始化方 ...

  3. js表单验证处理和childNodes 和children 的区别

    一.对提交表单进行空值验证 html代码: 1 <form action="#"onsubmit="return validate_form(this);" ...

  4. Model.modules和Model.children

    Model.modules和Model.children 首先我们先定义一个网络结构: class Linear(nn.Module):def __init__(self):super().__ini ...

  5. Html DOM元素的childNodes和children的区别

    对于DOM元素,children是指DOM Object类型的子对象,不包括tag之间隐形存在的TextNode,而childNodes包括tag之间隐形存在的TextNode对象. 具体看一下针对c ...

  6. 深入了解parentNode,parentElement,childNodes,children的区别,一看就懂

    今天是准备面试的第七天,js是由ECMAScript.文档对象模型(DOM)和浏览器对象模型(BOM)组成,我们今天就了解下与DOM有关的常用接口parentNode,parentElement,ch ...

  7. model.modules()和model.children()以及model._modules.items()

    两者包含的其实是你所定义的网络中继承自nn.Module的模块,比如nn.Relu,nn.Conv2d等,并不是你整个网络的forward()结构,而且你自定义的函数模块也是没有的,实验如下: 另外, ...

  8. childNodes和children的区别

    <!DOCTYPE html> <html><head><meta charset="utf-8" /><title>c ...

  9. Js-parentNode、parentElement,childNodes、children 的区别

    ---------------------------------------------------------------------- parentNode.parentElement,chil ...

最新文章

  1. mybatis,主键返回指的是返回到传入的对象中
  2. WIN7系统共享访问方式总结
  3. 研究生 论文写作【要注意的30个禁忌(总体问题、选题方面、摘要方面、研究方法方面、讨论与结果、结论方面)】
  4. 漫画兔善搞2007-等待爱玛马士基号的垃圾
  5. 一些常用的linux命令(2)
  6. 蛮力法在求解凸包问题中的应用(JAVA)
  7. CCF 201612-2 工资计算 java 解题
  8. 关于J2EE项目中三层架构如何在开发中得到正确的实施
  9. Kuboard云原生管理工具
  10. pygame小游戏(接球小游戏)
  11. R-Sys.time计算程序运行时间
  12. openSUSE 安装 Torch
  13. padavan路由器server酱设备下线快速响应。
  14. 技嘉显卡 RGBFusion 不能调光解决方法
  15. WinRAR 5.5 破解方法 - 自己动手, 更放心
  16. 牛客小白月赛2 I.艺
  17. 关于.net的一则笑话(无奈一笑)
  18. 云和恩墨校园猎手招募令
  19. word表格转换为图片
  20. yolov5的anchor详解

热门文章

  1. python入门先学什么-学习python需要什么基础
  2. 爬虫python能做什么-Python除了能做爬虫之外还能做什么?
  3. python零基础能学吗-零基础小白多久能学会python
  4. 语音情感识别--RNN
  5. 贪吃蛇大战 java小游戏百度云源码
  6. LeetCode 72 编辑距离
  7. 利用nginx-rtmp搭建视频点播、直播、HLS服务器
  8. NVIDIA新旗舰GeForce GTX 780深度评测
  9. eclipse for php开发环境,eclipse for php 开发环境配置
  10. display none 隐藏后怎么显示_第12天:打破常规之 display