#!pip install ANNarchy
ANN-to-SNN conversion - MLP
This notebook demonstrates how to transform a fully-connected neural network trained using tensorflow/keras into an SNN network usable in ANNarchy.
The methods are adapted from the original models used in:
Diehl et al. (2015) “Fast-classifying, high-accuracy spiking deep networks through weight and threshold balancing” Proceedings of IJCNN. doi: 10.1109/IJCNN.2015.7280696
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
print(f"Tensorflow {tf.__version__}")
Tensorflow 2.16.2
First we need to download and process the MNIST dataset provided by tensorflow.
# Download data
= tf.keras.datasets.mnist.load_data()
(X_train, t_train), (X_test, t_test)
# Normalize inputs
= X_train.reshape(X_train.shape[0], 784).astype('float32') / 255.
X_train = X_test.reshape(X_test.shape[0], 784).astype('float32') / 255.
X_test
# One-hot output vectors
= tf.keras.utils.to_categorical(t_train, 10)
T_train = tf.keras.utils.to_categorical(t_test, 10) T_test
Training an ANN in tensorflow/keras
The tensorflow.keras
network is build using the functional API.
The fully-connected network has two fully connected layers with ReLU, no bias, dropout at 0.5, and a softmax output layer with 10 neurons. We use the standard SGD optimizer and the categorical crossentropy loss for classification.
def create_mlp():
# Model
= tf.keras.layers.Input(shape=(784,))
inputs = tf.keras.layers.Dense(128, use_bias=False, activation='relu')(inputs)
x= tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.Dense(128, use_bias=False, activation='relu')(x)
x= tf.keras.layers.Dropout(0.5)(x)
x =tf.keras.layers.Dense(10, use_bias=False, activation='softmax')(x)
x
= tf.keras.Model(inputs, x)
model
# Optimizer
= tf.keras.optimizers.SGD(learning_rate=0.05)
optimizer
# Loss function
compile(
model.='categorical_crossentropy', # loss function
loss=optimizer, # learning rule
optimizer=['accuracy'] # show accuracy
metrics
)print(model.summary())
return model
We can now train the network and save the weights in the HDF5 format.
# Create model
= create_mlp()
model
# Train model
= model.fit(
history # training data
X_train, T_train, =128, # batch size
batch_size=20, # Maximum number of epochs
epochs=0.1, # Percentage of training data used for validation
validation_split
)
"runs/mlp.keras")
model.save(
# Test model
= model.predict(X_test, verbose=0)
predictions_keras = model.evaluate(X_test, T_test, verbose=0)
test_loss, test_accuracy print(f"Test accuracy: {test_accuracy}")
Model: "functional_1"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩ │ input_layer_1 (InputLayer) │ (None, 784) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dense_3 (Dense) │ (None, 128) │ 100,352 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dropout_2 (Dropout) │ (None, 128) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dense_4 (Dense) │ (None, 128) │ 16,384 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dropout_3 (Dropout) │ (None, 128) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dense_5 (Dense) │ (None, 10) │ 1,280 │ └─────────────────────────────────┴────────────────────────┴───────────────┘
Total params: 118,016 (461.00 KB)
Trainable params: 118,016 (461.00 KB)
Non-trainable params: 0 (0.00 B)
None
Epoch 1/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.4543 - loss: 1.5829 - val_accuracy: 0.9058 - val_loss: 0.3567
Epoch 2/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.8068 - loss: 0.6232 - val_accuracy: 0.9278 - val_loss: 0.2498
Epoch 3/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.8569 - loss: 0.4832 - val_accuracy: 0.9382 - val_loss: 0.2097
Epoch 4/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.8756 - loss: 0.4270 - val_accuracy: 0.9458 - val_loss: 0.1850
Epoch 5/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.8909 - loss: 0.3788 - val_accuracy: 0.9530 - val_loss: 0.1661
Epoch 6/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.8969 - loss: 0.3467 - val_accuracy: 0.9575 - val_loss: 0.1519
Epoch 7/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9056 - loss: 0.3280 - val_accuracy: 0.9598 - val_loss: 0.1447
Epoch 8/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9109 - loss: 0.3070 - val_accuracy: 0.9625 - val_loss: 0.1341
Epoch 9/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9166 - loss: 0.2879 - val_accuracy: 0.9647 - val_loss: 0.1258
Epoch 10/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9183 - loss: 0.2811 - val_accuracy: 0.9667 - val_loss: 0.1213
Epoch 11/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9203 - loss: 0.2737 - val_accuracy: 0.9663 - val_loss: 0.1184
Epoch 12/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9245 - loss: 0.2605 - val_accuracy: 0.9685 - val_loss: 0.1120
Epoch 13/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9299 - loss: 0.2450 - val_accuracy: 0.9688 - val_loss: 0.1094
Epoch 14/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9298 - loss: 0.2436 - val_accuracy: 0.9707 - val_loss: 0.1056
Epoch 15/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9313 - loss: 0.2346 - val_accuracy: 0.9712 - val_loss: 0.1041
Epoch 16/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9351 - loss: 0.2212 - val_accuracy: 0.9710 - val_loss: 0.1027
Epoch 17/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9368 - loss: 0.2238 - val_accuracy: 0.9728 - val_loss: 0.1013
Epoch 18/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9374 - loss: 0.2141 - val_accuracy: 0.9725 - val_loss: 0.0971
Epoch 19/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9393 - loss: 0.2141 - val_accuracy: 0.9752 - val_loss: 0.0937
Epoch 20/20
422/422 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.9406 - loss: 0.2080 - val_accuracy: 0.9740 - val_loss: 0.0939
Test accuracy: 0.96670001745224
=(12, 6))
plt.figure(figsize121)
plt.subplot('loss'], '-r', label="Training")
plt.plot(history.history['val_loss'], '-b', label="Validation")
plt.plot(history.history['Epoch #')
plt.xlabel('Loss')
plt.ylabel(
plt.legend()
122)
plt.subplot('accuracy'], '-r', label="Training")
plt.plot(history.history['val_accuracy'], '-b', label="Validation")
plt.plot(history.history['Epoch #')
plt.xlabel('Accuracy')
plt.ylabel(
plt.legend() plt.show()
Initialize the ANN-to-SNN converter
We first create an instance of the ANN-to-SNN conversion object. The function receives the input_encoding parameter, which is the type of input encoding we want to use.
By default, there are intrinsically bursting (IB
), phase shift oscillation (PSO
) and Poisson (poisson
) available.
from ANNarchy.extensions.ann_to_snn_conversion import ANNtoSNNConverter
= ANNtoSNNConverter(
snn_converter ='IB',
input_encoding='IaF',
hidden_neuron='spike_count',
read_out )
ANNarchy 4.8 (4.8.2) on darwin (posix).
After that, we provide the TensorFlow model stored as a .keras
file to the conversion tool. The print-out of the network structure of the imported network is suppressed when show_info=False
is provided to load_keras_model
.
= snn_converter.load_keras_model("runs/mlp.keras", show_info=True) net
WARNING: Dense representation is an experimental feature for spiking models, we greatly appreciate bug reports.
* Input layer: input_layer_1, (784,)
* InputLayer skipped.
* Dense layer: dense_3, 128
weights: (128, 784)
mean -0.0035283518955111504, std 0.05272930860519409
min -0.35987481474876404, max 0.22091051936149597
* Dropout skipped.
* Dense layer: dense_4, 128
weights: (128, 128)
mean 0.003263121470808983, std 0.10201630741357803
min -0.2913631200790405, max 0.3844316899776459
* Dropout skipped.
* Dense layer: dense_5, 10
weights: (10, 128)
mean -0.002390058944001794, std 0.21618692576885223
min -0.5936785340309143, max 0.48161792755126953
When the network has been built successfully, we can perform a test using all MNIST training samples. Using duration_per_sample
, the duration simulated for each image can be specified. Here, 200 ms seem to be enough.
= snn_converter.predict(X_test, duration_per_sample=200) predictions_snn
100%|██████████| 10000/10000 [00:55<00:00, 180.81it/s]
Using the recorded predictions, we can now compute the accuracy using scikit-learn for all presented samples.
from sklearn.metrics import classification_report, accuracy_score
print(classification_report(t_test, predictions_snn))
print("Test accuracy of the SNN:", accuracy_score(t_test, predictions_snn))
precision recall f1-score support
0 0.97 0.99 0.98 980
1 0.98 0.98 0.98 1135
2 0.96 0.95 0.96 1032
3 0.96 0.96 0.96 1010
4 0.97 0.96 0.96 982
5 0.96 0.96 0.96 892
6 0.96 0.97 0.97 958
7 0.97 0.96 0.97 1028
8 0.95 0.96 0.95 974
9 0.96 0.95 0.96 1009
accuracy 0.97 10000
macro avg 0.96 0.97 0.96 10000
weighted avg 0.97 0.97 0.97 10000
Test accuracy of the SNN: 0.9652
For comparison, here is the performance of the original ANN in keras:
print(classification_report(t_test, predictions_keras.argmax(axis=1)))
print("Test accuracy of the ANN:", accuracy_score(t_test, predictions_keras.argmax(axis=1)))
precision recall f1-score support
0 0.97 0.99 0.98 980
1 0.98 0.98 0.98 1135
2 0.96 0.96 0.96 1032
3 0.96 0.96 0.96 1010
4 0.96 0.97 0.96 982
5 0.97 0.97 0.97 892
6 0.96 0.97 0.97 958
7 0.97 0.97 0.97 1028
8 0.96 0.95 0.96 974
9 0.97 0.94 0.96 1009
accuracy 0.97 10000
macro avg 0.97 0.97 0.97 10000
weighted avg 0.97 0.97 0.97 10000
Test accuracy of the ANN: 0.9667