snnTorch#

snnTorch is a deep learning simulator for spiking neural networks, built on PyTorch.

Import a NIR graph to snnTorch#

import snntorch as snn
import torch

import nir

# Create a NIR Network
affine_weights = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
affine_bias = torch.tensor([1.0, 2.0])
li_tau = torch.tensor([0.9, 0.8])
li_r = torch.tensor([1.0, 1.0])
li_v_leak = torch.tensor([0.0, 0.0])
nir_network = nir.NIRGraph.from_list(nir.Affine(affine_weights, affine_bias), nir.LI(li_tau, li_r, li_v_leak))

# Import to snnTorch
snntorch_network = snn.import_from_nir(nir_network)

Export a NIR graph from snnTorch#

import snntorch as snn
import torch

lif1 = snn.Leaky(beta=0.9, init_hidden=True)
lif2 = snn.Leaky(beta=0.9, init_hidden=True, output=True)

# Create a network
snntorch_network = torch.nn.Sequential(
    torch.nn.Flatten(),
    torch.nn.Linear(784, 500),
    lif1,
    torch.nn.Linear(500, 10),
    lif2
)

sample_data = torch.randn(1, 784)

# Export to nir
nir_model = snn.export_to_nir(snntorch_network, sample_data)

# Save to file
nir.write("nir_model.nir", nir_model)