Table of Contents
Shortcuts

# ParameterDict

class torch.nn.ParameterDict(parameters=None)

  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

  `ParameterDict` is an ordered dictionary. `update()` with other unordered mapping types (e.g., Python’s plain `dict`) does not preserve the order of the merged mapping. On the other hand, `OrderedDict` or another `ParameterDict` will preserve their ordering.

  Note that the constructor, assigning an element of the dictionary and the `update()` method will convert any `Tensor` into `Parameter`.

  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 x

  clear()

    Remove all items from the ParameterDict.

  copy()

    Returns a copy of this `ParameterDict` instance.

    Return type:

      ParameterDict

  fromkeys(keys, default=None)

    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)

    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()

    Return an iterable of the ParameterDict key/value pairs.

    Return type:

      Iterable[Tuple[str, Any]]

  keys()

    Return an iterable of the ParameterDict keys.

    Return type:

      Iterable[str]

  pop(key)

    Remove key from the ParameterDict and return its parameter.

    Parameters:

      key (str) – key to pop from the ParameterDict

    Return type:

      Any

  popitem()

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

    Return type:

      Tuple[str, Any]

  setdefault(key, default=None)

    If key is in the ParameterDict, return its value. If not, insert key with a parameter default and return default. default defaults to None.

    Parameters:

      - key (str) – key to set default for

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

    Return type:

      Any

  update(parameters)

    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()

    Return an iterable of the ParameterDict values.

    Return type:

      Iterable[Any]

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.