ModuleDict
- class torch.nn.ModuleDict(modules=None)
Holds submodules in a dictionary.
ModuleDictcan be indexed like a regular Python dictionary, but modules it contains are properly registered, and will be visible by allmethods.
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
methods.
ModuleDict is an ordered dictionary that respects
the order of insertion, and
in update(), the order of the merged
OrderedDict, dict or another
ModuleDict (the argument to
update()).
Note that update() with other unordered mapping
types 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) -> None:
super().__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
Remove all items from the ModuleDict.
Update the ModuleDict with key-value pairs from a mapping, overwriting existing keys.
Return an iterable of the ModuleDict values.