ParameterDict¶
- 
class 
torch.nn.ParameterDict(parameters: Optional[Mapping[str, Parameter]] = None)[source]¶ Holds parameters in a dictionary.
ParameterDict can be indexed like a regular Python dictionary, but parameters it contains are properly registered, and will be visible by all Module methods.
ParameterDictis an ordered dictionary that respectsthe order of insertion, and
in
update(), the order of the mergedOrderedDictor anotherParameterDict(the argument toupdate()).
Note that
update()with other unordered mapping types (e.g., Python’s plaindict) does not preserve the order of the merged mapping.- Parameters
 parameters (iterable, optional) – a mapping (dictionary) of (string :
Parameter) or an iterable of key-value pairs of type (string,Parameter)
Example:
class MyModule(nn.Module): def __init__(self): super(MyModule, self).__init__() self.params = nn.ParameterDict({ 'left': nn.Parameter(torch.randn(5, 10)), 'right': nn.Parameter(torch.randn(5, 10)) }) def forward(self, x, choice): x = self.params[choice].mm(x) return x
- 
items() → Iterable[Tuple[str, Parameter]][source]¶ Return an iterable of the ParameterDict key/value pairs.
- 
pop(key: str) → Parameter[source]¶ Remove key from the ParameterDict and return its parameter.
- Parameters
 key (string) – key to pop from the ParameterDict
- 
update(parameters: Mapping[str, Parameter]) → None[source]¶ Update the
ParameterDictwith the key-value pairs from a mapping or an iterable, overwriting existing keys.Note
If
parametersis anOrderedDict, aParameterDict, or an iterable of key-value pairs, the order of new elements in it is preserved.- Parameters
 parameters (iterable) – a mapping (dictionary) from string to
Parameter, or an iterable of key-value pairs of type (string,Parameter)