Sequential
ModuleSequential(args: Module | OrderedDict[str, Module] = ())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
*argsModule or OrderedDict[str, Module]= ()- If a single
OrderedDict[str, Module]is passed, the modules are registered under their dict keys. - If multiple positional
Modulearguments are passed, they are registered under string-formatted integer indices'0','1','2', …
Attributes
_modulesOrderedDict[str, Module | None]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 layerMethods (9)
__init__
→None__init__(args: Module | OrderedDict[str, Module] = ())Initialise the Sequential module. See the class docstring for parameter semantics.
forward
→Tensorforward(x: Tensor)Apply the contained modules to the input.
Parameters
xTensorReturns
TensorOutput tensor produced by the contained modules.
append
→Noneappend(module: Module)Append a module to the end of the Sequential.
extend
→Noneextend(modules: Iterable[Module])Append each module from an iterable to the Sequential.
insert
→Noneinsert(index: int, module: Module)Insert a module at the given position in the Sequential.
__getitem__
→Module__getitem__(idx: int | slice)Return the child module(s) at the given index or slice.
__setitem__
→None__setitem__(idx: int, module: Module)Replace the child module at the given index.
__len__
→int__len__()Return the number of registered child modules.
__iter__
→Iterator[Module]__iter__()Iterate over the registered child modules.