Rate this Page

ParameterDict#

classtorch.nn.modules.container.ParameterDict(parameters=None)[source]#

Holds parameters in a dictionary.

ParameterDict can be indexed like a regular Python dictionary, but Parameters itcontains are properly registered, and will be visible by all Module methods.Other objects are treated as would be done by a regular Python dictionary

ParameterDict is anordered dictionary.update() with other unordered mappingtypes (e.g., Python’s plaindict) does not preserve the order of themerged mapping. On the other hand,OrderedDict or anotherParameterDictwill preserve their ordering.

Note that the constructor, assigning an element of the dictionary and theupdate() method will convert anyTensor intoParameter.

Parameters

values (iterable,optional) – a mapping (dictionary) of(string : Any) or an iterable of key-value pairsof type (string, Any)

Example:

classMyModule(nn.Module):def__init__(self)->None:super().__init__()self.params=nn.ParameterDict({"left":nn.Parameter(torch.randn(5,10)),"right":nn.Parameter(torch.randn(5,10)),})defforward(self,x,choice):x=self.params[choice].mm(x)returnx
clear()[source]#

Remove all items from the ParameterDict.

copy()[source]#

Return a copy of thisParameterDict instance.

Return type

ParameterDict

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

ParameterDict

get(key,default=None)[source]#

Return the parameter associated with key if present. Otherwise return default if provided, None if not.

Parameters
  • key (str) – key to get from the ParameterDict

  • default (Parameter,optional) – value to return if key not present

Return type

Any

items()[source]#

Return an iterable of the ParameterDict key/value pairs.

Return type

Iterable[tuple[str, Any]]

keys()[source]#

Return an iterable of the ParameterDict keys.

Return type

KeysView[str]

pop(key)[source]#

Remove key from the ParameterDict and return its parameter.

Parameters

key (str) – key to pop from the ParameterDict

Return type

Any

popitem()[source]#

Remove and return the last inserted(key, parameter) pair from the ParameterDict.

Return type

tuple[str,Any]

setdefault(key,default=None)[source]#

Set the default for a key in the Parameterdict.

If key is in the ParameterDict, return its value.If not, insertkey with a parameterdefault and returndefault.default defaults toNone.

Parameters
  • key (str) – key to set default for

  • default (Any) – the parameter set to the key

Return type

Any

update(parameters)[source]#

Update theParameterDict with key-value pairs fromparameters, overwriting existing keys.

Note

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

Parameters

parameters (iterable) – a mapping (dictionary) from string toParameter, or an iterable ofkey-value pairs of type (string,Parameter)

values()[source]#

Return an iterable of the ParameterDict values.

Return type

Iterable[Any]