参考https://zhuanlan.zhihu.com/p/75206669
nn.Sequential的参数为一个个模块,它负责将模块连接
seq = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),
nn.Conv2d(20,64,5),
nn.ReLU()
)
使用时直接seq(feature)
nn.ModuleList得参数和上面类似,多了个list的外框
self.modlist = nn.ModuleList([
nn.Conv2d(1, 20, 5),
nn.ReLU(),
nn.Conv2d(20, 64, 5),
nn.ReLU()
])
同时还支持python list的各种方法,比如append
但是并没有实现forward,因此需要自己手写,遍历这个list,然后分别调用
def forward(self, x):
for m in self.modlist:
x = m(x)
这样的好处是可以在forward时再加操作,增加了灵活性