snn.neuron_from_equations

class snntorch._neurons.customneuron.CustomNeuron(threshold=1.0, spike_grad=None, surrogate_disable=False, init_hidden=False, inhibition=False, learn_threshold=False, reset_mechanism='subtract', state_quant=False, output=False, graded_spikes_factor=1.0, learn_graded_spikes_factor=False)[source]

Bases: SpikingNeuron

Base class for a neuron generated by neuron_from_equations(). Not normally instantiated directly – call neuron_from_equations() to build a subclass from your own state equations, then use the returned class exactly like any other snnTorch neuron.

forward(input_, *states)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

init_values: dict = {}

initial value for each state variable (defaults to 0.0).

reset_fn = None

optional fn(states, spk, threshold) -> states applied after a firing decision, INSTEAD of reset_mechanism. Use this when a model’s reset doesn’t fit “subtract” / “zero” / “none” (e.g. Izhikevich’s v <- c, u <- u + d).

reset_state()[source]

Zero every state variable. Call once per sequence.

spike_state = None

which state variable is compared against threshold to fire. Defaults to state_names[0].

state_names: tuple = ()

names of the state variables this neuron carries, e.g. ("mem",) or ("v", "u"). Set by neuron_from_equations().

static update_fn(states, input_)[source]

fn(states: dict, input_: Tensor) -> dict. Must be supplied via neuron_from_equations(); computes every state variable’s next value from the current states and this step’s input.

snntorch._neurons.customneuron.neuron_from_equations(name, state_names, update_fn, spike_state=None, reset_fn=None, init_values=None)[source]

Generate a new spiking-neuron class from user-supplied state equations. The returned class is used exactly like any other snnTorch neuron – init_hidden, output, threshold, spike_grad, reset_mechanism, etc. all work.

Example – a two-state Izhikevich-style neuron:

import snntorch as snn

def izh_update(states, I):
    v, u = states["v"], states["u"]
    dv = 0.04 * v ** 2 + 5 * v + 140 - u + I
    du = 0.02 * (0.2 * v - u)
    return {"v": v + dv, "u": u + du}

def izh_reset(states, spk, threshold):
    v, u = states["v"], states["u"]
    fired = spk.detach().bool()
    v = torch.where(fired, torch.full_like(v, -65.0), v)
    u = torch.where(fired, u + 8.0, u)
    return {"v": v, "u": u}

Izhikevich = snn.neuron_from_equations(
    "Izhikevich", state_names=("v", "u"), update_fn=izh_update,
    spike_state="v", reset_fn=izh_reset, init_values={"v": -65.0},
)

lif = Izhikevich(threshold=30.0)
spk, (v, u) = lif(I), None  # first call auto-initialises state
# or, matching every other neuron's calling convention:
spk, v, u = lif(I)
Parameters:
  • name (str) – class name for the generated neuron (used for type.__name__ / repr; purely cosmetic).

  • state_names (tuple of str) – names of the state variables to carry between steps, e.g. ("mem",) or ("v", "u").

  • update_fn (callable) – fn(states: dict[str, Tensor], input_: Tensor) -> dict[str, Tensor]. Computes every state’s next value from the current states and this step’s input. Must return a value for every name in state_names.

  • spike_state (str, optional) – which state variable is compared against threshold to decide whether the neuron fires. Defaults to state_names[0].

  • reset_fn (callable, optional) – fn(states, spk, threshold) -> states, applied after the firing decision, in place of the standard reset_mechanism. Use this when the model’s reset rule isn’t a plain subtract/zero of spike_state (e.g. Izhikevich’s v <- c, u <- u + d). If None (default), the usual reset_mechanism (“subtract” / “zero” / “none”) is applied to spike_state, exactly as snntorch.Leaky does to mem.

  • init_values (dict, optional) – initial value for each state variable (default 0.0 for any not listed). Applied the first time the neuron is called, or after CustomNeuron.reset_state().

Returns:

a new subclass of CustomNeuron, ready to instantiate with the standard SpikingNeuron keyword arguments (threshold, spike_grad, init_hidden, output, reset_mechanism, inhibition, state_quant, graded_spikes_factor, …).

Return type:

type