BernoulliEncoder

Contents

BernoulliEncoder#

class braintools.spike.BernoulliEncoder(scale=1.0, normalize=True, min_val=None, max_val=None)#

Encode inputs using independent Bernoulli processes.

Each input value is converted to a probability \(p = \text{clip}(x \cdot \text{scale}, 0, 1)\), and spikes are generated independently at each time step with that probability. The probability is per step, independent of the time step dt: the same input gives a higher rate in Hz when dt is smaller.

Parameters:
  • scale (float) – Scaling factor for the input-to-probability conversion.

  • normalize (bool) – Whether to rescale inputs to [0, 1] with min_val and max_val; it has no effect unless both are given.

  • min_val (float | None) – Minimum value for normalization.

  • max_val (float | None) – Maximum value for normalization.

See also

RateEncoder

Fires with probability rate * dt for a rate in Hz.

PoissonEncoder

Poisson spike trains from rates in Hz.

Examples

>>> import brainstate
>>> import jax.numpy as jnp
>>> import braintools
>>> brainstate.random.seed(0)
>>> encoder = braintools.spike.BernoulliEncoder()
>>> spikes = encoder(jnp.array([0.1, 0.5, 0.9]), n_time=1000)
>>> spikes.shape
(1000, 3)
>>> p = spikes.mean(axis=0)  # approximately [0.1, 0.5, 0.9] per step