class torch.nn.ModuleDict(modules: Optional[Mapping[str, torch.nn.modules.module.Module]] = None)
[source]
Holds submodules in a dictionary.
ModuleDict
can be indexed like a regular Python dictionary, but modules it contains are properly registered, and will be visible by all Module
methods.
ModuleDict
is an ordered dictionary that respects
update()
, the order of the merged OrderedDict
, dict
(started from Python 3.6) or another ModuleDict
(the argument to update()
).Note that update()
with other unordered mapping types (e.g., Python’s plain dict
before Python version 3.6) does not preserve the order of the merged mapping.
modules (iterable, optional) – a mapping (dictionary) of (string: module) or an iterable of key-value pairs of type (string, module)
Example:
class MyModule(nn.Module): def __init__(self): super(MyModule, self).__init__() self.choices = nn.ModuleDict({ 'conv': nn.Conv2d(10, 10, 3), 'pool': nn.MaxPool2d(3) }) self.activations = nn.ModuleDict([ ['lrelu', nn.LeakyReLU()], ['prelu', nn.PReLU()] ]) def forward(self, x, choice, act): x = self.choices[choice](x) x = self.activations[act](x) return x
clear() → None
[source]
Remove all items from the ModuleDict.
items() → Iterable[Tuple[str, torch.nn.modules.module.Module]]
[source]
Return an iterable of the ModuleDict key/value pairs.
keys() → Iterable[str]
[source]
Return an iterable of the ModuleDict keys.
pop(key: str) → torch.nn.modules.module.Module
[source]
Remove key from the ModuleDict and return its module.
key (string) – key to pop from the ModuleDict
update(modules: Mapping[str, torch.nn.modules.module.Module]) → None
[source]
Update the ModuleDict
with the key-value pairs from a mapping or an iterable, overwriting existing keys.
Note
If modules
is an OrderedDict
, a ModuleDict
, or an iterable of key-value pairs, the order of new elements in it is preserved.
values() → Iterable[torch.nn.modules.module.Module]
[source]
Return an iterable of the ModuleDict values.
© 2019 Torch Contributors
Licensed under the 3-clause BSD License.
https://pytorch.org/docs/1.7.0/generated/torch.nn.ModuleDict.html