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
parametersdict[str, Parameter] or None= None{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 dynamicallyMethods (13)
__init__
→None__init__(parameters: dict[str, Parameter] | None = None)Initialise the ParameterDict module. See the class docstring for parameter semantics.
keys
→KeysView[str]keys()Return an iterable over the keys of the ParameterDict.
items
→ItemsView[str, Parameter]items()Return an iterable of (key, module) pairs in the ParameterDict.
values
→ValuesView[Parameter]values()Return an iterable over the modules in the ParameterDict.
get
→Parameter | Noneget(key: str, default: Parameter | None = None)Method on the ParameterDict module.
pop
→Parameterpop(key: str)Remove and return the module at the given index from the ParameterDict.
clear
→Noneclear()Remove all modules from the ParameterDict.
update
→Noneupdate(parameters: Mapping[str, Parameter] | Iterable[tuple[str, Parameter]])Update the ParameterDict with another mapping of modules.
forward
→Tensorforward(args: object = ())Apply the contained modules to the input.
Parameters
NoneReturns
TensorOutput tensor produced by the contained modules.
__getitem__
→Parameter__getitem__(key: str)Return the child module(s) at the given index or slice.
__setitem__
→None__setitem__(key: str, param: Parameter)Replace the child module at the given index.
__len__
→int__len__()Return the number of registered child modules.
__iter__
→Iterator[str]__iter__()Iterate over the registered child modules.