https://github.com/genn-team/ml_genn
A library for deep learning with Spiking Neural Networks (SNN).
https://github.com/genn-team/ml_genn
deep-learning genn machine-learning spiking-neural-networks
Last synced: 5 months ago
JSON representation
A library for deep learning with Spiking Neural Networks (SNN).
- Host: GitHub
- URL: https://github.com/genn-team/ml_genn
- Owner: genn-team
- License: lgpl-2.1
- Created: 2019-05-28T09:11:02.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-12-05T14:27:05.000Z (7 months ago)
- Last Synced: 2025-12-05T14:34:24.369Z (7 months ago)
- Topics: deep-learning, genn, machine-learning, spiking-neural-networks
- Language: Python
- Homepage: https://ml-genn.readthedocs.io
- Size: 37.6 MB
- Stars: 36
- Watchers: 4
- Forks: 11
- Open Issues: 42
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://gen-ci.inf.sussex.ac.uk/job/GeNN/job/ml_genn/job/master/) [](https://ml-genn.readthedocs.io) [](https://doi.org/10.5281/zenodo.7681736) [](https://codecov.io/gh/genn-team/ml_genn)
# mlGeNN
A library for deep learning with Spiking Neural Networks (SNN)powered by [GeNN](http://genn-team.github.io/genn/), a GPU enhanced Neuronal Network simulation environment.
## Installation
1. Follow the instructions in https://genn-team.github.io/genn/documentation/5/installation.html to install PyGeNN.
2. Clone this project
3. Install mlGeNN with setuptools using ``pip install .`` command in the ``ml_genn`` directory
3. To use mlGeNN to convert ANNs trained with Keras to SNNs, install mlGeNN TF with setuptools using ``pip install .`` command in the ``ml_genn_tf`` directory
## Usage
### Convert ANN to SNN
The following example illustrates how to convert an ANN model, defined in Keras, to an SNN using the few-spike ([Stöckl & Maass, 2021](http://dx.doi.org/10.1038/s42256-021-00311-4)) conversion method:
```python
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
tf_model = Sequential([
Conv2D(16, 3, padding='same', activation='relu',
use_bias=False, input_shape=(32, 32, 3)),
AveragePooling2D(2),
Flatten(),
Dense(10, activation='relu', use_bias=False)])
# compile and train tf_model with TensorFlow
...
from ml_genn_tf.converters import FewSpike
converter = FewSpike(k=8, signed_input=signed_input, norm_data=[x_subset])
net, net_inputs, net_outputs, tf_layer_pops = converter.convert(tf_model)
compiler = converter.create_compiler()
compiled_net = compiler.compile(net, inputs=net_inputs, outputs=net_outputs)
with compiled_net:
metrics, cb_data = compiled_net.evaluate({net_inputs[0]: validate_x},
{net_outputs[0]: validate_y})
print(f"Accuracy = {100.0 * metrics[net_outputs[0]].result}%")
```
For further examples, please see the examples/tf folder.
### Training an SNN using e-prop
The following example illustrates how to train a simple SNN with the e-prop learning rule ([Bellec, Scherr et al., 2020](http://dx.doi.org/10.1038/s41467-020-17236-y)):
```python
from ml_genn import InputLayer, Layer, SequentialNetwork
from ml_genn.compilers import EPropCompiler
from ml_genn.connectivity import Dense
from ml_genn.initializers import Normal
from ml_genn.neurons import LeakyIntegrate, LeakyIntegrateFire
from ml_genn.compilers.eprop_compiler import default_params
# load dataset
...
network = SequentialNetwork(default_params)
with network:
# Populations
input = InputLayer("poisson_input", 768)
Layer(Dense(Normal(sd=1.0 / np.sqrt(768))),
LeakyIntegrateFire(tau_refrac=5.0),
128)
output = Layer(Dense(Normal(sd=1.0 / np.sqrt(128))),
LeakyIntegrate(),
10)
compiler = EPropCompiler(example_timesteps=200,
losses="sparse_categorical_crossentropy",
optimiser="adam", batch_size=128)
compiled_net = compiler.compile(network)
with compiled_net:
metrics, _ = compiled_net.train({input: x},
{output: y},
num_epochs=10, shuffle=True)
print(f"Accuracy = {100 * metrics[output].result}%")
```
For further examples, please see the examples/eprop folder.