class

ModuleDict

extendsModule
ModuleDict(modules: dict[str, Module] | None = None)
source

A dict-like container that registers all child modules under string keys.

ModuleDict maps arbitrary string keys to Module objects and integrates them fully with the Lucid module system: parameters(), state_dict(), and device/dtype transfers all traverse into the registered modules transparently.

Unlike Sequential and ModuleList, the keys are user-defined strings rather than integers, making ModuleDict well-suited for multi-task or multi-branch architectures where branches have semantic names.

forward is not defined — the user dispatches to specific branches by key in the enclosing module's forward.

Parameters

modulesdict[str, Module] or None= None
Initial {name: module} mapping. Each entry is registered via add_module(key, module). Pass None (default) for an empty dict.

Attributes

_modulesOrderedDict[str, Module | None]
Internal ordered mapping from string key to child module.

Notes

  • Insertion order is preserved (backed by OrderedDict).
  • update accepts both Mapping[str, Module] and Iterable[tuple[str, Module]].

Examples

**Multi-task prediction heads keyed by task name:**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> class MultiTaskModel(nn.Module):
...     def __init__(self, shared_dim: int) -> None:
...         super().__init__()
...         self.backbone = nn.Linear(shared_dim, 256)
...         self.heads = nn.ModuleDict({
...             "classification": nn.Linear(256, 10),
...             "regression":     nn.Linear(256, 1),
...             "segmentation":   nn.Linear(256, 64),
...         })
...
...     def forward(self, x: lucid.Tensor, task: str) -> lucid.Tensor:
...         feat = lucid.nn.functional.relu(self.backbone(x))
...         return self.heads[task](feat)
>>>
>>> model = MultiTaskModel(shared_dim=512)
>>> # Dispatch dynamically at runtime:
>>> logits = model(x, task="classification")
**Conditional gating — adding/removing branches at runtime:**
>>> router = nn.ModuleDict({"low": nn.Linear(64, 32)})
>>> router["high"] = nn.Linear(64, 128)   # register a new branch
>>> router.pop("low")                      # remove old branch
>>> for name, branch in router.items():
...     print(name, branch)

Methods (13)

dunder

__init__

None
__init__(modules: dict[str, Module] | None = None)
source

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

fn

keys

KeysView[str]
keys()
source

Return an iterable over the keys of the ModuleDict.

fn

items

ItemsView[str, Module]
items()
source

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

fn

values

ValuesView[Module]
values()
source

Return an iterable over the modules in the ModuleDict.

fn

get

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

Method on the ModuleDict module.

fn

pop

Module
pop(key: str)
source

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

fn

clear

None
clear()
source

Remove all modules from the ModuleDict.

fn

update

None
update(modules: Mapping[str, Module] | Iterable[tuple[str, Module]])
source

Update the ModuleDict 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__

Module
__getitem__(key: str)
source

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

dunder

__setitem__

None
__setitem__(key: str, module: Module)
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.