msgpack_from_state_dict

msgpack_from_state_dict#

class braintools.file.msgpack_from_state_dict(target, state, name='.', mismatch='error')[source]#

Restores the state of the given target using a state dict.

This function takes the current target as an argument. This lets us know the exact structure of the target, as well as lets us add assertions that shapes and dtypes don’t change.

In practice, none of the leaf values in target are actually used. Only the tree structure, shapes and dtypes.

Parameters:
  • target (Any) – the object of which the state should be restored.

  • state (Any) – a dictionary generated by to_state_dict with the desired new state for target.

  • name (str) – name of branch taken, used to improve deserialization error messages.

  • mismatch (Literal['error', 'warn', 'ignore']) – How to handle mismatches between target and state dict. ‘error’ (default): raise ValueError on mismatch ‘warn’: issue warning and skip mismatched keys ‘ignore’: silently skip mismatched keys

Returns:

A copy of the object with the restored state.