brainevent.csr_apply_on_pre

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 with shape = (n_pre, n_post), evaluate

new_e = rule(edge_state[e], pre_vals[i], post_vals[j], params)

and return new_e where presynaptic neuron i is active and the unchanged edge_state[e] elsewhere. A numeric spike is active iff it is non-zero (negative values and NaN are active); a boolean spike iff True.

Parameters:
  • rule (Callable[[Any, Any, Any, Any], Any]) – rule(edge_e, pre_e, post_e, params) -> new_edge_e. edge_e, pre_e and post_e are the pytrees edge_state, pre_vals and post_vals indexed at one edge (leading dimension removed); params is passed unchanged. It must return a pytree with the structure of edge_state and is applied to every edge with jax.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. None passes None to rule.

  • post_vals (pytree or None, optional) – Per-postsynaptic-neuron values (leading dimension n_post), gathered to edges by column. None passes None to rule.

  • params (pytree or None, optional) – Rule parameters, broadcast to every edge (differentiable).

  • shape (Tuple[int, int]) – Matrix shape (n_pre, n_post).

Returns:

The new edge state, with the structure of edge_state and leaves of the same shape and dtype.

Return type:

pytree

Raises:
  • TypeError – If rule returns a pytree whose structure differs from edge_state.

  • ValueError – If a leaf of edge_state, pre_vals or post_vals has the wrong leading dimension, or pre_spike / indptr do not match shape.

See also

csr_apply_on_post

Postsynaptic-spike-triggered counterpart.

update_csr_on_binary_pre

Fast event-driven kernel for the additive rule w += post_trace.

CSR.apply_on_pre

Method form on a CSR matrix.

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 prefer update_csr_on_binary_pre().

Gradients flow through rule, edge_state, pre_vals, post_vals and params; spikes and structure are non-differentiable. As with any jax.numpy.where(), inactive edges send a zero cotangent into rule; if the rule’s derivative is not finite there (e.g. log(0)) the gradient becomes nan.

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)