0
0
PyTorchml~5 mins

Sequential model shortcut in PyTorch

Choose your learning style9 modes available
Introduction

We use the Sequential model shortcut to quickly build a simple chain of layers in a neural network without writing extra code for each step.

When you want to create a simple neural network with layers stacked one after another.
When you need a quick way to test ideas without writing a full class for the model.
When your model has a straight flow from input to output without branches or loops.
Syntax
PyTorch
import torch.nn as nn

model = nn.Sequential(
    nn.Linear(input_size, hidden_size),
    nn.ReLU(),
    nn.Linear(hidden_size, output_size)
)

The layers are passed as arguments inside nn.Sequential in the order they connect.

This shortcut works well for simple feed-forward networks.

Examples
A model with input size 10, one hidden layer of size 5, and output size 2.
PyTorch
model = nn.Sequential(
    nn.Linear(10, 5),
    nn.ReLU(),
    nn.Linear(5, 2)
)
A simple convolutional model for 28x28 images with 1 channel.
PyTorch
model = nn.Sequential(
    nn.Conv2d(1, 16, 3),
    nn.ReLU(),
    nn.Flatten(),
    nn.Linear(16*26*26, 10)
)
Sample Model

This code builds a simple Sequential model with two linear layers and ReLU in between. It runs a forward pass on sample data, calculates mean squared error loss, and performs one optimization step.

PyTorch
import torch
import torch.nn as nn
import torch.optim as optim

# Create a simple Sequential model
model = nn.Sequential(
    nn.Linear(4, 3),
    nn.ReLU(),
    nn.Linear(3, 2)
)

# Sample input tensor (batch size 2, features 4)
x = torch.tensor([[1.0, 2.0, 3.0, 4.0],
                  [4.0, 3.0, 2.0, 1.0]])

# Forward pass
output = model(x)

# Print output
print("Model output:")
print(output)

# Define dummy target and loss
target = torch.tensor([[0.0, 1.0], [1.0, 0.0]])
criterion = nn.MSELoss()
loss = criterion(output, target)
print(f"Loss: {loss.item():.4f}")

# Backward pass
optimizer = optim.SGD(model.parameters(), lr=0.01)
optimizer.zero_grad()
loss.backward()
optimizer.step()
OutputSuccess
Important Notes

Sequential models are easy to build but not flexible for complex architectures like multiple inputs or outputs.

Use nn.Sequential only when the data flows straight through layers.

Summary

Sequential shortcut helps build simple neural networks fast.

It stacks layers in order without extra code.

Good for beginners and quick experiments.