HiddenPerturbation#
- class braintrace.HiddenPerturbation#
The hidden-perturbation information.
Hidden perturbation adds a perturbation variable to each hidden state in the jaxpr and replaces the hidden states with the perturbed states:
\[h^t = f(x) \;\Rightarrow\; h^t = f(x) + \mathrm{perturb\_var},\]where \(h\) is the hidden state, \(f\) is the function, \(x\) is the input, and \(\mathrm{perturb\_var}\) is the perturbation variable.
- Variables:
perturb_vars (sequence of Var) – The perturbation variables.
perturb_hidden_paths (sequence of Path) – The hidden-state paths that are perturbed.
perturb_hidden_states (sequence of brainstate.HiddenState) – The hidden states that are perturbed.
perturb_jaxpr (ClosedJaxpr) – The perturbed jaxpr.
new_read_cuts (sequence of (Path, Path)) – The in-step reads
(k, j), by the update ofh_j, of the new value ofh_kthat the learning signal holds fixed, because every eligibility trace paired withh_k’s signal already carries them, within the step and across steps (see the Notes). Empty when nothing is cut.new_read_conflicts (sequence of str) – Why some in-step reads could not be cut: the relations disagree, the traces are not proven to carry a read across steps (it crosses hidden groups, or goes through an equation the group transition leaves out or one not proven position-preserving, i.e. not elementwise), or the reads cannot be emitted apart. A conflict blocks its sources: the signal of a blocked source is the total derivative, which counts some in-step paths twice, and the graph carries a
HIDDEN_NEW_READ_DOUBLE_COUNTrecord (also warned). The reads of the other sources are still cut (#248).new_read_exact (bool) –
Trueiff the reads were planned and no conflict was found: then no in-step read of a new hidden value is double counted in the single-step learning signal. It does not say that the single-step gradient is exact: the approximations of the learner itself (a diagonal trace of a coupled recurrence, a path the traces do not cover) are separate.Falsewhen the reads were not planned (no relations, as inadd_hidden_perturbation_in_module()) or a conflict was found.
See also
add_hidden_perturbation_in_moduleBuild perturbations directly from a model.
Notes
Internally a new variable \(\hat{h}^t = f(x)\) is defined and an extra equation \(h^t = \hat{h}^t + \mathrm{perturb\_var}\) is added. The perturbation lets the hidden-state gradient be read off the perturbation variable
\[\frac{\partial L^t}{\partial h^t} = \frac{\partial L^t}{\partial \mathrm{perturb\_var}}.\]Every reader of \(h^t_k\) in the step sees the perturbed value, so this is the total derivative: it includes the update of another hidden state \(h^t_j\) that reads the new \(h^t_k\), \(s_k = \partial_k L + J_{jk}^\top s_j\) with \(J_{jk} = \partial \hat{h}^t_j / \partial h^t_k\). When the graph is compiled with its relations, a read that every trace paired with \(s_k\) already carries is cut (
new_read_cuts): it sees \(\hat{h}^t_k\) instead, and \(s_k\) is the partial derivative with that read held fixed. Otherwise the path through the read would be counted twice, once in \(s_j e_j\) and once in \(s_k e_k\) (issue #239). A trace carries a read when they -> hiddenjaxpr of \(h_j\)’s group walks through it (within the step) and the transition of that group holds it too (across steps): \(h_k\) and \(h_j\) share the group, and every equation on the read is one the transition computes (HiddenGroup.carried_vars()) and is proven position-preserving (elementwise; seeNewReadPolicy.why_not_carried()). A read the relations would cut but the transition does not provably carry is a conflict. An equation that serves both a cut read and the outputs is emitted twice, once for each.Examples
>>> import brainstate >>> import braintrace >>> gru = braintrace.nn.GRUCell(3, 4) >>> _ = brainstate.nn.init_all_states(gru) >>> inputs = brainstate.random.randn(3) >>> hidden_perturb = braintrace.add_hidden_perturbation_in_module(gru, inputs) >>> isinstance(hidden_perturb, braintrace.HiddenPerturbation) True
- static __new__(_cls, perturb_vars, perturb_hidden_paths, perturb_hidden_states, perturb_jaxpr, bypass_param_paths=(), bypass_vars=(), bypass_hidden_paths=(), bypass_hidden_vars=(), new_read_cuts=(), new_read_conflicts=(), new_read_exact=False)#
Create new instance of HiddenPerturbation(perturb_vars, perturb_hidden_paths, perturb_hidden_states, perturb_jaxpr, bypass_param_paths, bypass_vars, bypass_hidden_paths, bypass_hidden_vars, new_read_cuts, new_read_conflicts, new_read_exact)
- bypass_hidden_data_to_hidden_group_data(bypass_hidden_data, hidden_groups)#
Assemble per-group arrays from per-hidden bypass data.
- Parameters:
bypass_hidden_data (sequence of jax.Array) – One array per entry of
bypass_hidden_vars(typically the cotangentsdL^t/dh^{t-1}along the bypass paths).hidden_groups (sequence of HiddenGroup) – The hidden groups to assemble, as in
perturb_data_to_hidden_group_data().
- Returns:
sequence of jax.Array – One concatenated array per hidden group; a perturbed hidden state without a bypass contributes zeros.
- Raises:
ValueError – If
bypass_hidden_datadoes not have the same length asbypass_hidden_vars.
- bypass_hidden_paths#
Alias for field number 6
- bypass_hidden_vars#
Alias for field number 7
- bypass_param_paths#
Alias for field number 4
- bypass_vars#
Alias for field number 5
- dict()#
Return this perturbation’s named fields as a plain dictionary.
- Returns:
dict – An ordered mapping from field name to value, as produced by the underlying
typing.NamedTuple.
- eval_jaxpr(inputs, perturb_data, bypass_data=(), bypass_hidden_data=())#
Evaluate the perturbed jaxpr.
- Parameters:
inputs (sequence of jax.Array) – The flat input values of the original jaxpr.
perturb_data (sequence of jax.Array) – The perturbation values, one per entry of
perturb_vars.bypass_data (sequence of jax.Array, optional) – The values of the bypass parameter copies, one per entry of
bypass_vars(the leaves of eachbypass_param_pathsvalue). Differentiating the outputs with respect to them gives exactly the parameters’ same-step bypass gradient. Defaults to none.bypass_hidden_data (sequence of jax.Array, optional) – The previous values of the
bypass_hidden_pathshidden states, one per entry ofbypass_hidden_vars. Differentiating the outputs with respect to them gives exactly the bypass learning signaldL^t/dh^{t-1}. Defaults to none.
- Returns:
sequence of jax.Array – The outputs of the perturbed jaxpr.
- init_perturb_data()#
Initialize the perturbation data to zeros.
- Returns:
sequence of jax.Array – One zero array per perturbation variable, matching its shape and dtype.
- new_read_conflicts#
Alias for field number 9
- new_read_cuts#
Alias for field number 8
- new_read_exact#
Alias for field number 10
- perturb_data_to_hidden_group_data(perturb_data, hidden_groups)#
Convert the perturbation data to per-hidden-group data.
- Parameters:
perturb_data (sequence of jax.Array) – The perturbation values, one per entry of
perturb_vars.hidden_groups (sequence of HiddenGroup) – The hidden groups to map the perturbation data onto.
- Returns:
sequence of jax.Array – One concatenated perturbation array per hidden group.
- Raises:
ValueError – If
perturb_datadoes not have the same length asperturb_vars, or if a hidden group needs a path that was not perturbed.
Notes
Both guards are
if ... raiserather thanassertso thatpython -Ocannot strip them. The mapping from paths to perturbation data is positional (zip(perturb_hidden_paths, perturb_data)), so a length disagreement silently re-attributes every cotangent past the mismatch; and a group asking for an unperturbed path used to surface as a bareKeyErrornaming a path tuple and nothing else. Both checks read only Python-level lengths and dict keys, never array data.
- perturb_hidden_paths#
Alias for field number 1
- perturb_hidden_states#
Alias for field number 2
- perturb_jaxpr#
Alias for field number 3
- perturb_vars#
Alias for field number 0
- HiddenPerturbation.__init__()#