MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / load_state_dict

Method load_state_dict

imperative/python/megengine/module/module.py:495–574  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Callers 8

pretrained_model_funcMethod · 0.45
update_modelFunction · 0.45
workerFunction · 0.45
test_save_loadFunction · 0.45
test_state_dictFunction · 0.45
_check_moduleFunction · 0.45

Calls 3

keysMethod · 0.80
formatMethod · 0.45

Tested by 7

update_modelFunction · 0.36
workerFunction · 0.36
test_save_loadFunction · 0.36
test_state_dictFunction · 0.36
_check_moduleFunction · 0.36