参考 详解PyTorch中的ModuleList和Sequential的区别 - 云+社区 - 腾讯云
目录
CLASS torch.nn.ModuleList(modules=None)
二、nn.Sequential与nn.ModuleList简介
三、nn.Sequential与nn.ModuleList的区别
一、官方文档
首先看官方文档的解释,仅列出了容器(Containers)中几个比较常用的CLASS。
CLASS torch.nn.Module:
- Base class for all neural network modules.
- Your models should also subclass this class.
A sequential container. Modules will be added to it in the order they are passed in the constructor. Alternatively, an ordered dict of modules can also be passed in.
# Example of using Sequential
model = nn.Sequential(
nn.Conv2d(1, 20, 5),
nn.ReLU(),
nn.Conv2d(20, 64, 5),
nn.ReLU()
)
# Example of using Sequential with OrderedDict
model = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1, 20, 5)),
('ReLU1', nn.ReLU()),
('conv2', nn.Conv2d(20, 64, 5)),
('ReLU2', nn.ReLU())
]))
CLASS torch.nn.ModuleList(modules=None)
Holds submodules in a list.
ModuleList
can be indexed like a regular Python list, but modules it contains are properly registered, and will be visible by all Module
methods.
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.linears = nn.ModuleList([nn.linear for i in range(10)])
# ModuleList can act as an iterable, or be indexed using ints
def forward(self, x):
for i, l in enumerate(self.linears):
x = self.linears[i // 2](x) + l(x)
return x
二、nn.Sequential与nn.ModuleList简介
1、nn.Sequential
#首先导入torch相关包
import torch
import torch.nn as nn
import torch.nn.functional as F
class net_seq(nn.Module):
def __init__(self):
super(net2, self).__init__()
self.seq = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),