r"""Copy parameters and buffers from :attr:`state_dict` into this module and its descendants. If :attr:`strict` is ``True``, then the keys of :attr:`state_dict` must exactly match the keys returned by this module's :meth:`~torch.nn.Module.state_dict` function. .. wa
(self, state_dict: Mapping[str, Any],
strict: bool = True, assign: bool = False)
| 2065 | unexpected_keys.append(key) |
| 2066 | |
| 2067 | def load_state_dict(self, state_dict: Mapping[str, Any], |
| 2068 | strict: bool = True, assign: bool = False): |
| 2069 | r"""Copy parameters and buffers from :attr:`state_dict` into this module and its descendants. |
| 2070 | |
| 2071 | If :attr:`strict` is ``True``, then |
| 2072 | the keys of :attr:`state_dict` must exactly match the keys returned |
| 2073 | by this module's :meth:`~torch.nn.Module.state_dict` function. |
| 2074 | |
| 2075 | .. warning:: |
| 2076 | If :attr:`assign` is ``True`` the optimizer must be created after |
| 2077 | the call to :attr:`load_state_dict`. |
| 2078 | |
| 2079 | Args: |
| 2080 | state_dict (dict): a dict containing parameters and |
| 2081 | persistent buffers. |
| 2082 | strict (bool, optional): whether to strictly enforce that the keys |
| 2083 | in :attr:`state_dict` match the keys returned by this module's |
| 2084 | :meth:`~torch.nn.Module.state_dict` function. Default: ``True`` |
| 2085 | assign (bool, optional): whether to assign items in the state |
| 2086 | dictionary to their corresponding keys in the module instead |
| 2087 | of copying them inplace into the module's current parameters and buffers. |
| 2088 | When ``False``, the properties of the tensors in the current |
| 2089 | module are preserved while when ``True``, the properties of the |
| 2090 | Tensors in the state dict are preserved. |
| 2091 | Default: ``False`` |
| 2092 | |
| 2093 | Returns: |
| 2094 | ``NamedTuple`` with ``missing_keys`` and ``unexpected_keys`` fields: |
| 2095 | * **missing_keys** is a list of str containing the missing keys |
| 2096 | * **unexpected_keys** is a list of str containing the unexpected keys |
| 2097 | |
| 2098 | Note: |
| 2099 | If a parameter or buffer is registered as ``None`` and its corresponding key |
| 2100 | exists in :attr:`state_dict`, :meth:`load_state_dict` will raise a |
| 2101 | ``RuntimeError``. |
| 2102 | """ |
| 2103 | if not isinstance(state_dict, Mapping): |
| 2104 | raise TypeError(f"Expected state_dict to be dict-like, got {type(state_dict)}.") |
| 2105 | |
| 2106 | missing_keys: List[str] = [] |
| 2107 | unexpected_keys: List[str] = [] |
| 2108 | error_msgs: List[str] = [] |
| 2109 | |
| 2110 | # copy state_dict so _load_from_state_dict can modify it |
| 2111 | metadata = getattr(state_dict, '_metadata', None) |
| 2112 | state_dict = OrderedDict(state_dict) |
| 2113 | if metadata is not None: |
| 2114 | # mypy isn't aware that "_metadata" exists in state_dict |
| 2115 | state_dict._metadata = metadata # type: ignore[attr-defined] |
| 2116 | |
| 2117 | def load(module, local_state_dict, prefix=''): |
| 2118 | local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {}) |
| 2119 | if assign: |
| 2120 | local_metadata['assign_to_params_buffers'] = assign |
| 2121 | module._load_from_state_dict( |
| 2122 | local_state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys, error_msgs) |
| 2123 | for name, child in module._modules.items(): |
| 2124 | if child is not None: |