Hello drasmuss,
Thanks for your answer, actually we have already make the robot work if we use normal artificial neural network as Q_function approximation. So it will be easy for us, if we can replace the ANN by Spiking neural network.
And now we try to use nengo_dl, because this simulator is quit similar to the ANN framework.
We have few questions, hope you can help us:
1, I do not understand the âminibatch_sizeâ in nengo_dl simulator. For example, the mnist dataset has training images 60000, when I start the training, I will feed whole data into nengo model. I should set the minibatch_size to 60000, or I can give it a arbitrary number, for example 60?
And there is another argument n_epochs, I guess we can give a arbitrary number to minibatch_size, for example 60, and if we train the n_epochs = 1000, the whole dataset 60000 will be trained, am I right?
2, After training, I print the sim.loss, it is 0.1. But when I do prediction, the accuracy is very low, so I think I didât find the rights way to do prediction. Please tell me the right way to do it. My code is there:
import nengo
import nengo_dl
import numpy as np
import tensorflow as tf
class Spiking_Qnetwork:
def __init__(self, input_shape, output_shape, nb_hidden, weights_path):
'''
Spiking neural network as the Q value function approximation
:param input_shape: the input dimension without batch_size, example: state is 2 dimension,
action is 1 dimenstion, the input shape is 3.
:param output_shape: the output dimension without batch_size, the dimenstion of Q values
:param nb_hidden: the number of neurons in ensemble
:param weights_path: the path to save all parameters
'''
self.input_shape = input_shape
self.output_shape = output_shape
self.nb_hidden = nb_hidden
self.weights_path = weights_path
self.model = self.build()
def encoder_decoder_initialization(self, shape):
'''
:return: initialised encoder or decoder
'''
rng = np.random.RandomState(1)
coders = rng.normal(size=shape)
return coders
def build(self):
encoders = self.encoder_decoder_initialization(shape=(self.nb_hidden, self.input_shape))
decoders = self.encoder_decoder_initialization(shape=((self.nb_hidden, self.output_shape)))
model = nengo.Network(seed=3)
with model:
self.input_neuron = nengo.Ensemble(n_neurons=self.nb_hidden,
dimensions=self.input_shape,
neuron_type=nengo.LIFRate(),
intercepts=nengo.dists.Uniform(-1.0, 1.0),
max_rates=nengo.dists.Choice([100]),
encoders=encoders,
)
output = nengo.Node(size_in=self.output_shape)
self.output_p = nengo.Probe(output)
nengo.Connection(self.input_neuron.neurons,
output,
synapse=None,
transform=decoders.T
)
return model
def training(self, input_data, label, batch_size, nb_epochs):
sim = nengo_dl.Simulator(self.model,
minibatch_size=batch_size,
step_blocks=1,
device="/gpu:0"
)
sim.train({self.input_neuron: input_data},
{self.output_p: label},
tf.train.MomentumOptimizer(5e-2, 0.9),
n_epochs=nb_epochs
)
sim.save_params(self.weights_path)
sim.close()
def predict(self, input_data, batch_size=1):
sim = nengo_dl.Simulator(self.model,
minibatch_size=batch_size,
step_blocks=1,
device="/gpu:0")
sim.load_params(self.weights_path)
sim.step(input_feeds={self.input_neuron: input_data})
output = sim.data[self.output_p]
sim.close()
return output
if name == âmainâ:
from keras.datasets import mnist
from keras.utils import np_utils
from sklearn.metrics import accuracy_score
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# data pre-processing
X_train = X_train.reshape(X_train.shape[0], -1) / 255. # normalize
X_test = X_test.reshape(X_test.shape[0], -1) / 255. # normalize
y_train = np_utils.to_categorical(y_train, nb_classes=10)
y_test = np_utils.to_categorical(y_test, nb_classes=10)
X_train_ = np.expand_dims(X_train, axis=1)
X_test_ = np.expand_dims(X_test, axis=1)
y_train_ = np.expand_dims(y_train, axis=1)
y_test_ = np.expand_dims(y_test, axis=1)
model = Spiking_Qnetwork(input_shape=28*28,
output_shape=10,
nb_hidden=1000,
weights_path="/home/huangbo/SpikingDeepRLControl/huangbo_ws/"
"networks/saved_weights/snn_weights")
# training
model.training(input_data=X_train_, label=y_train_, batch_size=10000, nb_epochs=5)
output = model.predict(batch_size=X_test.shape[0], input_data=X_test_)
prediction = np.squeeze(output, axis=1)
# evaluate the model
from sklearn.metrics import accuracy_score
acc = accuracy_score(np.argmax(y_test, axis=1), np.argmax(prediction, axis=1))
print "the test acc is:", acc