Converting an ANN to an SNN#

This tutorial walks you through how to convert your pre-trained model to a spiking version. Lets start by installing all the necessary packages.

%pip install --quiet torchvision
Note: you may need to restart the kernel to use updated packages.

Defining an ANN#

We define a simple convolutional architecture.

import torch.nn as nn

ann = nn.Sequential(
    nn.Conv2d(1, 20, 5, 1, bias=False),
    nn.AvgPool2d(2, 2),
    nn.Conv2d(20, 32, 5, 1, bias=False),
    nn.AvgPool2d(2, 2),
    nn.Conv2d(32, 128, 3, 1, bias=False),
    nn.AvgPool2d(2, 2),
    nn.Linear(128, 500, bias=False),
    nn.Linear(500, 10, bias=False),
/home/docs/checkouts/ TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See
  from .autonotebook import tqdm as notebook_tqdm

Define a custom dataset#

We’ll need to fine-tune our model on our dataset of choice. Here we’ll use MNIST. Since we intend to do a spiking neural network simulation, we override this Dataset to also optionally return a spike raster instead of an image.

In this implementation of the Dataset we use rate coding to generate a series of spikes at each pixel of the image proportional to it’s gray level.

import torch
from torchvision import datasets, transforms

class MNIST(datasets.MNIST):
    def __init__(self, root, train=True, is_spiking=False, time_window=100):
            root=root, train=train, download=True, transform=transforms.ToTensor()
        self.is_spiking = is_spiking
        self.time_window = time_window

    def __getitem__(self, index):
        img, target =[index].unsqueeze(0) / 255, self.targets[index]
        # img is now a tensor of 1x28x28

        if self.is_spiking:
            img = (torch.rand(self.time_window, *img.shape) < img).float()

        return img, target

Fine-tune the ANN#

We’ll make sure that classification accuracy is high enough with this model on MNIST. Note here that we are not yet using spiking input (spiking=False). This is vanilla training for standard image classification.

from import DataLoader

mnist_train = MNIST("./data", train=True, is_spiking=False)
train_loader = DataLoader(mnist_train, batch_size=128, shuffle=True)

mnist_test = MNIST("./data", train=False, is_spiking=False)
test_loader = DataLoader(mnist_test, batch_size=128, shuffle=False)

We iterate over our data loader train_loader and train our parameters using the Adam optimizer with a learning rate of 1e-4. Since the last layer in our network has no specific activation function defined, cross_entropy loss is a good candidate to train our network.

from import tqdm
import torch
import torch.nn.functional as F

device = "cuda" if torch.cuda.is_available() else "cpu"

ann =

optim = torch.optim.Adam(ann.parameters(), lr=1e-3)

n_epochs = 2

for n in tqdm(range(n_epochs)):
    for data, target in iter(train_loader):
        data, target =,
        output = ann(data)

        loss = F.cross_entropy(output, target)
  0%|          | 0/2 [00:00<?, ?it/s]
 50%|█████     | 1/2 [00:20<00:20, 20.35s/it]
100%|██████████| 2/2 [00:41<00:00, 20.59s/it]
100%|██████████| 2/2 [00:41<00:00, 20.55s/it]

correct_predictions = []

for data, target in iter(test_loader):
    data, target =,
    output = ann(data)

    # get the index of the max log-probability
    pred = output.argmax(dim=1, keepdim=True)

    # Compute the total correct predictions

correct_predictions =
    f"Classification accuracy: {correct_predictions.sum().item()/(len(correct_predictions))*100}%"
Classification accuracy: 97.87%

Training this model on MNIST is fairly straight forward and you should reach accuracies of around >98% within a small number of epochs. In the script above we only train for 3 epochs!

Model conversion to SNN#

Up until this point we have only operated on images using standard CNN architectures. Now we look at how to build an equivalent spiking convolutional neural network (SCNN).

sinabs has a handy method for this. Given a standard CNN model, the from_model method in sinabs that converts it into a spiking neural network. It is a one liner!

from sinabs.from_torch import from_model

input_shape = (1, 28, 28)

sinabs_model = from_model(
    ann, input_shape=input_shape, add_spiking_output=True, synops=False

You can see that this method takes two more parameters in addition to the model to be converted.

input_shape is needed in order to instantiate a SNN with the appropriate number of neurons because unlike traditional CNNs, SNNs are stateful.

add_spiking_output is a boolean flag to specify whether or not to add a spiking layer as the last layer in the network. This ensure that both the input and output to our network are of the form of spikes.

synops=True tells sinabs to include the machinery for calculating synaptic operations, which we’ll use later.

Let us now look at the generated SCNN. You should see that the only major difference is that the ReLU layers are replaced by SpikingLayer.

  (0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1), bias=False)
  (1): IAFSqueeze(spike_threshold=1.0, min_v_mem=-1.0)
  (2): AvgPool2d(kernel_size=2, stride=2, padding=0)
  (3): Conv2d(20, 32, kernel_size=(5, 5), stride=(1, 1), bias=False)
  (4): IAFSqueeze(spike_threshold=1.0, min_v_mem=-1.0)
  (5): AvgPool2d(kernel_size=2, stride=2, padding=0)
  (6): Conv2d(32, 128, kernel_size=(3, 3), stride=(1, 1), bias=False)
  (7): IAFSqueeze(spike_threshold=1.0, min_v_mem=-1.0)
  (8): AvgPool2d(kernel_size=2, stride=2, padding=0)
  (9): Flatten(start_dim=1, end_dim=-1)
  (10): Linear(in_features=128, out_features=500, bias=False)
  (11): IAFSqueeze(spike_threshold=1.0, min_v_mem=-1.0)
  (12): Linear(in_features=500, out_features=10, bias=False)
  (Spiking output): IAFSqueeze(spike_threshold=1.0, min_v_mem=-1.0)

Model validation in sinabs simulation#

Lets test our SCNN model to verify whether the network is in fact “equivalent” to the CNN model in terms of its performance. As we did previously, we start by defining a data loader (this time it is going to produce spikes, spiking=True) and then pass it to our test method.

# Time window per sample
time_window = 100  # time steps
test_batch_size = 10

spike_mnist_test = MNIST(
    "./data", train=False, is_spiking=True, time_window=time_window
spike_test_loader = DataLoader(
    spike_mnist_test, batch_size=test_batch_size, shuffle=True

Since the spiking simulations are significantly slower on a PC, we are going to limit our test to 300 samples here. You can of course test it on the entire 10k samples if you want to verify that it works.

import sinabs.layers as sl

correct_predictions = []

for data, target in tqdm(spike_test_loader):
    data, target =,
    data = sl.FlattenTime()(data)
    with torch.no_grad():
        output = sinabs_model(data)
        output = output.unflatten(
            0, (test_batch_size, output.shape[0] // test_batch_size)

    # get the index of the max log-probability
    pred = output.sum(1).argmax(dim=1, keepdim=True)

    # Compute the total correct predictions
    if len(correct_predictions) * test_batch_size >= 300:

correct_predictions =
    f"Classification accuracy: {correct_predictions.sum().item()/(len(correct_predictions))*100}%"
  0%|          | 0/1000 [00:00<?, ?it/s]
  0%|          | 1/1000 [00:00<12:28,  1.33it/s]
  0%|          | 2/1000 [00:01<12:11,  1.36it/s]
  0%|          | 3/1000 [00:02<12:04,  1.38it/s]
  0%|          | 4/1000 [00:02<12:02,  1.38it/s]
  0%|          | 5/1000 [00:03<12:00,  1.38it/s]
  1%|          | 6/1000 [00:04<11:59,  1.38it/s]
  1%|          | 7/1000 [00:05<11:56,  1.38it/s]
  1%|          | 8/1000 [00:05<11:55,  1.39it/s]
  1%|          | 9/1000 [00:06<12:00,  1.38it/s]
  1%|          | 10/1000 [00:07<11:59,  1.38it/s]
  1%|          | 11/1000 [00:07<11:56,  1.38it/s]
  1%|          | 12/1000 [00:08<11:54,  1.38it/s]
  1%|▏         | 13/1000 [00:09<11:52,  1.38it/s]
  1%|▏         | 14/1000 [00:10<11:51,  1.39it/s]
  2%|▏         | 15/1000 [00:10<11:50,  1.39it/s]
  2%|▏         | 16/1000 [00:11<11:50,  1.39it/s]
  2%|▏         | 17/1000 [00:12<11:47,  1.39it/s]
  2%|▏         | 18/1000 [00:13<11:46,  1.39it/s]
  2%|▏         | 19/1000 [00:13<11:46,  1.39it/s]
  2%|▏         | 20/1000 [00:14<11:45,  1.39it/s]
  2%|▏         | 21/1000 [00:15<12:01,  1.36it/s]
  2%|▏         | 22/1000 [00:15<11:55,  1.37it/s]
  2%|▏         | 23/1000 [00:16<11:55,  1.36it/s]
  2%|▏         | 24/1000 [00:17<11:53,  1.37it/s]
  2%|▎         | 25/1000 [00:18<11:49,  1.37it/s]
  3%|▎         | 26/1000 [00:18<11:48,  1.37it/s]
  3%|▎         | 27/1000 [00:19<11:47,  1.38it/s]
  3%|▎         | 28/1000 [00:20<11:44,  1.38it/s]
  3%|▎         | 29/1000 [00:21<11:41,  1.38it/s]
  3%|▎         | 29/1000 [00:21<12:08,  1.33it/s]
Classification accuracy: 98.66666666666667%

We see that this auto-generated spiking (sinabs_model) network’s performance is close to that of the ann! Yay!

You would have noticed a free parameter that was added time_window. This is a critical parameter that determines whether or not your SNN is going to work well. The longer time_window is, the more spikes we produce as input and the better the performance of the network is going to be. Feel free to experiment with this parameter and see how this changes your network performance.

Visualisation of specific example#

# Get one sample from the dataloader
img, label = spike_mnist_test[10]

Lets visualize this data, just so we know what to expect. We can do this by collapsing the time dimension of the spike raster returned by the dataloader.

import matplotlib.pyplot as plt

%matplotlib inline


We now take this data (including the time dimension), and pass it to the Sinabs SNN model.

snn_output = sinabs_model(

Let us now display the output in time.

import numpy as np


plt.ylabel("Neuron ID")
plt.yticks(np.arange(10) + 0.5, np.arange(10))

As you can see, the majority of spikes are emitted by the output neuron corresponding to the digit plotted above, which is a correct inference.