A list-like container that registers all child modules with the parent.
ModuleList stores an ordered collection of Module objects and
makes them visible to the Lucid module system (parameters(),
state_dict(), device transfer, etc.) exactly like named sub-modules
defined as class attributes. Unlike Sequential, it does not
define forward — the user iterates over the list manually and
controls the data flow.
Parameters
'0', '1', …).
Pass None (default) to start with an empty list.Attributes
_modulesOrderedDict[str, Module | None]Notes
forwardis intentionally not implemented — calling it raisesNotImplementedError. Design your own data flow in the enclosing module'sforwardmethod.- Parameters of all registered modules participate in gradient computation
and are returned by
parameters()andnamed_parameters()on the parent module. - After insertion or deletion the internal indices are kept contiguous by
_renumber_modules.
Examples
**Ensemble of encoders — manual iteration:**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> class EnsembleEncoder(nn.Module):
... def __init__(self, n: int, in_dim: int, out_dim: int) -> None:
... super().__init__()
... self.encoders = nn.ModuleList(
... [nn.Linear(in_dim, out_dim) for _ in range(n)]
... )
...
... def forward(self, x: lucid.Tensor) -> lucid.Tensor:
... # Stack outputs from each encoder along a new axis
... outputs = [enc(x) for enc in self.encoders]
... return lucid.stack(outputs, dim=0)
>>>
>>> enc = EnsembleEncoder(n=4, in_dim=256, out_dim=64)
**Dynamic layer collection built incrementally:**
>>> class DynamicMLP(nn.Module):
... def __init__(self, dims: list[int]) -> None:
... super().__init__()
... self.layers = nn.ModuleList()
... for in_d, out_d in zip(dims[:-1], dims[1:]):
... self.layers.append(nn.Linear(in_d, out_d))
...
... def forward(self, x: lucid.Tensor) -> lucid.Tensor:
... for layer in self.layers[:-1]:
... x = lucid.nn.functional.relu(layer(x))
... return self.layers[-1](x)
>>>
>>> mlp = DynamicMLP([784, 512, 256, 10])Used by 1
Constructors
1Instance methods
4Append a module to the end of the ModuleList.
Append each module from an iterable to the ModuleList.
Apply the contained modules to the input.
Parameters
NoneReturns
TensorOutput tensor produced by the contained modules.
Insert a module at the given position in the ModuleList.
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.