Skip to content
class

ParameterDict

extendsModule
ParameterDict(parameters: dict[str, Tensor] | 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, Tensor] or None= None
Initial {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]
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"].shape  # retrieve by name
(64,)
>>> _ = bias_bank.pop("mid_freq")  # remove dynamically
>>> list(bias_bank.keys())
['low_freq', 'high_freq']

Used by 1

Constructors

1
dunder

__init__

→None
__init__(parameters: dict[str, Tensor] | None = None)

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

Instance methods

8
fn

clear

→None
clear()

Remove all modules from the ParameterDict.

fn

forward

→Tensor
forward(args: object = ())

Apply the contained modules to the input.

Parameters

None
No description.

Returns

Tensor

Output tensor produced by the contained modules.

fn

get

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

Method on the ParameterDict module.

fn

items

→ItemsView[str, Parameter]
items()

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

fn

keys

→KeysView[str]
keys()

Return an iterable over the keys of the ParameterDict.

fn

pop

→Parameter | None
pop(key: str)

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).

fn

update

→None
update(parameters: Mapping[str, Tensor] | Iterable[tuple[str, Tensor]])

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.

fn

values

→ValuesView[Parameter]
values()

Return an iterable over the modules in the ParameterDict.

Dunder methods

4
dunder

__getitem__

→Parameter | None
__getitem__(key: str)

Return 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).

dunder

__iter__

→Iterator[str]
__iter__()

Iterate over the registered child modules.

dunder

__len__

→int
__len__()

Return the number of registered child modules.

dunder

__setitem__

→None
__setitem__(key: str, param: Tensor)

Register param under key.

A plain tensor is wrapped in a Parameter, as the reference does for every entry point that adds one.