Straight-line neural networks
Build a compact feed-forward network from layers whose interfaces line up.
PyTorch · Class
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.
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.
class torch.nn.Sequential(*args: Module)
class torch.nn.Sequential(*arg: OrderedDict[str, Module])| Name | Type | Description |
|---|---|---|
*args | Module | Modules supplied positionally; they are inserted and executed in the supplied order. |
*arg | OrderedDict[str, Module] | An ordered mapping from child names to modules; it provides explicit names and order. |
A Sequential module whose forward call returns the output of its last contained module.
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.
Build a compact feed-forward network from layers whose interfaces line up.
Compose preprocessing or tensor transformations that should run in a fixed sequence.
Give layers stable descriptive names while retaining sequential execution.
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.
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.
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.Moduletorch.nn.ModuleListtorch.nn.ModuleDicttorch.nn.Sequential.appendtorch.nn.Sequential.forwardtorch.nn.Sequential.insert