ParameterDict¶
-
class
torch.nn.
ParameterDict
(parameters=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
-
copy
()[source]¶ Returns a copy of this
ParameterDict
instance.
-
fromkeys
(keys, default=None)[source]¶ Return a new ParameterDict with the keys provided
- Parameters
keys (iterable, string) – keys to make the new ParameterDict from
default (Parameter, optional) – value to set for all keys
-
get
(key, default=None)[source]¶ Return the parameter associated with key if present. Otherwise return default if provided, None if not.
- Parameters
key (string) – key to get from the ParameterDict
default (Parameter, optional) – value to return if key not present
-
pop
(key)[source]¶ Remove key from the ParameterDict and return its parameter.
- Parameters
key (string) – key to pop from the ParameterDict
-
setdefault
(key, default=None)[source]¶ If key is in the ParameterDict, return its parameter. If not, insert key with a parameter default and return default. default defaults to None.
- Parameters
key (string) – key to set default for
default (
Parameter
) – the parameter set to the key
-
update
(parameters)[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
)