-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_classes.py
359 lines (290 loc) · 12.4 KB
/
net_classes.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import numpy as np
import copy
import random
from matplotlib import pyplot as plt
from sklearn.metrics import log_loss, f1_score
from scipy.special import softmax
from sklearn.metrics import confusion_matrix
import seaborn as sns
# For the regulare neural network
def sigmoid(x):
return 1 / (1 + np.exp(x))
class Neuron():
def __init__(self, id, threshold=0.6, weight=[1], leakage=0.5, spike_train_length=100, bias=0):
self.id = id
self.threshold = float(threshold)
self.leakage = float(leakage)
self.spike_train = np.zeros(spike_train_length)
self.weight = np.array(weight)
self.potential = 0
self.fired = False
self.bias = 0
def neuron_update(self, input):
# Take the input array, multiply it with the weight array and sum the result
self.spike_train = np.zeros(len(self.spike_train))
self.potential = 0
input_array = np.sum(input * self.weight[:, None], axis=0)# +self.bias
# Send each timestep of the input array to the neuron
# Save the output to spike_train
for i in range(len(input_array)):
step = input_array[i]
self.potential += (step - self.leakage * self.potential)
if self.potential < self.threshold or self.fired:
# if self.potential < 0: self.potential = 0
self.spike_train[i] = 0
self.fired = False
else:
self.spike_train[i] = 1
self.potential = 0
self.fired = True
def get_spike_train(self):
# Return the spike train
return self.spike_train
def reset(self):
# Reset the neuron for a new input
self.spike_train = np.zeros(len(self.spike_train))
self.potential = 0
def mutate(self, mutation_rate=0.5, weight_mutation_rate=0.05,):
for i in range(len(self.weight)):
if np.random.rand() < mutation_rate:
self.weight[i] += np.random.normal(0, weight_mutation_rate)
def get_genome_neuron(self):
# Return the genome of the neuron
return [np.copy(self.weight), copy.copy(self.threshold), copy.copy(self.leakage), copy.copy(self.bias)]
def set_genome(self, genome):
# Set the genome of the neuron
self.weight = genome[0]
self.threshold = genome[1]
self.leakage = genome[2]
self.bias = genome[3]
class RNuron(Neuron):
def neuron_update(self, input):
self.output= sigmoid(np.dot(input, self.weights))
return self.output
class Layer:
def __init__(self, neurons=[], spike_train_length=100):
self.neurons = neurons
self.spike_train_length = spike_train_length
self.output = np.zeros((len(neurons), self.spike_train_length))
self.input_layer = False
self.genome = []
def add_neuron(self, neuron):
# Add a neuron to the layer
self.neurons.append(neuron)
def update_output_array(self):
# Get the output from each neuron and store it in the output array
self.output = np.zeros((len(self.neurons), self.spike_train_length))
def layer_update(self, input):
# Send the input to each neuron in the layer
i = 0
if self.input_layer:
for neuron in self.neurons:
# Update neuron
neuron.neuron_update(input[i])
self.output[i] = neuron.get_spike_train()
i += 1
else:
for neuron in self.neurons:
neuron.neuron_update(input)
self.output[i] = neuron.get_spike_train()
i += 1
# Store neuron output in output array
# Return the array of neuron outputs
return self.output
def reset(self):
# Reset the layer for a new input
self.output = np.zeros((len(self.neurons), self.spike_train_length))
def get_number_of_neurons(self):
return len(self.neurons)
def get_genome_layer(self):
self.genome = []
for neuron in self.neurons:
self.genome.append(neuron.get_genome_neuron())
return self.genome
def set_genome(self, genome):
for i in range(len(self.neurons)):
self.neurons[i].set_genome(genome[i])
def mutate(self, mutation_rate=0.5, weight_mutation_rate=0.05, threshold_mutation_rate=0.5,
leakage_mutation_rate=0.5):
for neuron in self.neurons:
neuron.mutate(mutation_rate, weight_mutation_rate)
class Network:
def __init__(self, layers=[], nr_outputs=10, spike_train_length=100, id=0, batch_size=1):
self.layers = layers
self.id = id
# Create a empty array for the output
self.output = np.zeros((nr_outputs, spike_train_length))
self.prediction_history = []
self.genome = []
self.current_prediction_score = 0
self.current_accuracy = 0
self.accuracy_history = [0.0]
self.batch_size = batch_size
self.accuracy_counter = 0
# Cross entropy vars
self.batch_output = []
self.batch_target = []
self.f1_score = 0
self.current_f1_score = 0
self.predicted = 0
self.batch_prediction = []
self.current_log_loss = 0
self.wrong_prediction = 0
def add_layer(self, layer):
# Add a layer to the network
self.layers.append(layer)
def network_update(self, input):
# Send the input to each layer in the network
for layer in self.layers:
# Copy the output from the previous layer to the input of the next layer
input = layer.layer_update(input)
self.output = input
# Check time to set np.array
for i in range(len(input)):
self.output[i] = input[i]
def reset(self):
# Reset the network for a new input
for layer in self.layers:
layer.reset()
def batch_reset(self):
self.current_accuracy = 0
self.batch_target = []
self.batch_output = []
self.batch_prediction = []
self.accuracy_counter = 0
self.current_f1_score = 0
self.confusion_predicted = []
self.wrong_prediction = 0
def get_output(self):
return self.output
def mutate_network(self, mutation_rate=0.5, weight_mutation_rate=0.05, threshold_mutation_rate=0.5,
leakage_mutation_rate=0.5):
for layer in self.layers:
layer.mutate(mutation_rate, weight_mutation_rate, threshold_mutation_rate, leakage_mutation_rate)
def get_genome(self):
for layer in self.layers:
self.genome.append(layer.get_genome_layer())
return self.genome
def get_prediction(self, answer):
"""
Get the prediction of the network
And save it to self.prediction_history
:param answer: The correct answer
:return: The prediction
"""
# Get the prediction of the network
output = self.get_output()
prediction = [sum(neuron) for neuron in output]
self.batch_output.append(prediction)
self.batch_target.append(answer)
# Get the accuracy of the prediction
if prediction.count(max(prediction)) == 1:
self.predicted = prediction.index(max(prediction))
else:
# Choose on random one of the max indexes
self.predicted = random.choice([i for i, x in enumerate(prediction) if x == max(prediction)])
self.confusion_predicted.append(self.predicted)
# self.predicted = random.choice([i for i, x in enumerate(prediction) if x == max(prediction)])
self.batch_prediction.append(self.predicted)
if self.predicted == answer:
# If the answer is the only max value
if prediction.count(max(prediction)) == 1:
self.accuracy_counter += 1
else:
self.accuracy_counter += 1 / prediction.count(max(prediction))
else:
self.wrong_prediction += 1
if sum(prediction) == 0:
self.prediction_history.append([0, answer])
return 0.0
else:
pred_score = prediction[answer] / sum(prediction)
self.prediction_history.append([pred_score, answer])
return pred_score
def get_accuracy(self, batch_size):
return self.accuracy_history
def get_new_fit(self):
return (self.current_accuracy + self.current_f1_score) - self.wrong_prediction/self.batch_size
def calculate_prediction_score(self):
"""
Get the average score of the prediction
"""
self.current_prediction_score = sum(
[x[0] for x in self.prediction_history[-self.batch_size:]]) / self.batch_size
self.current_accuracy = self.accuracy_counter / self.batch_size
self.accuracy_counter = 0
self.f1_score = self.get_f1_score()
self.log_loss = self.get_log_loss()
def get_log_loss(self):
soft = self.soft_max(self.batch_output)
batch_answer = np.zeros((len(self.batch_target), 10))
for i in range(len(self.batch_target)):
batch_answer[i][self.batch_target[i]] = 1
self.current_log_loss = log_loss(batch_answer, soft)
return self.current_log_loss
def soft_max(self, array):
return softmax(array, axis=1)
def get_f1_score(self):
# Good pred with 'macro' and 'weighted' average, should probably not use 'micro'
self.current_f1_score = f1_score(self.batch_target, self.batch_prediction, average='weighted')
return self.current_f1_score
def get_prediction_score(self):
return self.current_prediction_score, self.current_accuracy
# return sum([x[0] for x in self.prediction_history[-batch_size:]])
def get_genome(self):
self.genome = []
for layer in self.layers:
self.genome.append(layer.get_genome_layer())
return self.genome
def set_genome(self, genome):
for i in range(len(self.layers)):
self.layers[i].set_genome(genome[i])
def get_pred(self):
return self.current_accuracy, self.current_f1_score, self.current_log_loss
def plot_output(self):
spike_train = self.get_output()
i = 0
for train in spike_train:
train = train + i
plt.plot(train, label='Neuron {}'.format(i))
i += 1
plt.title(f'Spike train for the number: {image}, epoch: {ep}')
plt.yticks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# plt.legend()
plt.show()
self.plot_confusion_matrix()
def plot_confusion_matrix(self):
cm = confusion_matrix(self.batch_target, self.confusion_predicted, labels=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
vmin = np.min(cm)
vmax = np.max(cm)
off_diag_mask = np.eye(*cm.shape, dtype=bool)
fig = plt.figure()
sns.heatmap(cm, annot=True, mask=~off_diag_mask, cmap='Blues', vmin=vmin, vmax=vmax)
sns.heatmap(cm, annot=True, mask=off_diag_mask,vmin=vmin, vmax=vmax, cmap='Reds', cbar=False,
cbar_kws=dict(ticks=[]))
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()
def __str__(self):
return f'Network {self.id} ' \
f'\n with a acc score of {self.current_accuracy}' \
f'\n and a F1 score of {self.current_f1_score}' \
def __gt__(self, other):
# return self.current_accuracy < other.current_accuracy
return self.f1_score > other.f1_score
# return self.log_loss > other.log_loss
if __name__ == '__main__':
neuron1 = Neuron(id=1, threshold=3, weight=[0.1, 0.3], leakage=0.1, spike_train_length=10)
neuron2 = Neuron(id=1, threshold=1, weight=[1, 2], leakage=0.01, spike_train_length=10)
neuron3 = Neuron(id=1, threshold=1, weight=[1, 3], leakage=0.1, spike_train_length=10)
neuron4 = Neuron(id=1, threshold=1, weight=[1, -2], leakage=0.1, spike_train_length=10)
neuron5 = Neuron(id=1, threshold=5, weight=[1, 0.1], leakage=0.5, spike_train_length=10)
layer1 = Layer(neurons=[neuron1, neuron2], spike_train_length=10)
layer2 = Layer(neurons=[neuron3, neuron4, neuron5], spike_train_length=10)
layer2.add_neuron(neuron5)
layer2.update_output_array()
net = Network(layers=[layer1, layer2], nr_outputs=4, spike_train_length=10)
print(layer2.get_number_of_neurons())
print(layer1.get_number_of_neurons())
spike_train = np.array([[0, 0, 0, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 1, 1, 1, 0]])
net.network_update(spike_train)