unflatten#
- class brainstate.graph.unflatten(graphdef, state_mapping, /, *, index_ref=None, index_ref_cache=None)#
Reconstruct a graph node from a
GraphDefand a state mapping.- Parameters:
graphdef (
GraphDef) – The static structure produced byflatten().state_mapping (
NestedDict) – The dynamic state mapping (or a merge of filtered mappings).index_ref (
dict|None) – Aglobal index -> rebuilt objecttable to accumulate into. Shared across amerge_contextso references resolve across calls.index_ref_cache (
dict|None) – A cache of pre-existing objects keyed by global index; when supplied, matching nodes/states are reused and updated in place (the “update” path) instead of created fresh.
- Returns:
The reconstructed root object.
- Return type:
- Raises:
ValueError – If a referenced state path is missing from the mapping.
Examples
>>> import brainstate >>> model = brainstate.nn.Linear(2, 3) >>> graphdef, states = brainstate.graph.flatten(model) >>> rebuilt = brainstate.graph.unflatten(graphdef, states) >>> isinstance(rebuilt, brainstate.nn.Linear) True