nn.ModuleList
-
Class torch.nn.ModuleList(modules=None)
简单的说,就是把子模块存储在list中.它类似于list, 既可以 append 操作,也可以做 insert 操作,也可以 extend 操作. 但是由于把layers存入Modulelist中后只是完成了存储作用,所以不能直接在forward中直接运行,需要通过索引调出相应的submodule.class MyModule(nn.Module): def __init__(self): super(MyModule, self).__init__() self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)]) # extend操作 self.linears.extend([nn.Linear(10, 10),nn.Linear(10, 10) ]) # append操作 self.linears.append(nn.Linear(10, 10)) def forward(self, x): # ModuleList can act as an iterable, or be indexed using ints for i, l in enumerate(self.linears): x = self.linears[i // 2](x) + l(x) return x
nn.Sequential
-
Class torch.nn.Sequential(*args)
顺序容器.模块将按照顺序存进sequential中,相当于一个包装起来的子模块集,可以在forward中直接运行.class MyModule(nn.Module): def __init__(self): super(MyModule, self).__init__() self.layers = nn.Sequential( nn.Conv2d(1,20,5), nn.ReLU(), nn.Conv2d(20,64,5), nn.ReLU() ) def forward(self, x): # ModuleList can act as an iterable, or be indexed using ints x = self.layers return x