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

