ModuleDict#
- classtorch.nn.ModuleDict(modules=None)[source]#
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 allModulemethods.ModuleDictis anordered dictionary that respectsthe order of insertion, and
in
update(), the order of the mergedOrderedDict,dict(started from Python 3.6) or anotherModuleDict(the argument toupdate()).
Note that
update()with other unordered mappingtypes (e.g., Python’s plaindictbefore Python version 3.6) does notpreserve the order of the merged mapping.- Parameters
modules (iterable,optional) – a mapping (dictionary) of (string: module)or an iterable of key-value pairs of type (string, module)
Example:
classMyModule(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()]])defforward(self,x,choice,act):x=self.choices[choice](x)x=self.activations[act](x)returnx
- update(modules)[source]#
Update the
ModuleDictwith key-value pairs from a mapping, overwriting existing keys.Note
If
modulesis anOrderedDict, aModuleDict, oran iterable of key-value pairs, the order of new elements in it is preserved.