class

ParameterDict

extendsModule
ParameterDict(parameters: dict[str, Parameter] | None = None)
source

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
Initial {name: parameter} mapping. Each entry is registered via register_parameter. Pass None (default) for an empty dict.

Attributes

_parametersOrderedDict[str, Parameter | None]
Internal ordered mapping from string key to Parameter, inherited from Module.

Notes

  • forward is intentionally not implemented. Access parameters by key in the enclosing module's forward: 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 dynamically

Methods (13)

dunder

__init__

None
__init__(parameters: dict[str, Parameter] | None = None)
source

Initialise the ParameterDict module. See the class docstring for parameter semantics.

fn

keys

KeysView[str]
keys()
source

Return an iterable over the keys of the ParameterDict.

fn

items

ItemsView[str, Parameter]
items()
source

Return an iterable of (key, module) pairs in the ParameterDict.

fn

values

ValuesView[Parameter]
values()
source

Return an iterable over the modules in the ParameterDict.

fn

get

Parameter | None
get(key: str, default: Parameter | None = None)
source

Method on the ParameterDict module.

fn

pop

Parameter
pop(key: str)
source

Remove and return the module at the given index from the ParameterDict.

fn

clear

None
clear()
source

Remove all modules from the ParameterDict.

fn

update

None
update(parameters: Mapping[str, Parameter] | Iterable[tuple[str, Parameter]])
source

Update the ParameterDict with another mapping of modules.

fn

forward

Tensor
forward(args: object = ())
source

Apply the contained modules to the input.

Parameters

None
No description.

Returns

Tensor

Output tensor produced by the contained modules.

dunder

__getitem__

Parameter
__getitem__(key: str)
source

Return the child module(s) at the given index or slice.

dunder

__setitem__

None
__setitem__(key: str, param: Parameter)
source

Replace the child module at the given index.

dunder

__len__

int
__len__()
source

Return the number of registered child modules.

dunder

__iter__

Iterator[str]
__iter__()
source

Iterate over the registered child modules.