PyTorch torch.nn.Sequential Function
PyTorch torch.nn Reference Manual
torch.nn.SequentialIt is a container in PyTorch for quickly building sequential models.
It executes modules in the declared order and is suitable for building simple linear networks.
Function Definition
torch.nn.Sequential(*args)
Parameter Description:
*args: Any number of modules, which will be executed in the order they are passed in.
Usage Examples
Example 1: Basic Usage
Build a simple network using Sequential:
Example
import torch.nn as nn
# Create a sequential container
model = nn.Sequential(
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 5)
)
# Test forward propagation
input_tensor = torch.randn(3, 10)
output = model(input_tensor)
print("Model structure:")
print(model)
print("nInput shape:", input_tensor.shape)
print("Output shape:", output.shape)
Example 2: Naming Layers with OrderedDict
Specify a name for each layer using OrderedDict:
Example
import torch.nn as nn
from collections import OrderedDict
# Name layers using 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))
]))
# Layers can be accessed by name
print("fc1 layer:", model.fc1)
print("relu1 layer:", model.relu1)
# Test
x = torch.randn(32, 784)
output = model(x)
print("nOutput shape:", output.shape)
Example 3: CNN Example
Build a convolutional neural network:
Example
import torch.nn as nn
cnn = nn.Sequential(
# First convolution block
nn.Conv2d(3, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2, 2),
# Second convolution block
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2, 2),
# Flatten
nn.Flatten(),
nn.Linear(64 * 8 * 8, 256),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(256, 10)
)
# Test
x = torch.randn(4, 3, 32, 32)
output = cnn(x)
print("CNN structure:")
print(cnn)
print("nInput shape:", x.shape)
print("Output shape:", output.shape)
Example 4: Accessing Intermediate Layer Outputs
Use forward hooks to access intermediate layers:
Example
import torch.nn as nn
# Simple network
model = nn.Sequential(
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 10)
)
# Method 1: Using hooks
feature = None
def hook_fn(module, input, output):
global feature
feature = output.clone()
# Register hook
model[1].register_forward_hook(hook_fn)
# Forward propagation
x = torch.randn(1, 10)
output = model(x)
print("ReLU output shape:", feature.shape)
print("ReLU output:", feature.squeeze().tolist())
Sequential vs Manually Defining forward
| Approach | Advantages | Disadvantages |
|---|---|---|
nn.Sequential |
Concise, quick to build | Poor flexibility, cannot share layers |
| Manually defining forward | Flexible control of the data flow | More code |
Common Questions
Q1: How to get a specific layer in Sequential?
Access via index or name:model[0]ormodel.fc1
Q2: Is Sequential suitable for all networks?
Not suitable for networks with skip connections, branch structures, or complex data flows.
Q3: How to modify layers in Sequential?
You can usemodel[index] = new_moduleto replace the specified layer.
Use Cases
nn.SequentialMain application scenarios include:
- Simple networks: Linearly stacked MLP, CNN
- Rapid prototyping: Quickly validate model ideas
- Feature extraction: Fixed network structure
Tip: For complex networks, it is recommended to inherit nn.Module and manually define the forward method.
Other Extensions