PyTorch基础模块nn.Sequential

nn.Sequential是PyTorch中用于构建神经网络序列模型的模块,它可以按照添加的顺序执行前向传播。文章介绍了nn.Sequential的不同实现方式,包括通过直接传入层、OrderedDict以及使用add_module方法。nn.Sequential使得模型构建更加简洁,同时也支持通过索引访问层。文章还对比了nn.Sequential与nn.ModuleList的区别,并展示了源码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PyTorch基础模块nn.Sequential

nn.Sequential()介绍

一个序列容器,用于搭建神经网络的模块按照被传入构造器的顺序添加到nn.Sequential()容器中。除此之外,一个包含神经网络模块的OrderedDict也可以被传入nn.Sequential()容器中。利用nn.Sequential()搭建好模型架构,模型前向传播时调用forward()方法,模型接收的输入首先被传入nn.Sequential()包含的第一个网络模块中。然后,第一个网络模块的输出传入第二个网络模块作为输入,按照顺序依次计算并传播,直到nn.Sequential()里的最后一个模块输出结果。

类似于keras中的序贯模型,当一个模型较简单的时候,我们可以使用torch.nn.Sequential类来实现简单的顺序连接模型。这个Sequential模型也是继承自Module类

Sequential的实现在container.py模块中。

Sequential似乎是一个容器,的确,它确确实实是可以作为一个容器包装各层。先简单看一下它的定

class Sequential(Module): # 继承Module
    def __init__(self, *args):  # 重写了构造函数
    def _get_item_by_idx(self, iterator, idx):
    def __getitem__(self, idx):
    def __setitem__(self, idx, module):
    def __delitem__(self, idx):
    def __len__(self):
    def __dir__(self):
    def forward(self, input):  # 重写关键方法forward

container.py里面还有那些“容器”存在

class Container(Module):
class Sequential(Module)class ModuleList(Module)class ModuleDict(Module):
class ParameterList(Module):
class ParameterDict(Module):

Sequential类不同的实现

最简单的序贯模型

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(model[2]) # 通过索引获取第几个层
'''运行结果为:
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()
)
Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
'''

在每一个包装块里面,各个层是没有名称的,默认按照0、1、2、3、4来排名。

给每一个层添加名称(有序字典实现)

import torch.nn as nn
from collections import OrderedDict
model = nn.Sequential(OrderedDict([
                  ('conv1', nn.Conv2d(1,20,5)),
                  ('relu1', nn.ReLU()),
                  ('conv2', nn.Conv2d(20,64,5)),
                  ('relu2', nn.ReLU())
                ]))
 
print(model)
print(model[2]) # 通过索引获取第几个层
'''运行结果为:
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()
)
Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
'''

很多人认为python中的字典是无序的,因为它是按照hash来存储的,但是python中有个模块collections(英文,收集、集合),里面自带了一个子类OrderedDict,实现了对字典对象中元素的排序
从上面的结果中可以看出,这个时候每一个层都有了自己的名称,但是此时需要注意,我并不能够通过名称直接获取层,依然只能通过索引index,即model[2] 是正确的,model[“conv2”] 是错误的,这其实是由它的定义实现的,看上面的Sequenrial定义可知,只支持index访问。

Sequential的第三种实现

import torch.nn as nn
from collections import OrderedDict
model = nn.Sequential()
model.add_module("conv1",nn.Conv2d(1,20,5))
model.add_module('relu1', nn.ReLU())
model.add_module('conv2', nn.Conv2d(20,64,5))
model.add_module('relu2', nn.ReLU())
 
print(model)
print(model[2]) # 通过索引获取第几个层

add_module()这个方法是定义在它的父类Module里面的,Sequential继承了。

nn.Sequential()的本质作用

与一层一层的单独调用模块组成序列相比,nn.Sequential() 可以允许将整个容器视为单个模块(即相当于把多个模块封装成一个模块),forward()方法接收输入之后,nn.Sequential()按照内部模块的顺序自动依次计算并输出结果。

这就意味着我们可以利用nn.Sequential() 自定义自己的网络层。

from torch import nn


class net(nn.Module):
    def __init__(self, in_channel, out_channel):
        super(net, self).__init__()
        self.layer1 = nn.Sequential(nn.Conv2d(in_channel, in_channel / 4, kernel_size=1),
                                    nn.BatchNorm2d(in_channel / 4),
                                    nn.ReLU())
        self.layer2 = nn.Sequential(nn.Conv2d(in_channel / 4, in_channel / 4),
                                    nn.BatchNorm2d(in_channel / 4),
                                    nn.ReLU())
        self.layer3 = nn.Sequential(nn.Conv2d(in_channel / 4, out_channel, kernel_size=1),
                                    nn.BatchNorm2d(out_channel),
                                    nn.ReLU())
        
    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        
        return x

上边的代码,我们通过nn.Sequential()将卷积层,BN层和激活函数层封装在一个层中,输入x经过卷积、BN和ReLU后直接输出激活函数作用之后的结果。

nn.Sequential()和nn.ModuleList的区别在于:nn.ModuleList只是一个储存网络模块的list,其中的网络模块之间没有连接关系和顺序关系。而nn.Sequential()内的网络模块之间是按照添加的顺序级联的。

nn.Sequential()源码

def __init__(self, *args):
        super(Sequential, self).__init__()
        if len(args) == 1 and isinstance(args[0], OrderedDict):
            for key, module in args[0].items():
                self.add_module(key, module)
        else:
            for idx, module in enumerate(args):
                self.add_module(str(idx), module)
def forward(self, input):
        for module in self:
            input = module(input)
        return input

nn.Sequential()首先判断接收的参数是否为OrderedDict类型,如果是的话,分别取出OrderedDict内每个元素的key(自定义的网络模块名)和value(网络模块),然后将其通过add_module方法添加到nn.Sequrntial()中。
调用forward()方法进行前向传播时,for循环按照顺序遍历nn.Sequential()中存储的网络模块,并以此计算输出结果,并返回最终的计算结果。

nn.Sequential(*layers)

layers为list类型。*作用在实参上,代表的是将输入迭代器拆成一个个元素。

从nn.Sequential的定义来看,输入要么是orderdict,要么是一系列的模型。遇到list,属于else类,由于args转化为元组形式,list类型相当于1个元组元素,因此只能循环一次,加入的模型层却不止一个。必须用号进行转化,否则会报错 TypeError: list is not a Module subclass。

### 使用 `nn.Sequential` 构建神经网络 在 PyTorch 中,`nn.Sequential` 是一种便捷的方式用于创建按顺序执行的一系列层组成的神经网络。通过这种方式可以简化代码结构并提高可读性。 #### 导入必要的库 为了使用 `nn.Sequential` 需要先导入 PyTorch 及其子模块: ```python import torch import torch.nn as nn ``` #### 定义简单的前馈神经网络 下面展示了一个具体的例子来说明如何利用 `nn.Sequential` 创建一个多层感知器(MLP),该模型接收大小为 28×28 的输入图像,并最终输出分类概率分布[^1]: ```python model = nn.Sequential( nn.Linear(28 * 28, 32), nn.ReLU(), nn.Linear(32, 10), nn.Softmax(dim=1) ) print("Model structure:\n", model) ``` 这段代码定义了一种线性的变换序列,其中包含了两个全连接层以及激活函数 ReLU 和 Softmax 函数。第一个线性层将输入维度从 \(784\) 映射到了 \(32\) 维;第二个线性层进一步映射至 \(10\) 类别的预测向量上,并应用了 softmax 来获得类别间的相对可能性得分。 #### 处理输入张量形状调整 当处理实际数据集时,通常会遇到不同尺寸的数据样本。对于上述案例中的 MNIST 数据集而言,每幅图片都是灰度图形式存储的二维数组 (28 × 28),但在传递给模型之前需要将其展平成一维向量以便于计算矩阵乘法操作。这可以通过调用 `.view()` 方法实现: ```python x_input = torch.randn(2, 28, 28, 1) # 模拟两批随机数作为测试输入 reshaped_x = x_input.view(x_input.size()[0], -1) # 将最后三个轴合并为单个特征向量 print("Reshaped input shape:", reshaped_x.shape) ``` 这里 `-1` 表示自动推断剩余维度的数量以保持原始批量大小不变。因此如果原批次中有两张图片,则经过重塑后的张量应具有 `[batch_size, flattened_features]` 形状,即 `(2, 784)`。 #### 执行前向传播过程 一旦完成了对输入数据预处理工作之后就可以直接调用模型对象来进行推理运算: ```python with torch.no_grad(): # 关闭梯度跟踪模式因为这是评估阶段而非训练期间 y_pred = model.forward(reshaped_x) print("Predicted probabilities:\n", y_pred) ``` 注意,在此上下文中使用了 `torch.no_grad()` 上下文管理器关闭了反向传播机制,因为在仅做预测时不涉及参数更新所以没有必要开启它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值