๐ŸŽ“ A1Learner
๐Ÿ”

PyTorch Sequential Models

A sequential model is a module that runs its layers in order. nn.Sequential builds one from a list of layers.

Build the sequence

The seed fixes the starting weights of both linear layers. ReLU is an activation that sets negative numbers to zero.

torch.manual_seed(0)
model = nn.Sequential(
    nn.Linear(5, 6),
    nn.ReLU(),
    nn.Linear(6, 2),
)
print(model)

Run the batch

One call runs every step in order. Rows of five enter and rows of two leave.

batch = torch.ones(3, 5)
print(model(batch).shape)

Reach one step

Steps have numbers, starting at 0. The first step is the first linear layer.

print(model[0])
print(len(model))

Example

import torch
from torch import nn

torch.manual_seed(0)

model = nn.Sequential(
    nn.Linear(5, 6),
    nn.ReLU(),
    nn.Linear(6, 2),
)

batch = torch.ones(3, 5)
output = model(batch)

print("Input shape:", batch.shape)
print("Output shape:", output.shape)
print("Steps in model:", len(model))

Key points

  • nn.Sequential: a module that runs layers in the order listed
  • Use nn.Sequential when the model is one straight line of steps
  • Use model[0] to reach the first step in the sequence
  • A sequential model is a single line of layers, run top to bottom

In this example, the seed is set first, so the starting weights are repeatable. The model lists three steps inside nn.Sequential. The first step makes six numbers from five. ReLU then sets any negative numbers to zero. The last step makes two numbers from six. The batch holds three rows of five ones. One call runs all three steps in order. The prints show the input and output shapes, then the step count.

Note

A custom class fits better when forward needs branches or reused layers

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Python Playground

Exercise

In which order does nn.Sequential run its layers?