brainevent.csr_apply_on_pre#
- brainevent.csr_apply_on_pre(rule, edge_state, indices, indptr, pre_spike, *, pre_vals=None, post_vals=None, params=None, shape)[source]#
Apply a per-edge plasticity rule to the edges of active presynaptic neurons.
For every stored edge
e = (i, j)of a CSR matrix withshape = (n_pre, n_post), evaluatenew_e = rule(edge_state[e], pre_vals[i], post_vals[j], params)and return
new_ewhere presynaptic neuroniis active and the unchangededge_state[e]elsewhere. A numeric spike is active iff it is non-zero (negative values and NaN are active); a boolean spike iffTrue.- Parameters:
rule (
Callable[[Any,Any,Any,Any],Any]) –rule(edge_e, pre_e, post_e, params) -> new_edge_e.edge_e,pre_eandpost_eare the pytreesedge_state,pre_valsandpost_valsindexed at one edge (leading dimension removed);paramsis passed unchanged. It must return a pytree with the structure ofedge_stateand is applied to every edge withjax.vmap().edge_state (pytree) – Per-edge state in CSR data order; every leaf has leading dimension
nse(trailing dimensions are allowed), e.g.{'w': w, 'elig': elig}.indices (jax.Array) – CSR column indices, shape
(nse,).indptr (jax.Array) – CSR row pointers, shape
(n_pre + 1,).pre_spike (
Array) – Presynaptic spikes (bool or numeric), shape(n_pre,).pre_vals (pytree or None, optional) – Per-presynaptic-neuron values (leading dimension
n_pre), gathered to edges by row.NonepassesNonetorule.post_vals (pytree or None, optional) – Per-postsynaptic-neuron values (leading dimension
n_post), gathered to edges by column.NonepassesNonetorule.params (pytree or None, optional) – Rule parameters, broadcast to every edge (differentiable).
- Returns:
The new edge state, with the structure of
edge_stateand leaves of the same shape and dtype.- Return type:
pytree
- Raises:
TypeError – If
rulereturns a pytree whose structure differs fromedge_state.ValueError – If a leaf of
edge_state,pre_valsorpost_valshas the wrong leading dimension, orpre_spike/indptrdo not matchshape.
See also
csr_apply_on_postPostsynaptic-spike-triggered counterpart.
update_csr_on_binary_preFast event-driven kernel for the additive rule
w += post_trace.CSR.apply_on_preMethod form on a
CSRmatrix.
Notes
The rule is evaluated on every edge and the result is masked, so the cost is
O(nse)per call independent of the number of spikes; for the plain additive rule preferupdate_csr_on_binary_pre().Gradients flow through
rule,edge_state,pre_vals,post_valsandparams; spikes and structure are non-differentiable. As with anyjax.numpy.where(), inactive edges send a zero cotangent intorule; if the rule’s derivative is not finite there (e.g.log(0)) the gradient becomesnan.Examples
>>> import jax.numpy as jnp >>> import brainevent >>> csr = brainevent.CSR.fromdense(jnp.array([[1., 0.5], [0., 2.]])) >>> def rule(edge, pre, post, p): ... w = edge['w'] + p['lr'] * (p['w_max'] - edge['w']) * post ... return {'w': w, 'elig': edge['elig'] * 0.9 + post} >>> state = {'w': csr.data, 'elig': jnp.zeros_like(csr.data)} >>> new = brainevent.csr_apply_on_pre( ... rule, state, csr.indices, csr.indptr, jnp.array([True, False]), ... post_vals=jnp.array([1., 0.5]), params={'lr': 0.1, 'w_max': 3.}, ... shape=csr.shape, ... ) >>> new['w'] Array([1.2 , 0.625, 2. ], dtype=float32)