Sequential Containers for Simple Architectures
What you'll learn: How to build neural networks quickly by stacking layers in order using nn.Sequential, without writing custom forward methods.
The Core Idea
When your model is just a straight chain of layers—input goes through layer 1, then layer 2, then layer 3, and so on—you don't need to write a full custom nn.Module with a forward() method. PyTorch's nn.Sequential container lets you define the entire model as an ordered list of layers, and it automatically handles the forward pass for you.
Think of it like a factory assembly line: each station (layer) processes the product (data) in strict order, and the output of one becomes the input to the next. nn.Sequential manages this flow automatically.
How It Works
Instead of this:
class MyModel(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 128)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
You can write:
model = nn.Sequential(
nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 10)
)
The Sequential container automatically chains these layers. When you call model(x), it passes x through each layer in order.
When to Use It
Use nn.Sequential for straightforward architectures where data flows linearly through layers. It's perfect for simple feedforward networks or basic convolutional stacks. If you need branching, skip connections, or conditional logic, you'll need a custom nn.Module.
Key Takeaway: nn.Sequential simplifies building linear layer stacks by automatically handling the forward pass, eliminating boilerplate code for simple architectures.