nn.Sequential

https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html?highlight=nn%20sequential#torch.nn.Sequential

nn.Sequential定义的网络中各层会按照定义的顺序进行级联,因此需要保证各层的输入和输出之间要衔接。并且nn.Sequential实现了farward()方法。

example:

  1. # Using Sequential to create a small model. When `model` is run,
  2. # input will first be passed to `Conv2d(1,20,5)`. The output of
  3. # `Conv2d(1,20,5)` will be used as the input to the first
  4. # `ReLU`; the output of the first `ReLU` will become the input
  5. # for `Conv2d(20,64,5)`. Finally, the output of
  6. # `Conv2d(20,64,5)` will be used as input to the second `ReLU`
  7. model = nn.Sequential(
  8. nn.Conv2d(1,20,5),
  9. nn.ReLU(),
  10. nn.Conv2d(20,64,5),
  11. nn.ReLU()
  12. )
  13. # Using Sequential with OrderedDict. This is functionally the
  14. # same as the above code
  15. model = nn.Sequential(OrderedDict([
  16. ('conv1', nn.Conv2d(1,20,5)),
  17. ('relu1', nn.ReLU()),
  18. ('conv2', nn.Conv2d(20,64,5)),
  19. ('relu2', nn.ReLU())
  20. ]))

What’s the difference between a Sequential and a torch.nn.ModuleList ? A ModuleList is exactly what it sounds like a list for storing Module s! On the other hand, the layers in a Sequential are connected in a cascading way.

ModuleList是存储模块s的列表

nn.ModuleList

nn.ModuleList 并没有定义一个网络,它只是将不同的模块储存在一起,这些模块之间并没有什么先后顺序可言

  1. class net3(nn.Module):
  2. def __init__(self):
  3. super(net3, self).__init__()
  4. self.linears = nn.ModuleList([nn.Linear(10,20), nn.Linear(20,30), nn.Linear(5,10)])
  5. def forward(self, x):
  6. x = self.linears[2](x)
  7. x = self.linears[0](x)
  8. x = self.linears[1](x)
  9. return x
  10. net = net3()
  11. print(net)
  12. # net3(
  13. # (linears): ModuleList(
  14. # (0): Linear(in_features=10, out_features=20, bias=True)
  15. # (1): Linear(in_features=20, out_features=30, bias=True)
  16. # (2): Linear(in_features=5, out_features=10, bias=True)
  17. # )
  18. # )
  19. input = torch.randn(32, 5)
  20. print(net(input).shape)
  21. # torch.Size([32, 30])

https://blog.csdn.net/m0_51004308/article/details/118034232?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-1-118034232.pc_agg_new_rank&utm_term=nn.sequential&spm=1000.2123.3001.4430