代码:
import torch
import torch.nn as nn
input_nc = 3
hide1_nc = 6
hide2_nc = 6
output_nc = 3
class 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 x
model = 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