Rate this Page

ModuleDict#

classtorch.nn.modules.container.ModuleDict(modules=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 allModule methods.

ModuleDict is anordered dictionary that respects

  • the order of insertion, and

  • inupdate(), the order of the mergedOrderedDict,dict (started from Python 3.6) or anotherModuleDict (the argument toupdate()).

Note thatupdate() with other unordered mappingtypes does not preserve 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
clear()[source]#

Remove all items from the ModuleDict.

items()[source]#

Return an iterable of the ModuleDict key/value pairs.

Return type:

ItemsView[str,Module]

keys()[source]#

Return an iterable of the ModuleDict keys.

Return type:

KeysView[str]

pop(key)[source]#

Remove key from the ModuleDict and return its module.

Parameters:

key (str) – key to pop from the ModuleDict

Return type:

Module

update(modules)[source]#

Update theModuleDict with key-value pairs from a mapping, overwriting existing keys.

Note

Ifmodules is anOrderedDict, aModuleDict, oran iterable of key-value pairs, the order of new elements in it is preserved.

Parameters:

modules (iterable) – a mapping (dictionary) from string toModule,or an iterable of key-value pairs of type (string,Module)

values()[source]#

Return an iterable of the ModuleDict values.

Return type:

ValuesView[Module]