arvoelke / nengolib

Nengo library of additional extensions
Other
29 stars 6 forks source link

Add Stochastic- and Poisson-spiking mixins #188

Closed arvoelke closed 4 years ago

arvoelke commented 4 years ago

Supercedes #158.

Some Poisson code here: https://github.com/nengo/nengo/issues/1487

Some stochastic code below (WIP):

class StochasticSpikingMixin:
    """Spiking via stochastic-rounding of some given tuning curve."""

    probeable = ('spikes',)

    # TODO(arvoelke): the simulator should pass in the rng
    rng = np.random.RandomState(seed=0)

    def rates(self, x, gain, bias):
        J = self.current(x, gain, bias)
        output = np.zeros_like(J)
        self._curve(J=J, output=output)
        return output

    def step_math(self, dt, J, spiked):
        rates = np.zeros_like(J)
        self._curve(J=J, output=rates)
        e_spikes = rates * dt
        # https://en.wikipedia.org/wiki/Rounding#Stochastic_rounding
        n_spikes = np.floor(e_spikes)
        n_spikes[self.rng.rand(spiked.size) < e_spikes - n_spikes] += 1
        spiked[...] = n_spikes / dt  # no amplitude scaling

    def _curve(self, J, output):
        raise NotImplementError("_curve must be implemented")
arvoelke commented 4 years ago

Closing in favour of internal solution.