-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraining.py
executable file
·158 lines (130 loc) · 5.28 KB
/
training.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
Deep Spiking Convolutional Neural Network
with STDP Learning Rule on MNIST data
______________________________________________________
Research Internship
Technical University Munich
Creator: Sven Gronauer
Date: February 2018
"""
import logging
import argparse
import pickle
import time
import os
import datetime
import numpy as np
import scipy
import matplotlib.pyplot as plt
import pylab
import seaborn as sns
import shutil
sns.set_style("dark")
import spynnaker8 as s
import pyNN.utility.plotting as plot
from SpikingConvNet import algorithms, classes, utils
from SpikingConvNet.parameters import *
def training_function(rc, model):
""" Train spiking neural network """
if rc.train_svm == True: # train classifier
# setup structure and receive spikes from last conv. layer
s.setup(timestep=TIMESTEP)
s.set_number_of_neurons_per_core(s.IF_curr_exp, 100)
rc.rebuild = True
network = classes.Spinnaker_Network(rc, model)
s.run(network.total_simtime)
[spikes_to_classify, voltage] = network.retrieve_data()
s.end()
# transform spiketrains to dataset, a datapoint consists of:
# count number of spikes for each neuron in last layer
tensor_last_layer = model.tensors[-1]
utils.plot_spike_activity(rc, spikes_to_classify, tensor_last_layer)
plt.show()
X_train = algorithms.spikes_for_classifier(rc,
tensor_last_layer,
spikes_to_classify)
model.classifier.train(X_train, network.y_train)
# svm.train_SVM(rc, X_train, y_train)
print "SVM X_train:"
print X_train
print("Max(X_train) = {}".format(np.max(X_train)))
np.savetxt(DATA_PATH+"/SVM_X_train", X_train, delimiter=',')
network.print_parameters()
elif rc.train_layer:
layer = rc.train_layer
tensor_prev = model.tensors[layer-1]
tensor = model.tensors[layer]
neurons_post = tensor[2] / tensor_prev[2]
stride = model.layers[layer].stride
kernel_shape = model.layers[layer].shape
if layer >= 2:
# Training a deeper layer requires spike times of the previous layer
# Therefore, the spikes of the previous layer must be determined at first
s.setup(timestep=TIMESTEP)
s.set_number_of_neurons_per_core(s.IF_curr_exp, 100)
rc.rebuild = True
network = classes.Spinnaker_Network(rc, model)
s.run(network.total_simtime)
[spikes_to_window, voltage] = network.retrieve_data()
s.end()
# for i, plain in enumerate(spikes_to_window):
# print i,plain
# utils.plot_membrane_voltages(rc, voltage, network.total_simtime)
utils.plot_spike_activity(rc, spikes_to_window, tensor_prev)
# plt.show()
deep_spikes = algorithms.windowed_spikes(spikes_to_window,
tensor_prev,
tensor,
kernel_shape,
stride)
# save deep_spikes
pickle.dump(deep_spikes, open(MODEL_PATH+"deepspikes.p", "wb"))
rc.logging.info('Calculated Deep Spikes')
# comment to save processing time
# utils.plot_spikes(rc, spikes_to_window, title="Spikes of Layer")
# Now, train the appreciated layer with STDP rules
s.setup(timestep=TIMESTEP)
s.set_number_of_neurons_per_core(s.IF_curr_exp, 100)
rc.rebuild = False
if layer >=2:
network = classes.Spinnaker_Network(rc, model,deepspikes=deep_spikes)
else:
network = classes.Spinnaker_Network(rc,model)
network.print_parameters()
s.run(network.total_simtime)
try:
spikes_input, spikes_layer, voltage = network.retrieve_data()
except TypeError:
rc.logging.error("No spikes have been emitted in the last layer")
# Receive kernel weights and prepare to plot
w_init = network.w_init.reshape((-1,kernel_shape[0],kernel_shape[1]))
network.update_kernel_weights()
w_1 = network.w_layer[layer].reshape((-1,kernel_shape[0],kernel_shape[1]))
delta = w_1 - w_init
s.end()
network.print_parameters()
""" =============
Display Plots
============= """
# try:
# utils.plot_membrane_voltages(rc, voltage, network.total_simtime)
# except:
# print("not printed voltages !")
# try:
# utils.plot_spikes(rc, spikes_input, title="Spikes of Layer")
# except:
# print("not printed spikes_layer")
try:
utils.plot_spikes(rc, spikes_layer, title="Spikes of Layer")
except:
print("not printed spikes_layer")
utils.plot_heatpmap(rc, network.images, title="Input Patterns")
try:
utils.plot_heatpmap(rc, delta, title = "Weight Changes", delta=True)
except:
print("not printed DELTA weights")
try:
utils.plot_heatpmap(rc, w_1, title = "Kernel Weights_1")
except:
print("not printed w_1 Kernel weights")
plt.show()