Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

nn.Sequential

torch.nn.Sequential is a registered container that applies modules in order, passing each output to the next. It is useful for compact feed-forward chains and can accept positional modules or an OrderedDict of named modules.

How it works

Sequential stores submodules in a defined order and makes them behave as one module. During the forward pass, it sends the input to the first module, passes each result to the next module, and returns the final result; modules may also be supplied through an OrderedDict when names are needed.

Syntax

class torch.nn.Sequential(*args: Module)
class torch.nn.Sequential(*arg: OrderedDict[str, Module])

Parameters

NameTypeDescription
*argsModuleModules supplied positionally; they are inserted and executed in the supplied order.
*argOrderedDict[str, Module]An ordered mapping from child names to modules; it provides explicit names and order.

Returns

A Sequential module whose forward call returns the output of its last contained module.

Key points

  • nn.Sequential is a Module container for representing a straight-line stack of layers.
  • Constructor order determines execution order.
  • An OrderedDict lets you construct the chain with explicit child names.
  • Each module receives the output produced by the preceding module.
  • The container's result is the output from its final child.
  • Children are registered submodules, so the container treats the complete chain as one module.

Common mistakes

  • Use Sequential when each module's output should become the next module's input; it is not the general-purpose storage behavior of ModuleList.
  • The chain assumes that the modules can accept the preceding module's output, so incompatible shapes or signatures fail during execution.

Example: A feed-forward module chain

import torch
from torch import nn

model = nn.Sequential(
    nn.Linear(4, 8),
    nn.ReLU(),
    nn.Linear(8, 2),
)

x = torch.randn(3, 4)
y = model(x)
print(y.shape)  # torch.Size([3, 2])

Creates a registered two-layer feed-forward model and applies it to a batch of three four-feature inputs.

When to use it

Straight-line neural networks

Build a compact feed-forward network from layers whose interfaces line up.

Transformation pipelines

Compose preprocessing or tensor transformations that should run in a fixed sequence.

Named module stacks

Give layers stable descriptive names while retaining sequential execution.

Interview questions

QWhat does nn.Sequential do?

Sequential is a container module whose children are applied in constructor order. Its forward pass feeds the input through each child and returns the final child's output; because the children are registered submodules, the container can be used as one module.

Follow-up: How does it differ from ModuleList? ModuleList stores modules for explicit iteration, whereas Sequential automatically connects them in a cascade.

QHow can modules be supplied to Sequential?

You can pass modules positionally, such as Linear, ReLU, and Linear, or pass an OrderedDict of named modules. The latter is useful when stable, descriptive child names are needed.

Follow-up: What happens to the names with positional construction? PyTorch assigns container keys based on their positions, while an OrderedDict supplies the keys explicitly.

QWhen should Sequential not be used?

Sequential is appropriate for a straight-line computation in which one module's output is the next module's input. It is not suited to architectures requiring branching, multiple inputs or outputs, or custom data flow.

Follow-up: What should be used for custom data flow? Define a Module with an explicit forward method so the computation can express those branches and connections.

  • torch.nn.Module
  • torch.nn.ModuleList
  • torch.nn.ModuleDict
  • torch.nn.Sequential.append
  • torch.nn.Sequential.forward
  • torch.nn.Sequential.insert

Source: Sequential — PyTorch main documentation. Checked 2026-10-16.