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.
ParameterDict
is an ordered dictionary that respectsthe order of insertion, and
in
update()
, the order of the mergedOrderedDict
or 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
ParameterDict
with the key-value pairs from a mapping or an iterable, overwriting existing keys.Note
If
parameters
is 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
)