snn.Leaky
- class snntorch._neurons.leaky.Leaky(beta, 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, graded_spikes_factor=1.0, learn_graded_spikes_factor=False, reset_delay=True)[source]
Bases:
LIF
First-order leaky integrate-and-fire neuron model. Input is assumed to be a current injection. Membrane potential decays exponentially with rate 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] = βU[t] + I_{\rm in}[t+1] - 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] = βU[t] + I_{\rm syn}[t+1] - R(βU[t] + I_{\rm in}[t+1])\]\(I_{\rm in}\) - Input current
\(U\) - Membrane potential
\(U_{\rm thr}\) - Membrane threshold
\(R\) - Reset mechanism: if active, \(R = 1\), otherwise \(R = 0\)
\(β\) - Membrane potential decay rate
Example:
import torch import torch.nn as nn import snntorch as snn beta = 0.5 # Define Network class Net(nn.Module): def __init__(self): super().__init__() # initialize layers self.fc1 = nn.Linear(num_inputs, num_hidden) self.lif1 = snn.Leaky(beta=beta) self.fc2 = nn.Linear(num_hidden, num_outputs) self.lif2 = snn.Leaky(beta=beta) 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
- Parameters:
beta (float or torch.tensor) – membrane 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).
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 “subtract”
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
graded_spikes_factor (float or torch.tensor) – output spikes are scaled this value, if specified. Defaults to 1.0
learn_graded_spikes_factor (bool, optional) – Option to enable learnable graded spikes. Defaults to False
reset_delay (bool, optional) – If True, a spike is returned with a one-step delay after the threshold is reached. Defaults to True
- 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:
- Leaky.beta (torch.Tensor) - optional learnable weights must be
manually passed in, of shape 1 or (input_size).
- Leaky.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_leaky()[source]
Deprecated, use
Leaky.reset_mem
instead
Used to clear hidden state variables to zero. Intended for use where hidden state variables are instance variables. Assumes hidden states have a batch dimension already.