liveBook · Manning

Sequential

As explained in Deep Learning with PyTorch, Second Edition

A container module that groups child modules in order, forming a sequential network structure.

Sequential is a PyTorch container that applies child modules in the order they are provided, forming a straightforward pass-through network. It is useful when each layer receives the previous layer’s output, such as a linear model with an activation between two linear layers. The container can expose parameters for inspection and training, while activation modules such as Tanh contribute no trainable parameters. section 6.3.2

Key properties of Sequential include:

  • Positional construction names child modules by ordinal, while OrderedDict allows descriptive names such as hidden_linear and output_linear. section 6.3.2
  • named_parameters exposes parameter names and shapes, and named submodules can be accessed as attributes for inspecting parameters or gradients. section 6.3.2
  • Sequential supports simple multilayer and convolutional networks, but it does not provide explicit visibility into intermediate outputs. section 7.5 section 8.2.4
  • Use a custom nn.Module when the architecture needs reshaping steps, nonlinear data flows, custom layer interactions, or conditional paths. section 6.3.2 section 8.2.4

A named feed-forward network

A named Sequential model can combine a hidden linear layer, Tanh activation, and output layer: nn.Sequential(OrderedDict([('hidden_linear', nn.Linear(1, 8)), ('hidden_activation', nn.Tanh()), ('output_linear', nn.Linear(8, 1))])). Its parameters can then be passed directly to an optimizer and inspected through names such as hidden_linear.weight. section 6.3.2

# In[19]:
from collections import OrderedDict

seq_model = nn.Sequential(OrderedDict([
    ('hidden_linear', nn.Linear(1, 8)),
    ('hidden_activation', nn.Tanh()),
    ('output_linear', nn.Linear(8, 1))
]))

seq_model

# Out[19]:
Sequential(
  (hidden_linear): Linear(in_features=1, out_features=8, bias=True)
  (hidden_activation): Tanh()
  (output_linear): Linear(in_features=8, out_features=1, bias=True)
)

Inspect the complete named Sequential definition and its printed module structure. section 6.3.2

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains ordering, naming, parameter inspection, and visibility limits, but defers flexible architectures to custom nn.Module.
Examples
Uses named linear networks, parameter listings, gradient inspection, and convolutional pipelines that expose a reshaping problem.
What it assumes
Assumes basic PyTorch modules and training, while demonstrating ordered composition and parameter inspection directly.
What you can do after
Builds ordered PyTorch models, trains them, and inspects their named parameters and gradients.
How it connects
Connects Sequential with PyTorch modules, linear layers, activation functions, convolution, pooling, parameters, and custom nn.Module.

Also explained in

Compare how 2 books explain Sequential →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.