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; a plain tensor is wrapped in a
Parameter first, as every entry point that adds one does.
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"].shape # retrieve by name
(64,)
>>> _ = bias_bank.pop("mid_freq") # remove dynamically
>>> list(bias_bank.keys())
['low_freq', 'high_freq']Used 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 parameter at key, as the reference does
-- None if that slot was explicitly cleared by assignment,
never an assertion failure (LCD-571).
Update the ParameterDict with another mapping of modules.
A plain tensor is wrapped in a Parameter, as the
reference does for every entry point that adds one.
Return an iterable over the modules in the ParameterDict.
Dunder methods
4Return the parameter at key, as the reference does -- None
if that slot was explicitly cleared by assignment, never an
assertion failure for reading back what was legitimately put
there (LCD-571).
Iterate over the registered child modules.
Return the number of registered child modules.
Register param under key.
A plain tensor is wrapped in a Parameter, as the
reference does for every entry point that adds one.