class snntorch._neurons.slstm.SLSTM(input_size, hidden_size, bias=True, threshold=1.0, spike_grad=None, surrogate_disable=False, init_hidden=False, inhibition=False, learn_threshold=False, reset_mechanism='none', state_quant=False, output=False)[source]

Bases: SpikingNeuron

A spiking long short-term memory cell. Hidden states are membrane potential and synaptic current \(mem, syn\), which correspond to the hidden and cell states \(h, c\) in the original LSTM formulation.

The input is expected to be of size \((N, X)\) where \(N\) is the batch size.

Unlike the LSTM module in PyTorch, only one time step is simulated each time the cell is called.

\[\begin{split}\begin{array}{ll} \\ i_t = \sigma(W_{ii} x_t + b_{ii} + W_{hi} mem_{t-1} + b_{hi}) \\ f_t = \sigma(W_{if} x_t + b_{if} + W_{hf} mem_{t-1} + b_{hf}) \\ g_t = \tanh(W_{ig} x_t + b_{ig} + W_{hg} mem_{t-1} + b_{hg}) \\ o_t = \sigma(W_{io} x_t + b_{io} + W_{ho} mem_{t-1} + b_{ho}) \\ syn_t = f_t ∗ syn_{t-1} + i_t ∗ g_t \\ mem_t = o_t ∗ \tanh(syn_t) \\ \end{array}\end{split}\]

where \(\sigma\) is the sigmoid function and ∗ is the Hadamard product. The output state \(mem_{t+1}\) is thresholded to determine whether an output spike is generated. To conform to standard LSTM state behavior, the default reset mechanism is set to reset=”none”, i.e., no reset is applied. If this is changed, the reset is only applied to \(h_t\).


import torch
import torch.nn as nn
import snntorch as snn

beta = 0.5

# Define Network
class Net(nn.Module):
    def __init__(self):

        num_inputs = 784
        num_hidden1 = 1000
        num_hidden2 = 10

        spike_grad_lstm = surrogate.straight_through_estimator()

        # initialize layers
        self.slstm1 = snn.SLSTM(num_inputs, num_hidden1,
        self.slstm2 = snn.SLSTM(num_hidden1, num_hidden2,

    def forward(self, x):
        # Initialize hidden states and outputs at t=0
        syn1, mem1 = self.slstm1.init_slstm()
        syn2, mem2 = self.slstm2.init_slstm()

        # Record the final layer
        spk2_rec = []
        mem2_rec = []

        for step in range(num_steps):
            spk1, syn1, mem1 = self.slstm1(x.flatten(1), syn1, mem1)
            spk2, syn2, mem2 = self.slstm2(spk1, syn2, mem2)


        return torch.stack(spk2_rec), torch.stack(mem2_rec)
  • input_size (int) – number of expected features in the input \(x\)

  • hidden_size (int) – the number of features in the hidden state \(mem\)

  • bias (bool, optional) – If True, adds a learnable bias to the output. Defaults to True

  • threshold (float, optional) – Threshold for \(h\) 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 ATan surrogate gradient

  • surrogate_disable (bool, Optional) – Disables surrogate gradients regardless of spike_grad argument. Useful for ONNX compatibility. Defaults to False

  • learn_threshold (bool, optional) – Option to enable learnable threshold. 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

  • 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 states \(mem\) and \(syn\) are 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_, syn_0, mem_0
  • input_ of shape (batch, input_size): tensor containing input features

  • syn_0 of shape (batch, hidden_size): tensor containing the initial synaptic current (or cell state) for each element in the batch.

  • mem_0 of shape (batch, hidden_size): tensor containing the initial membrane potential (or hidden state) for each element in the batch.

Outputs: spk, syn_1, mem_1
  • spk of shape (batch, hidden_size): tensor containing the output spike

  • syn_1 of shape (batch, hidden_size): tensor containing the next synaptic current (or cell state) for each element in the batch

  • mem_1 of shape (batch, hidden_size): tensor containing the next membrane potential (or hidden state) for each element in the batch

Learnable Parameters:
  • SLSTM.lstm_cell.weight_ih (torch.Tensor) - the learnable input-hidden weights, of shape (4*hidden_size, input_size)

  • SLSTM.lstm_cell.weight_ih (torch.Tensor) – the learnable hidden-hidden weights, of shape (4*hidden_size, hidden_size)

  • SLSTM.lstm_cell.bias_ih – the learnable input-hidden bias, of shape (4*hidden_size)

  • SLSTM.lstm_cell.bias_hh – the learnable hidden-hidden bias, of shape (4*hidden_size)

classmethod detach_hidden()[source]

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_, syn=None, mem=None)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.


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.


Deprecated, use SLSTM.reset_mem instead

classmethod reset_hidden()[source]

Used to clear hidden state variables to zero. Intended for use where hidden state variables are instance variables.