pytorch:nn.ModuleList和nn.Sequential、list的用法以及区别

文章目录

在构建网络的时候,pytorch有一些基础概念很重要,比如nn.Module,nn.ModuleList,nn.Sequential,这些类我们称为为容器(containers),可参考containers。本文中我们主要学习nn.ModuleList和nn.Sequential,并判断在什么时候用哪一个比较合适。

1. nn.ModuleList和nn.Sequential简介

nn.ModuleList

nn.ModuleList,它是一个存储不同module,并自动将每个module的parameters添加到网络之中的容器。你可以把任意nn.Module的子类(如nn.Conv2d,nn.Linear等)加到这个list里面,方法和python自带的list一样,无非是extend,append等操作,但不同于一般的list,加入到nn.ModuleList里面的module是会自动注册到整个网络上的,同时module的parameters也会自动添加到整个网络中。若使用python的list,则会出问题。


 
 
  1. class net 1(nn.Module):
  2. def __init__( self):
  3. super(net 1, self).__init__()
  4. self.linears = nn.ModuleList([nn.Linear( 10,10) for i in range( 2)])
  5. def forward( self, x):
  6. for m in self.linears:
  7. x = m(x)
  8. return x
  9. net = net 1()
  10. print(net)
  11. # net 1(
  12. # (modules): ModuleList(
  13. # ( 0): Linear( in_features = 10, out_features = 10, bias = True)
  14. # ( 1): Linear( in_features = 10, out_features = 10, bias = True)
  15. # )
  16. # )
  17. for param in net.parameters():
  18. print( type(param. data), param. size())
  19. # < class 'torch.Tensor' > torch. Size([ 10, 10])
  20. # < class 'torch.Tensor' > torch. Size([ 10])
  21. # < class 'torch.Tensor' > torch. Size([ 10, 10])
  22. # < class 'torch.Tensor' > torch. Size([ 10])

可以看到,这个网络权重(weights)和偏置(bias)都在这个网络之内。而对于使用python自带list的例子如下:
 


 
 
  1. class net 2(nn.Module):
  2. def __init__( self):
  3. super(net 2, self).__init__()
  4. self.linears = [nn.Linear( 10,10) for i in range( 2)]
  5. def forward( self, x):
  6. for m in self.linears:
  7. x = m(x)
  8. return x
  9. net = net 2()
  10. print(net)
  11. # net 2()
  12. print(list(net.parameters()))
  13. # []

显然,使用python的list添加的卷积层和它们的parameters并没有自动注册到我们的网络中。当然,我们还是可以使用forward来计算输出结果。但是如果用其实例化的网络进行训练的时候,因为这些层的parameters不在整个网络之中,所以其网络参数也不会被更新,也就是无法训练。

 

但是,我们需要注意到,nn.ModuleList并没有定义一个网络,它只是将不同的模块储存在一起,这些模块之间并没有什么先后顺序可言,比如:


 
 
  1. class net 3(nn.Module):
  2. def __init__( self):
  3. super(net 3, 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 = net 3()
  11. print(net)
  12. # net 3(
  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])

根据net3的结果,我们可以看出ModuleList里面的顺序并不能决定什么,网络的执行顺序是根据forward函数来决定的。但是一般设置ModuleList中的顺序和forward中保持一致,增强代码的可读性。

我们再来考虑另一种情况,既然ModuleList可以根据序号来调用,那么一个模型可以在forward函数中被调用多次。但需要注意的是,被调用多次的模块,是使用同一组parameters的,也就是它们是参数共享的。


 
 
  1. class net 4(nn.Module):
  2. def __init__( self):
  3. super(net 4, self).__init__()
  4. self.linears = nn.ModuleList([nn.Linear( 5, 10), nn.Linear( 10, 10)])
  5. def forward( self, x):
  6. x = self.linears[ 0](x)
  7. x = self.linears[ 1](x)
  8. x = self.linears[ 1](x)
  9. return x
  10. net = net 4()
  11. print(net)
  12. # net 4(
  13. # (linears): ModuleList(
  14. # ( 0): Linear( in_features = 5, out_features = 10, bias = True)
  15. # ( 1): Linear( in_features = 10, out_features = 10, bias = True)
  16. # )
  17. # )
  18. for name, param in net.named_parameters():
  19. print(name, param. size())
  20. # linears. 0.weight torch. Size([ 10, 5])
  21. # linears. 0.bias torch. Size([ 10])
  22. # linears. 1.weight torch. Size([ 10, 10])
  23. # linears. 1.bias torch. Size([ 10])

 

nn.Sequential

不同于nn.ModuleList,nn.Sequential已经实现了内部的forward函数,而且里面的模块必须是按照顺序进行排列的,所以我们必须确保前一个模块的输出大小和下一个模块的输入大小是一致的。


 
 
  1. class net 5(nn.Module):
  2. def __init__( self):
  3. super(net 5, self).__init__()
  4. self. block = nn. Sequential(nn.Conv 2d( 1,20,5),
  5. nn.ReLU(),
  6. nn.Conv 2d( 20,64,5),
  7. nn.ReLU())
  8. def forward( self, x):
  9. x = self. block(x)
  10. return x
  11. net = net 5()
  12. print(net)
  13. # net 5(
  14. # ( block): Sequential(
  15. # ( 0): Conv 2d( 1, 20, kernel_ size =( 5, 5), stride =( 1, 1))
  16. # ( 1): ReLU()
  17. # ( 2): Conv 2d( 20, 64, kernel_ size =( 5, 5), stride =( 1, 1))
  18. # ( 3): ReLU()
  19. # )
  20. # )

下面给出了两个nn.Sequential初始化的例子,在第二个初始化中我们用到了OrderedDict来指定每个module的名字


 
 
  1. # Example of using Sequential
  2. model 1 = nn. Sequential(
  3. nn.Conv 2d( 1,20,5),
  4. nn.ReLU(),
  5. nn.Conv 2d( 20,64,5),
  6. nn.ReLU()
  7. )
  8. print(model 1)
  9. # Sequential(
  10. # ( 0): Conv 2d( 1, 20, kernel_ size =( 5, 5), stride =( 1, 1))
  11. # ( 1): ReLU()
  12. # ( 2): Conv 2d( 20, 64, kernel_ size =( 5, 5), stride =( 1, 1))
  13. # ( 3): ReLU()
  14. # )
  15. # Example of using Sequential with OrderedDict
  16. import collections
  17. model 2 = nn. Sequential(collections.OrderedDict([
  18. ( 'conv1', nn.Conv 2d( 1,20,5)),
  19. ( 'relu1', nn.ReLU()),
  20. ( 'conv2', nn.Conv 2d( 20,64,5)),
  21. ( 'relu2', nn.ReLU())
  22. ]))
  23. print(model 2)
  24. # Sequential(
  25. # (conv 1): Conv 2d( 1, 20, kernel_ size =( 5, 5), stride =( 1, 1))
  26. # (relu 1): ReLU()
  27. # (conv 2): Conv 2d( 20, 64, kernel_ size =( 5, 5), stride =( 1, 1))
  28. # (relu 2): ReLU()
  29. # )

有同学可能发现了,诶,你这个 model1 和 从类 net5 实例化来的 net 有什么区别吗?是没有的。这两个网络是相同的,因为 nn.Sequential 就是一个 nn.Module 的子类,也就是 nn.Module 所有的方法 (method) 它都有。并且直接使用 nn.Sequential 不用写 forward 函数,因为它内部已经帮你写好了。

这时候有同学该说了,既然 nn.Sequential 这么好,我以后都直接用它了。如果你确定 nn.Sequential 里面的顺序是你想要的,而且不需要再添加一些其他处理的函数 (比如 nn.functional 里面的函数,nn 与 nn.functional 有什么区别? ),那么完全可以直接用 nn.Sequential。这么做的代价就是失去了部分灵活性,毕竟不能自己去定制 forward 函数里面的内容了。

一般情况下 nn.Sequential 的用法是来组成卷积块 (block),然后像拼积木一样把不同的 block 拼成整个网络,让代码更简洁,更加结构化。

 

2.nn.Sequential与nn.ModuleList的区别

不同点1:nn.Sequential内部实现了forward函数,因此可以不用写forward函数,而nn.ModuleList则没有实现内部forward函数。

不同点2:nn.Sequential可以使用OrderedDict对每层进行命名。

不同点3:nn.Sequential里面的模块按照顺序进行排列的,所以必须确保前一个模块的输出大小和下一个模块的输入大小是一致的。而nn.ModuleList 并没有定义一个网络,它只是将不同的模块储存在一起,这些模块之间并没有什么先后顺序可言。

不同点4:有的时候网络中有很多相似或者重复的层,我们一般会考虑用 for 循环来创建它们,而不是一行一行地写,比如:

layers = [nn.Linear(10, 10) for i in range(5)]
 
 

那么这里我们使用ModuleList:


 
 
  1. class net 4(nn.Module):
  2. def __init__( self):
  3. super(net 4, self).__init__()
  4. layers = [nn.Linear( 10, 10) for i in range( 5)]
  5. self.linears = nn.ModuleList(layers)
  6. def forward( self, x):
  7. for layer in self.linears:
  8. x = layer(x)
  9. return x
  10. net = net 4()
  11. print(net)
  12. # net 4(
  13. # (linears): ModuleList(
  14. # ( 0): Linear( in_features = 10, out_features = 10, bias = True)
  15. # ( 1): Linear( in_features = 10, out_features = 10, bias = True)
  16. # ( 2): Linear( in_features = 10, out_features = 10, bias = True)
  17. # )
  18. # )

这个是比较一般的方法,但如果不想这么麻烦,我们也可以用 Sequential 来实现,如 net7 所示!注意 * 这个操作符,它可以把一个 list 拆开成一个个独立的元素。但是,请注意这个 list 里面的模块必须是按照想要的顺序来进行排列的。在 场景一 中,我个人觉得使用 net7 这种方法比较方便和整洁。


 
 
  1. class net 7(nn.Module):
  2. def __init__( self):
  3. super(net 7, self).__init__()
  4. self.linear_list = [nn.Linear( 10, 10) for i in range( 3)]
  5. self.linears = nn. Sequential( * self.linears_list)
  6. def forward( self, x):
  7. self.x = self.linears(x)
  8. return x
  9. net = net 7()
  10. print(net)
  11. # net 7(
  12. # (linears): Sequential(
  13. # ( 0): Linear( in_features = 10, out_features = 10, bias = True)
  14. # ( 1): Linear( in_features = 10, out_features = 10, bias = True)
  15. # ( 2): Linear( in_features = 10, out_features = 10, bias = True)
  16. # )
  17. # )

下面我们考虑 场景二,当我们需要之前层的信息的时候,比如 ResNets 中的 shortcut 结构,或者是像 FCN 中用到的 skip architecture 之类的,当前层的结果需要和之前层中的结果进行融合,一般使用 ModuleList 比较方便,一个非常简单的例子如下:


 
 
  1. class net 8(nn.Module):
  2. def __init__( self):
  3. super(net 8, self).__init__()
  4. self.linears = nn.ModuleList([nn.Linear( 10, 20), nn.Linear( 20, 30), nn.Linear( 30, 50)])
  5. self.trace = []
  6. def forward( self, x):
  7. for layer in self.linears:
  8. x = layer(x)
  9. self.trace.append(x)
  10. return x
  11. net = net 8()
  12. input = torch.randn( 32, 10) # input batch size: 32
  13. output = net( input)
  14. for each in net.trace:
  15. print(each.shape)
  16. # torch. Size([ 32, 20])
  17. # torch. Size([ 32, 30])
  18. # torch. Size([ 32, 50])

我们使用了一个 trace 的列表来储存网络每层的输出结果,这样如果以后的层要用的话,就可以很方便地调用了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
net = nn.Sequential(nn.Linear(2,2))创建了一个包含一个线性层的Sequential模型。这个线性层的输入维度是2,输出维度也是2。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [pytorch人工神经网络基础:线性回归神经网络(nn.Module+nn.Sequential+nn.Linear+nn.init+optim.SGD)](https://blog.csdn.net/hustlei/article/details/123598993)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [DCGAN生成艺术图片迷宫游戏图片.zip](https://download.csdn.net/download/qq_30803353/87780745)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [pytroch用自定义的tensor初始化nn.sequential中linear或者conv层的一种简单方法。](https://blog.csdn.net/york1996/article/details/84234352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Quelquefois

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值