r"""Loads a given dictionary created by :func:`state_dict` into this module. If ``strict`` is ``True``, the keys of :func:`state_dict` must exactly match the keys returned by :func:`state_dict`. Users can also pass a closure: ``Function[key: str, var: Tensor] -> Optional[np.
(
self,
state_dict: Union[dict, Callable[[str, Tensor], Optional[np.ndarray]]],
strict=True,
)
| 493 | return rst |
| 494 | |
| 495 | def load_state_dict( |
| 496 | self, |
| 497 | state_dict: Union[dict, Callable[[str, Tensor], Optional[np.ndarray]]], |
| 498 | strict=True, |
| 499 | ): |
| 500 | r"""Loads a given dictionary created by :func:`state_dict` into this module. |
| 501 | If ``strict`` is ``True``, the keys of :func:`state_dict` must exactly match the keys |
| 502 | returned by :func:`state_dict`. |
| 503 | |
| 504 | Users can also pass a closure: ``Function[key: str, var: Tensor] -> Optional[np.ndarray]`` |
| 505 | as a `state_dict`, in order to handle complex situations. For example, load everything |
| 506 | except for the final linear classifier: |
| 507 | |
| 508 | .. code-block:: |
| 509 | |
| 510 | state_dict = {...} # Dict[str, np.ndarray] |
| 511 | model.load_state_dict({ |
| 512 | k: None if k.startswith('fc') else v |
| 513 | for k, v in state_dict.items() |
| 514 | }, strict=False) |
| 515 | |
| 516 | Here returning ``None`` means skipping parameter ``k``. |
| 517 | |
| 518 | To prevent shape mismatch (e.g. load PyTorch weights), we can reshape before loading: |
| 519 | |
| 520 | .. code-block:: |
| 521 | |
| 522 | state_dict = {...} |
| 523 | def reshape_accordingly(k, v): |
| 524 | return state_dict[k].reshape(v.shape) |
| 525 | model.load_state_dict(reshape_accordingly) |
| 526 | |
| 527 | We can also perform inplace re-initialization or pruning: |
| 528 | |
| 529 | .. code-block:: |
| 530 | |
| 531 | def reinit_and_pruning(k, v): |
| 532 | if 'bias' in k: |
| 533 | M.init.zero_(v) |
| 534 | if 'conv' in k: |
| 535 | |
| 536 | """ |
| 537 | unused = [] |
| 538 | if isinstance(state_dict, dict): |
| 539 | unused = state_dict.keys() |
| 540 | |
| 541 | def closure(k, _): # var unused |
| 542 | return state_dict[k] if k in state_dict else None |
| 543 | |
| 544 | elif callable(state_dict): |
| 545 | closure = state_dict |
| 546 | else: |
| 547 | raise ValueError( |
| 548 | "`state_dict` must load a dict or callable, got {}".format( |
| 549 | type(state_dict) |
| 550 | ) |
| 551 | ) |
| 552 |