PyTorch中的Sequential、ModuleList和ModuleDict用法总结

1. 区别与联系

首先来一张图,总体概括一下它们的区别:
在这里插入图片描述

  • 区别
  1. nn.Sequential内部实现了forward函数,因此可以不用写forward函数。而nn.ModuleListnn.ModuleDict则没有实现内部forward函数。
  2. nn.Sequential需要严格按照顺序执行,而其它两个模块则可以任意调用。

下面分别进行介绍。

1.1 nn.Sequential

  • nn.Sequential里面的模块按照顺序进行排列的,所以必须确保前一个模块的输出大小和下一个模块的输入大小是一致的。
  • nn.Sequential中可以使用OrderedDict来指定每个module的名字。

1.2 nn.ModuleList

  • nn.ModuleList里面储存了不同 module,并自动将每个 moduleparameters 添加到网络之中的容器(注册),里面的module是按照List的形式顺序存储的,但是在forward中调用的时候可以随意组合。
  • 可以任意将 nn.Module 的子类 (比如 nn.Conv2d, nn.Linear 之类的) 加到这个 list 里面,方法和 Python 自带的 list 一样,也就是说它可以使用 extend,append 等操作。

1.3 nn.ModuleDict

  • ModuleDict可以像常规Python字典一样索引,同样自动将每个 moduleparameters 添加到网络之中的容器(注册)。
  • 同样的它可以使用OrderedDict、dict或者ModuleDict对它进行update,也就是追加。

2. nn.sequential

这里举两个例子来说明nn.sequential,一个是直接通过nn.Sequential添加子模块,另一个方法是使用OrderedDict来指定每个模块的名字。

下面两种方法可以达到同样的效果。

import torch.nn as nn

model = nn.Sequential(
          nn.Conv2d(1,20,5),
          nn.ReLU(),
          nn.Conv2d(20,64,5),
          nn.ReLU()
        )
print(model)
print('='*50)
from collections import OrderedDict
# Using Sequential with OrderedDict. This is functionally the
# same as the above code
model = nn.Sequential(OrderedDict([
          ('conv1', nn.Conv2d(1,20,5)),
          ('relu1', nn.ReLU()),
          ('conv2', nn.Conv2d(20,64,5)),
          ('relu2', nn.ReLU())
        ]))

print(model)

输出:

Sequential(
  (0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (1): ReLU()
  (2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
  (3): ReLU()
)
==================================================
Sequential(
  (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (relu1): ReLU()
  (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
  (relu2): ReLU()
)

3. nn.ModuleList

一些常用方法:

  • append():在ModuleList后面添加网络层
  • extend():拼接两个ModuleList
  • insert():指定ModuleList中位置插入网络层
import torch.nn as nn
class MyNet(nn.Module):
    def __init__(self):
        super(MyModule, self).__init__()
        self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(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

myNet = MyNet()
print(myNet)

输出:

MyModule(
  (linears): ModuleList(
    (0): Linear(in_features=10, out_features=10, bias=True)
    (1): Linear(in_features=10, out_features=10, bias=True)
    (2): Linear(in_features=10, out_features=10, bias=True)
    (3): Linear(in_features=10, out_features=10, bias=True)
    (4): Linear(in_features=10, out_features=10, bias=True)
    (5): Linear(in_features=10, out_features=10, bias=True)
    (6): Linear(in_features=10, out_features=10, bias=True)
    (7): Linear(in_features=10, out_features=10, bias=True)
    (8): Linear(in_features=10, out_features=10, bias=True)
    (9): Linear(in_features=10, out_features=10, bias=True)
  )
)

4. nn.ModuleDict

一些常用方法:

  • clear(): 清空ModuleDict
  • items(): 返回可迭代的键值对(key-value pairs)
  • keys(): 返回字典的键(key)
  • values(): 返回字典的值(value)
  • pop(): 返回一对键值,并从字典中删除
  • update():添加dict、OrderedDict或者ModuleDict结构。
import torch.nn as nn
class MyNet(nn.Module):
    def __init__(self):
        super(MyModule, self).__init__()
        self.choices = nn.ModuleDict({
                'conv': nn.Conv2d(10, 10, 3),
                'pool': nn.MaxPool2d(3)
        })
        self.activations = nn.ModuleDict([
                ['lrelu', nn.LeakyReLU()],
                ['prelu', nn.PReLU()]
        ])

    def forward(self, x, choice, act):
        # x = self.choices[choice](x)
        # x = self.activations[act](x)
        return x
    
my_net = MyNet()
print(my_net)

输出如下,forward不管怎么设计,都不能很好的打印出my_net网络。

MyModule(
  (choices): ModuleDict(
    (conv): Conv2d(10, 10, kernel_size=(3, 3), stride=(1, 1))
    (pool): MaxPool2d(kernel_size=3, stride=3, padding=0, dilation=1, ceil_mode=False)
  )
  (activations): ModuleDict(
    (lrelu): LeakyReLU(negative_slope=0.01)
    (prelu): PReLU(num_parameters=1)
  )
)

5. 自行设计网络

(1)使用python的list添加(不可行)

import torch.nn as nn
class MyNet(nn.Module):
    def __init__(self):
        super(net_modlist, self).__init__()
        self.modlist = [
                       nn.Conv2d(1, 20, 5),
                       nn.ReLU(),
                        nn.Conv2d(20, 64, 5),
                        nn.ReLU()
                        ]

    def forward(self, x):
        for m in self.modlist:
            x = m(x)
        return x

my_net = MyNet()
print(my_net)

print('====================================')
for param in my_net.parameters():
    print(type(param.data), param.size())

输出如下,可以看到使用 Python 中的list形式添加卷积层和它们的 parameters 并没有自动注册到我们的网络中。

net_modlist()
====================================

(2)手动添加(可行)

也可以手工挨个的添加网络。

import torch.nn as nn
class MyNet(nn.Module):
    def __init__(self):
        super(net_modlist, self).__init__()
        self.conv2d_1 = nn.Conv2d(1, 20, 5)
        self.ReLU = nn.ReLU()
        self.conv2d_2 = nn.Conv2d(20, 64, 5),

    def forward(self, x):
        x = self.conv2d_1(x)
        x = self.ReLU(x)
        x = self.conv2d_2(x)
        x = self.ReLU(x)
        return x

my_net = MyNet()
print(my_net)

print('====================================')
for param in my_net.parameters():
    print(type(param.data), param.size())

输出如下,可以看到手动设置也可以把添加的卷积层和它们的 parameters 注册到我们的网络中。

net_modlist(
  (conv2d_1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (ReLU): ReLU()
)
====================================
<class 'torch.Tensor'> torch.Size([20, 1, 5, 5])
<class 'torch.Tensor'> torch.Size([20])

参考:
nn.Sequential官网
nn.ModuleList官网
nn.ModuleDict官网

  • 19
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
pytorch 是一个高效的深度学习框架,其nn.modulelist 和 nn.sequential是常用的模块。这两种模块都可以用于创建深度学习网络,并且能够实现自动求导。nn.sequential 是一个有序的容器,其每个模块按照传入的顺序依次进行计算。nn.modulelist 是一个无序的容器,其每个模块都可以以列表的形式存储,且没有特定的计算顺序。 nn.sequential 模块的优点是简单易用,并且可以通过一行代码构建和训练网络。例如,要创建一个简单的两层全连接神经网络,可以如下代码实现: ``` model = nn.Sequential(nn.Linear(784, 64), nn.ReLU(), nn.Linear(64, 10), nn.Softmax(dim=1)) ``` 这会定义一个两个全连接层网络以及 ReLU 和softmax 激活函数,输入大小为 784(MNIST 图像大小) ,输出大小为 10(10 个数字)。 nn.modulelist 是一个更加灵活的容器,可以在其添加任意的子模块。要使用 nn.modulelist,需要先创建一个空的 nn.modulelist,然后手动向其添加子模块。例如,可以这样创建一个相同的两层全连接网络: ``` model = nn.ModuleList([ nn.Linear(784, 64), nn.ReLU(), nn.Linear(64, 10), nn.Softmax(dim=1) ]) ``` 需要注意的是,nn.modulelist 的子模块顺序可能会影响计算结果,因为没有特定的训练顺序。因此,在使用 nn.modulelist 时应该尽量保证顺序的准确性。 综上所述,nn.sequential 和 nn.modulelist 都是常用的容器,用于组织神经网络的子模块,它们在不同场景下具有各自的优势。在简单的前向计算,nn.sequential 更加容易使用;在需要更好的灵活性时,nn.modulelist 可以更好地实现目标。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

非晚非晚

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

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

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

打赏作者

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

抵扣说明:

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

余额充值