Skip to content
Snippets Groups Projects
Commit 926fb8c0 authored by Hammouda Elbez's avatar Hammouda Elbez :computer:
Browse files

Nengo script added

parent 5aff81c7
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
## FMNIST with Nengo
%% Cell type:markdown id: tags:
### Load the libraries
%% Cell type:code id: tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import nengo
import nengo_dl
seed = 0
np.random.seed(seed)
tf.random.set_seed(seed)
```
%% Cell type:markdown id: tags:
### fashion MNIST dataset
%% Cell type:code id: tags:
``` python
(train_images, train_labels), (
test_images,
test_labels,
) = tf.keras.datasets.fashion_mnist.load_data()
# normalize images so values are between 0 and 1
train_images = train_images / 255.0
test_images = test_images / 255.0
# flatten images
train_images = train_images.reshape((train_images.shape[0], -1))
test_images = test_images.reshape((test_images.shape[0], -1))
class_names = [
"T-shirt/top",
"Trouser",
"Pullover",
"Dress",
"Coat",
"Sandal",
"Shirt",
"Sneaker",
"Bag",
"Ankle boot",
]
num_classes = len(class_names)
plt.figure(figsize=(10, 10))
for i in range(25):
plt.subplot(5, 5, i + 1)
plt.imshow(train_images[i].reshape((28, 28)), cmap=plt.cm.binary)
plt.axis("off")
plt.title(class_names[train_labels[i]])
```
%% Output
%% Cell type:markdown id: tags:
### Build The network
%% Cell type:code id: tags:
``` python
minibatch_size = 20
# Add time dimension
train_images = train_images[:, None, :]
train_labels = train_labels[:, None, None]
test_images = test_images[:, None, :]
test_labels = test_labels[:, None, None]
with nengo.Network(seed=seed) as net:
# set up some default parameters to match the Keras defaults
net.config[nengo.Ensemble].gain = nengo.dists.Choice([1])
net.config[nengo.Ensemble].bias = nengo.dists.Choice([0])
net.config[nengo.Connection].synapse = None
net.config[nengo.Connection].transform = nengo_dl.dists.Glorot()
# input node, same as before
inp = nengo.Node(output=np.ones(28 * 28))
# add the first dense layer
hidden = nengo.Ensemble(128, 1, neuron_type=nengo.RectifiedLinear())
nengo.Connection(inp, hidden.neurons)
# add the linear output layer (using nengo.Node since there is
# no nonlinearity)
out = nengo.Node(size_in=num_classes)
nengo.Connection(hidden, out)
# add a probe to collect output
out_p = nengo.Probe(out)
```
%% Cell type:markdown id: tags:
### Run the training
%% Cell type:code id: tags:
``` python
with net:
nengo_dl.configure_settings(stateful=False, use_loop=False)
with nengo_dl.Simulator(net, minibatch_size=minibatch_size) as sim:
sim.compile(
optimizer=tf.optimizers.Adam(),
loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=["accuracy"],
)
# Do training
sim.fit(train_images, train_labels, epochs=5)
print(
"Test accuracy:",
np.round(sim.evaluate(test_images, test_labels, verbose=0)["probe_accuracy"] * 100,2)," %"
)
```
%% Output
| Building network (0%) | ETA: --:--:--
Build finished in 0:00:00
|# Optimizing graph | 0:00:00
|# Optimizing graph: operator simplificaton | 0:00:00
Optimizing graph: operator simplificaton finished in 0:00:00
|# Optimizing graph: merging operators | 0:00:00
Optimizing graph: merging operators finished in 0:00:00
|# Optimizing graph: ordering signals | 0:00:00
Optimizing graph: ordering signals finished in 0:00:00
|# Optimizing graph: creating signals | 0:00:00
Optimizing graph: creating signals finished in 0:00:00
Optimization finished in 0:00:00
|# Constructing graph | 0:00:00
/home/hammouda/.local/lib/python3.6/site-packages/nengo_dl/simulator.py:461: UserWarning: No GPU support detected. See https://www.nengo.ai/nengo-dl/installation.html#installing-tensorflow for instructions on setting up TensorFlow with GPU support.
"No GPU support detected. See "
| # Constructing graph | 0:00:00
| Constructing graph: pre-build stage (0%) | ETA: --:--:--
Constructing graph: pre-build stage finished in 0:00:00
| Constructing graph: build stage (0%) | ETA: --:--:--
|##############Constructing graph: build stage (62%) | ETA: 0:00:00
Constructing graph: build stage finished in 0:00:00
| # Constructing graph | 0:00:00
Construction finished in 0:00:00
Epoch 1/5
| Constructing graph: pre-build stage (0%) | ETA: --:--:--
Constructing graph: pre-build stage finished in 0:00:00
| Constructing graph: build stage (0%) | ETA: --:--:--
|##############Constructing graph: build stage (25%) | ETA: 0:00:00
Constructing graph: build stage finished in 0:00:00
| Constructing graph: pre-build stage (0%) | ETA: --:--:--
Constructing graph: pre-build stage finished in 0:00:00
| Constructing graph: build stage (0%) | ETA: --:--:--
|##############Constructing graph: build stage (25%) | ETA: 0:00:00
Constructing graph: build stage finished in 0:00:00
3000/3000 [==============================] - 7s 2ms/step - loss: 0.5096 - probe_loss: 0.5096 - probe_accuracy: 0.8168
Epoch 2/5
3000/3000 [==============================] - 6s 2ms/step - loss: 0.3666 - probe_loss: 0.3666 - probe_accuracy: 0.8675
Epoch 3/5
3000/3000 [==============================] - 6s 2ms/step - loss: 0.3317 - probe_loss: 0.3317 - probe_accuracy: 0.8798
Epoch 4/5
3000/3000 [==============================] - 6s 2ms/step - loss: 0.3080 - probe_loss: 0.3080 - probe_accuracy: 0.8857
Epoch 5/5
3000/3000 [==============================] - 7s 2ms/step - loss: 0.2898 - probe_loss: 0.2898 - probe_accuracy: 0.8934
| Constructing graph: pre-build stage (0%) | ETA: --:--:--
Constructing graph: pre-build stage finished in 0:00:00
| Constructing graph: build stage (0%) | ETA: --:--:--
Constructing graph: build stage finished in 0:00:00
Test accuracy: 87.8 %
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment