ParameterDict
ModuleParameterDict(parameters: dict[str, Parameter] | None = None)A dict-like container that registers Parameter objects under string keys.
ParameterDict maps arbitrary string keys to Parameter leaf tensors
and registers each one via register_parameter, making them first-class
citizens of the Lucid module system: they appear in parameters(),
named_parameters(), and state_dict()/load_state_dict.
This is the preferred pattern when parameters have meaningful names or when the set of parameters is determined programmatically (e.g., one weight matrix per attention head type, one scale per branch name).
Parameters
{name: parameter} mapping. Each entry is registered via
register_parameter. Pass None (default) for an empty dict.Attributes
_parametersOrderedDict[str, Parameter | None]Parameter, inherited
from Module.Notes
forwardis intentionally not implemented. Access parameters by key in the enclosing module'sforward:self.params['query'].- Insertion order is preserved (backed by
OrderedDict).
Examples
**Named projection matrices for a custom multi-head attention layer:**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> class CustomAttention(nn.Module):
... def __init__(self, dim: int) -> None:
... super().__init__()
... self.weights = nn.ParameterDict({
... "query": nn.Parameter(lucid.randn(dim, dim)),
... "key": nn.Parameter(lucid.randn(dim, dim)),
... "value": nn.Parameter(lucid.randn(dim, dim)),
... "output": nn.Parameter(lucid.randn(dim, dim)),
... })
...
... def forward(self, x: lucid.Tensor) -> lucid.Tensor:
... q = x @ self.weights["query"]
... k = x @ self.weights["key"]
... v = x @ self.weights["value"]
... # ... attention logic ...
... return v @ self.weights["output"]
**Runtime-configured bias bank with named entries:**
>>> bias_bank = nn.ParameterDict()
>>> for name in ["low_freq", "mid_freq", "high_freq"]:
... bias_bank[name] = nn.Parameter(lucid.zeros(64))
>>> bias_bank["low_freq"] # retrieve by name
>>> bias_bank.pop("mid_freq") # remove dynamicallyUsed by 1
Constructors
1Instance methods
8Remove all modules from the ParameterDict.
Apply the contained modules to the input.
Parameters
NoneReturns
TensorOutput tensor produced by the contained modules.
Method on the ParameterDict module.
Return an iterable of (key, module) pairs in the ParameterDict.
Return an iterable over the keys of the ParameterDict.
Remove and return the module at the given index from the ParameterDict.
update
→Noneupdate(parameters: Mapping[str, Parameter] | Iterable[tuple[str, Parameter]])Update the ParameterDict with another mapping of modules.
Return an iterable over the modules in the ParameterDict.
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.