RateEncoder#
- class braintools.spike.RateEncoder(gain=100.0, method='linear', min_rate=0.0, max_rate=None, normalize=False, min_val=None, max_val=None)#
Encode analog values into spike rates using various rate encoding methods.
The rate encoder converts continuous input values into spike trains where the firing rate grows with the input intensity. An input \(x\) is mapped to a rate between
min_rateandmax_rate(in Hz), and every time step fires independently with probability \(\text{rate} \cdot dt\) (clipped to[0, 1]), where \(dt\) is thebrainstate.environtime step.- Parameters:
gain (
float) – Firing rate (Hz) for an input of 1; used asmax_ratewhen that is not given.method (
str) – How the input is mapped to a rate (see Notes).min_rate (
float) – Minimum firing rate in Hz.max_rate (
float|None) – Maximum firing rate in Hz. Defaults togain.normalize (
bool) – Whether to rescale inputs to[0, 1]withmin_valandmax_val(both must be given).
- Raises:
ValueError – If
methodis not one of'linear','exponential'or'sqrt'.
See also
PoissonEncoderTakes rates in Hz directly.
BernoulliEncoderSets the spike probability per time step directly.
Notes
With \(r_0\) =
min_rateand \(r_1\) =max_rate:'linear': \(r = r_0 + x\,(r_1 - r_0)\).'exponential': \(r = r_0 + \frac{e^x - 1}{e - 1}\,(r_1 - r_0)\).'sqrt': \(r = r_0 + \sqrt{x}\,(r_1 - r_0)\).
Examples
>>> import brainstate >>> import brainunit as u >>> import jax.numpy as jnp >>> import braintools >>> brainstate.environ.set(dt=1. * u.ms) >>> brainstate.random.seed(0) >>> encoder = braintools.spike.RateEncoder(gain=100., method='linear') >>> spikes = encoder(jnp.array([0.1, 0.5, 0.9]), n_time=10000) >>> spikes.shape (10000, 3) >>> # The mean is a probability per step; divide by dt (in s) for Hz. >>> rates = spikes.mean(axis=0) / 1e-3 # approximately [10, 50, 90] Hz