On this page
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. Other objects are treated as would be done by a regular Python dictionary
ParameterDictis an ordered dictionary.update()with other unordered mapping types (e.g., Python’s plaindict) does not preserve the order of the merged mapping. On the other hand,OrderedDictor anotherParameterDictwill preserve their ordering.Note that the constructor, assigning an element of the dictionary and the
update()method will convert anyTensorintoParameter.- Parameters
-
values (iterable, optional) – a mapping (dictionary) of (string : Any) or an iterable of key-value pairs of type (string, Any)
Example:
class MyModule(nn.Module): def __init__(self): super().__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 xclear()[source]-
Remove all items from the ParameterDict.
copy()[source]-
Returns a copy of this
ParameterDictinstance.- Return type
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
- Return type
get(key, default=None)[source]-
Return the parameter associated with key if present. Otherwise return default if provided, None if not.
items()[source]-
Return an iterable of the ParameterDict key/value pairs.
pop(key)[source]-
Remove key from the ParameterDict and return its parameter.
popitem()[source]-
Remove and return the last inserted
(key, parameter)pair from the ParameterDict
setdefault(key, default=None)[source]-
If key is in the ParameterDict, return its value. If not, insert
keywith a parameterdefaultand returndefault.defaultdefaults toNone.
update(parameters)[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)
© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.nn.ParameterDict.html