API

 torch / nn / torch.nn


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 respects

Note that update() with other unordered mapping types (e.g., Python’s plain dict) 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
clear() → None[source]

Remove all items from the ParameterDict.

items() → Iterable[Tuple[str, Parameter]][source]

Return an iterable of the ParameterDict key/value pairs.

keys() → Iterable[str][source]

Return an iterable of the ParameterDict keys.

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 an OrderedDict, a ParameterDict, 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)

values() → Iterable[Parameter][source]

Return an iterable of the ParameterDict values.


此页内容是否对您有帮助