现在位置: 首页 > PyTorch 教程 > 正文

PyTorch torch.nn.Sequential 函数

PyTorch torch.nn 参考手册 PyTorch torch.nn 参考手册


torch.nn.Sequential 是 PyTorch 中用于快速构建顺序模型的容器。

它按照声明的顺序依次执行各个模块,适合构建简单的线性网络。

函数定义

torch.nn.Sequential(*args)

参数说明:

  • *args: 任意数量的模块,将按照传入顺序依次执行。

使用示例

示例 1: 基本用法

使用 Sequential 构建简单网络:

实例

import torch
import torch.nn as nn

# 创建顺序容器
model = nn.Sequential(
    nn.Linear(10, 20),
    nn.ReLU(),
    nn.Linear(20, 5)
)

# 测试前向传播
input_tensor = torch.randn(3, 10)
output = model(input_tensor)

print("模型结构:")
print(model)
print("n输入形状:", input_tensor.shape)
print("输出形状:", output.shape)

示例 2: 使用 OrderedDict 给层命名

通过 OrderedDict 为每层指定名称:

实例

import torch
import torch.nn as nn
from collections import OrderedDict

# 使用 OrderedDict 命名层
model = nn.Sequential(OrderedDict([
    ('fc1', nn.Linear(784, 256)),
    ('relu1', nn.ReLU()),
    ('fc2', nn.Linear(256, 128)),
    ('relu2', nn.ReLU()),
    ('output', nn.Linear(128, 10))
]))

# 可以通过名称访问层
print("fc1 层:", model.fc1)
print("relu1 层:", model.relu1)

# 测试
x = torch.randn(32, 784)
output = model(x)
print("n输出形状:", output.shape)

示例 3: CNN 示例

构建卷积神经网络:

实例

import torch
import torch.nn as nn

cnn = nn.Sequential(
    # 第一个卷积块
    nn.Conv2d(3, 32, kernel_size=3, padding=1),
    nn.BatchNorm2d(32),
    nn.ReLU(),
    nn.MaxPool2d(2, 2),

    # 第二个卷积块
    nn.Conv2d(32, 64, kernel_size=3, padding=1),
    nn.BatchNorm2d(64),
    nn.ReLU(),
    nn.MaxPool2d(2, 2),

    # 展平
    nn.Flatten(),
    nn.Linear(64 * 8 * 8, 256),
    nn.ReLU(),
    nn.Dropout(0.5),
    nn.Linear(256, 10)
)

# 测试
x = torch.randn(4, 3, 32, 32)
output = cnn(x)

print("CNN 结构:")
print(cnn)
print("n输入形状:", x.shape)
print("输出形状:", output.shape)

示例 4: 访问中间层输出

使用 forward hook 访问中间层:

实例

import torch
import torch.nn as nn

# 简单网络
model = nn.Sequential(
    nn.Linear(10, 20),
    nn.ReLU(),
    nn.Linear(20, 10)
)

# 方法1:使用 hooks
feature = None

def hook_fn(module, input, output):
    global feature
    feature = output.clone()

# 注册 hook
model[1].register_forward_hook(hook_fn)

# 前向传播
x = torch.randn(1, 10)
output = model(x)

print("ReLU 输出形状:", feature.shape)
print("ReLU 输出:", feature.squeeze().tolist())

Sequential vs 手动定义 forward

方式 优点 缺点
nn.Sequential 简洁、快速构建 灵活性差,无法共享层
手动定义 forward 灵活控制数据流 代码较多

常见问题

Q1: 如何获取 Sequential 中的某一层?

通过索引或名称访问:model[0]model.fc1

Q2: Sequential 适合所有网络吗?

不适合有跳跃连接、分支结构或复杂数据流的网络。

Q3: 如何修改 Sequential 中的层?

可以通过 model[index] = new_module 替换指定层。


使用场景

nn.Sequential 主要应用场景包括:

  • 简单网络: 线性堆叠的 MLP、CNN
  • 快速原型: 快速验证模型想法
  • 特征提取: 固定的网络结构

提示:对于复杂网络,建议继承 nn.Module 手动定义 forward 方法。


PyTorch torch.nn 参考手册 PyTorch torch.nn 参考手册