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, and lets us assert that array leaves keep their shape (see mismatch).

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

Note

For brainstate.State leaves the restore is performed in place: the template State object passed in target has its .value mutated and is returned (rather than copied). Pass a throwaway template if you need to keep the original untouched.

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 (including array shape mismatches). ‘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 (except for State leaves, which are restored in place; see note above).