PyTorch torch.nn.Sequential function
PyTorch torch.nn Reference Manual
torch.nn.Sequential is a container in PyTorch for quickly building sequential models.
It executes modules in the order they are declared, making it 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
import
torch.
nn
as
nn
Create a sequential container
model
=
nn.
Sequential
(
nn.
Linear
(
10
,
20
)
,
nn.
ReLU
(
)
,
nn.
Linear
(
20
,
5
)
)
Test the forward pass
input_tensor
=
torch.
randn
(
3
,
10
)
output
=
model
(
input_tensor
)
(
"Model structure:"
)
(
model
)
(
"nInput shape:"
,
input_tensor.
shape
)
(
"Output shape:"
,
output.
shape
)
Example 2: Naming layers with OrderedDict
Specify a name for each layer via OrderedDict:
Example
import
torch
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
(
"fc1 layer:"
,
model.
fc1
)
(
"relu1 layer:"
,
model.
relu1
)
Testing
x
=
torch.
randn
(
32
,
784
)
output
=
model
(
x
)
(
"nOutput shape:"
,
output.
shape
)
Example 3: CNN example
Build a convolutional neural network:
Example
import
torch
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
)
)
Testing
x
=
torch.
randn
(
4
,
3
,
32
,
32
)
output
=
cnn
(
x
)
(
"CNN structure:"
)
(
cnn
)
(
"nInput shape:"
,
x.
shape
)
(
"Output shape:"
,
output.
shape
)
Example 4: Accessing intermediate layer outputs
Use a forward hook to access intermediate layers:
Example
import
torch
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 a hook
model
[
1
]
.
register_forward_hook
(
hook_fn
)
Forward propagation
x
=
torch.
randn
(
1
,
10
)
output
=
model
(
x
)
(
"ReLU output shape:"
,
feature.
shape
)
(
"ReLU output:"
,
feature.
squeeze
(
)
.
tolist
(
)
)
Sequential vs. manually defining forward
| Method | Advantages | Disadvantages |
|---|---|---|
nn.Sequential |
Concise and fast to build | Inflexible, cannot share layers |
| Manually defining forward | Flexible control over data flow | More code |
Common questions
Q1: How do you get a specific layer in a Sequential?
Access by index or name:model[0] or model.fc1
Q2: Is Sequential suitable for all networks?
Not suitable for networks with skip connections, branching structures, or complex data flow.
Q3: How do you modify a layer in a Sequential?
can be used via model[index] = new_module Replace the specified layer.
Use Cases
nn.Sequential Main application scenarios include:
- Simple network: Linearly stacked MLPs and CNNs
- Rapid prototyping: Quickly validating model ideas
- Feature extraction: Fixed network structures
Tip: For complex networks, it is recommended to subclass nn.Module and define the forward method manually.