snn.Lapicque
- class snntorch._neurons.lapicque.Lapicque(beta=False, R=False, C=False, time_step=1, threshold=1.0, spike_grad=None, surrogate_disable=False, init_hidden=False, inhibition=False, learn_beta=False, learn_threshold=False, reset_mechanism='subtract', state_quant=False, output=False)[source]
Bases:
LIF
An extension of Lapicque’s experimental comparison between extracellular nerve fibers and an RC circuit. It is qualitatively equivalent to
Leaky
but defined using RC circuit parameters. Input stimulus is integrated by membrane potential which decays exponentially with a rate of beta. For \(U[T] > U_{\rm thr} ⇒ S[T+1] = 1\).If reset_mechanism = “subtract”, then \(U[t+1]\) will have threshold subtracted from it whenever the neuron emits a spike:
\[U[t+1] = I_{\rm in}[t+1] (\frac{T}{C}) + (1- \frac{T}{\tau})U[t] - RU_{\rm thr}\]If reset_mechanism = “zero”, then \(U[t+1]\) will be set to 0 whenever the neuron emits a spike:
\[U[t+1] = I_{\rm in}[t+1] (\frac{T}{\tau}) + (1- \frac{T}{\tau})U[t] - R(I_{\rm in}[t+1] (\frac{T}{C}) + (1- \frac{T}{\tau})U[t])\]\(I_{\rm in}\) - Input current
\(U\) - Membrane potential
\(U_{\rm thr}\) - Membrane threshold
\(T\)- duration of each time step
\(R\) - Reset mechanism: if active, \(R = 1\), otherwise \(R = 0\)
\(β\) - Membrane potential decay rate. Alternatively, the membrane potential decay rate β can be specified instead:
\[β = e^{-1/RC}\]\(R\) - Parallel resistance of passive membrane (note: distinct from the reset \(R\))
\(C\) - Parallel capacitance of passive membrane
Notes:
If only β is defined, then R will default to 1, and C will be inferred.
If RC is defined, β will be automatically calculated.
If (β and R) or (β and C) are defined, the missing variable will be automatically calculated.
Note that β, R and C are treated as ‘hard-wired’ physically plausible parameters, and are therefore not learnable. For a single-state neuron with a learnable decay rate β, use
snn.Leaky
instead.
Example:
import torch import torch.nn as nn import snntorch as snn beta = 0.5 R = 1 C = 1.44 # Define Network class Net(nn.Module): def __init__(self): super().__init__() # initialize layers self.fc1 = nn.Linear(num_inputs, num_hidden) self.lif1 = snn.Lapicque(beta=beta) self.fc2 = nn.Linear(num_hidden, num_outputs) self.lif2 = snn.Lapicque(R=R, C=C) # lif1 and lif2 are approximately equivalent def forward(self, x, mem1, spk1, mem2): cur1 = self.fc1(x) spk1, mem1 = self.lif1(cur1, mem1) cur2 = self.fc2(spk1) spk2, mem2 = self.lif2(cur2, mem2) return mem1, spk1, mem2, spk2
For further reading, see:
L. Lapicque (1907) Recherches quantitatives sur l’excitation électrique des nerfs traitée comme une polarisation. J. Physiol. Pathol. Gen. 9, pp. 620-635. (French)
N. Brunel and M. C. Van Rossum (2007) Lapicque’s 1907 paper: From frogs to integrate-and-fire. Biol. Cybern. 97, pp. 337-339. (English)
Although Lapicque did not formally introduce this as an integrate-and-fire neuron model, we pay homage to his discovery of an RC circuit mimicking the dynamics of synaptic current.
- Parameters:
beta (float or torch.tensor, Optional) – RC potential decay rate. Clipped between 0 and 1 during the forward-pass. May be a single-valued tensor (i.e., equal decay rate for all neurons in a layer), or multi-valued (one weight per neuron).
R (int or torch.tensor, Optional) – Resistance of RC circuit
C (int or torch.tensor, Optional) – Capacitance of RC circuit
time_step (float, Optional) – time step precision. Defaults to 1
threshold (float, optional) – Threshold for \(mem\) to reach in order to generate a spike S=1. Defaults to 1
spike_grad (surrogate gradient function from snntorch.surrogate, optional) – Surrogate gradient for the term dS/dU. Defaults to None (corresponds to ATan surrogate gradient. See snntorch.surrogate for more options)
surrogate_disable (bool, Optional) – Disables surrogate gradients regardless of spike_grad argument. Useful for ONNX compatibility. Defaults to False
init_hidden (bool, optional) – Instantiates state variables as instance variables. Defaults to False
inhibition (bool, optional) – If True, suppresses all spiking other than the neuron with the highest state. Defaults to False
learn_beta (bool, optional) – Option to enable learnable beta. Defaults to False
learn_threshold (bool, optional) – Option to enable learnable threshold. Defaults to False
reset_mechanism (str, optional) – Defines the reset mechanism applied to \(mem\) each time the threshold is met. Reset-by-subtraction: “subtract”, reset-to-zero: “zero”, none: “none”. Defaults to “none”
state_quant (quantization function from snntorch.quant, optional) – If specified, hidden state \(mem\) is quantized to a valid state for the forward pass. Defaults to False
output (bool, optional) – If True as well as init_hidden=True, states are returned when neuron is called. Defaults to False
- Inputs: input_, mem_0
input_ of shape (batch, input_size): tensor containing
input features - mem_0 of shape (batch, input_size): tensor containing the initial membrane potential for each element in the batch.
- Outputs: spk, mem_1
spk of shape (batch, input_size): tensor containing the
output spikes. - mem_1 of shape (batch, input_size): tensor containing the next membrane potential for each element in the batch
- Learnable Parameters:
Lapcique.beta (torch.Tensor) - optional learnable weights must
be manually passed in, of shape 1 or (input_size). - Lapcique.threshold (torch.Tensor) - optional learnable thresholds must be manually passed in, of shape 1 or`` (input_size).
Returns the hidden states, detached from the current graph. Intended for use in truncated backpropagation through time where hidden state variables are instance variables.
- forward(input_, mem=None)[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_lapicque()[source]
Deprecated, use
Lapicque.reset_mem
instead
Used to clear hidden state variables to zero. Intended for use where hidden state variables are instance variables.