An ordered container of modules applied one after another in sequence.
Sequential composes a pipeline of modules so that the output of each
module is fed as the sole input to the next. Given modules
the computation is:
This is the most common way to build feedforward models in Lucid.
Modules are stored internally in an OrderedDict and indexed either
by integer position or by string key.
Parameters
Attributes
_modulesOrderedDict[str, Module | None]Internal ordered mapping from string key to child module, inherited
from
Module. Direct mutation is discouraged — prefer append,
insert, __setitem__, and __delitem__.Notes
- Slicing (
seq[1:3]) returns a newSequentialcontaining only the sliced modules — keys are preserved from the original. - After a deletion the internal keys are renumbered (
_renumber_modules) so that indices remain contiguous integers starting at0. Sequentialdoes not implement custom parameter grouping; everyParameterin every child module is returned byparameters().
Examples
**CNN feature extractor followed by classifier head:**
>>> import lucid.nn as nn
>>> backbone = nn.Sequential(
... nn.Conv2d(3, 64, kernel_size=3, padding=1),
... nn.ReLU(),
... nn.Conv2d(64, 128, kernel_size=3, padding=1),
... nn.ReLU(),
... nn.AdaptiveAvgPool2d((1, 1)),
... )
>>> head = nn.Linear(128, 10)
>>> # backbone: (N, 3, H, W) -> (N, 128, 1, 1)
**Named modules via OrderedDict:**
>>> from collections import OrderedDict
>>> model = nn.Sequential(OrderedDict([
... ("conv1", nn.Conv2d(1, 32, 3, padding=1)),
... ("relu1", nn.ReLU()),
... ("conv2", nn.Conv2d(32, 64, 3, padding=1)),
... ("pool", nn.MaxPool2d(2)),
... ]))
>>> # Access by name:
>>> conv = model["conv1"] # __getitem__ via integer works too: model[0]
**Dynamic construction and mutation:**
>>> layers = [nn.Linear(128, 128) for _ in range(4)]
>>> mlp = nn.Sequential(*layers)
>>> mlp.append(nn.Linear(128, 10)) # add output layer
>>> mlp.insert(0, nn.Flatten()) # prepend flatten
>>> del mlp[1] # remove first hidden layerUsed by 1
Constructors
1Instance methods
4Append a module to the end of the Sequential.
Append each module from an iterable to the Sequential.
Insert a module at the given position in the Sequential.
Dunder methods
4Return the child module(s) at the given index or slice.
Iterate over the registered child modules.
Return the number of registered child modules.
Replace the child module at the given index.