snntorch
Deep and online learning with spiking neural networks in Python
What is snntorch?
Deep and online learning with spiking neural networks in Python
Overview of snntorch
What it is
snnTorch is a Python package for performing gradient-based learning with spiking neural networks. It extends PyTorch by integrating pre-designed spiking neuron models that can be treated as recurrent activation units. The package takes advantage of PyTorch's GPU-accelerated tensor computation and applies it to networks of spiking neurons. snnTorch is designed to be used intuitively with PyTorch, as though each spiking neuron were simply another activation in a sequence of layers. It is agnostic to fully-connected layers, convolutional layers, residual connections, etc. The neuron models are represented by recursive functions, which removes the need to store membrane potential traces for all neurons in a system to calculate the gradient. This enables small and large networks to be viably trained on CPU where needed, and on GPU when tensors are loaded onto CUDA.
Key features
- Spiking neuron library (
snntorch) deeply integrated with autograd, similar totorch.nn. - Export and import functionality via
snntorch.export_nirandsnntorch.import_nirusing the NIR (Neural Interchange Representation) format. - Functional operations on spikes (
snntorch.functional) including loss functions and regularization. - Spike generation and data conversion (
snntorch.spikegen). - Visualization tools for spike-based data (
snntorch.spikeplot) using matplotlib and celluloid. - Optional surrogate gradient functions (
snntorch.surrogate). - Spiking-aware weight initialization functions (
snntorch.weight_init) and dataset utility functions (snntorch.utils).
Use cases
snnTorch is used for training spiking neural networks (SNNs) using gradient-based learning methods. It is suitable for research and development in neuromorphic computing, where the brain's spike-based information encoding is leveraged to develop more efficient neural networks. The package can be applied to tasks such as image classification, temporal pattern recognition, and any problem that benefits from event-driven computation. It is also used for exploring online learning and deep learning with spiking neurons.
Getting started
Install snnTorch via pip:bashpip install snntorchTo install from source:bashgit clone https://github.com/jeshraghian/snnTorchcd snntorchpython setup.py installTo install with conda:bashconda install -c conda-forge snntorchFor a quick example, see the following Python snippet (requires PyTorch):pythonimport torch, torch.nn as nnimport snntorch as snnfrom snntorch import surrogatefrom snntorch import utilsnum_steps = 25batch_size = 1beta = 0.5spike_grad = surrogate.fast_sigmoid()net = nn.Sequential( nn.Conv2d(1, 8, 5), nn.MaxPool2d(2), snn.Leaky(beta=beta, init_hidden=True, spike_grad=spike_grad), nn.Conv2d(8, 16, 5), nn.MaxPool2d(2), snn.Leaky(beta=beta, init_hidden=True, spike_grad=spike_grad), nn.Flatten(), nn.Linear(16 * 4 * 4, 10), snn.Leaky(beta=beta, init_hidden=True, spike_grad=spike_grad, output=True))data_in = torch.rand(num_steps, batch_size, 1, 28, 28)spike_recording = []utils.reset(net)for step in range(num_steps): spike, state = net(data_in[step]) spike_recording.append(spike)
License and pricing
The license is MIT. The pricing type is open source.
Based on the project's public README and repository metadata.