-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralNetwork.py
500 lines (398 loc) · 21.1 KB
/
NeuralNetwork.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# -------------------------------------------- #
# --------- NEURAL NETWORK DESCRIPTION ------- #
# -------------------------------------------- #
# #
# Type Problem: Classification #
# Type Network: Multi-Layer #
# Type Target: Multi-Target #
# #
# -------------------------------------------- #
# #
# Hidden Activation: Hyperbolic Tangent #
# Output Activation: SoftMax #
# #
# -------------------------------------------- #
from __future__ import print_function
import random
import math
class NeuralNetwork:
# ---------- NETWORK INITIALIZATION ---------- #
def __init__(self, num_input, num_hidden, num_output, num_epochs, early_stop, learning_rate, momentum, rand_range):
# Set when to stop leaning
self.early_stop = early_stop
# Set the number of nodes
self.num_input = num_input
self.num_hidden = num_hidden
self.num_output = num_output
# Set training epochs
self.num_epochs = num_epochs
# Set backpropagation variables
self.learning_rate = learning_rate
self.momentum = momentum
# Set the upper and lower random bounds
self.upper_bound = rand_range
self.lower_bound = -rand_range
# Set lists of zeros to input and output nodes
self.inputs = self.generate_matrix(False, 1, self.num_input)[0]
self.outputs = self.generate_matrix(False, 1, self.num_output)[0]
# Set the confusion matrix
self.confusion_matrix = self.generate_matrix(False, self.num_output, self.num_output)
# Set a matrix of random values (Weight of Edges)
self.weights_inputs_hidden = self.generate_matrix(True, self.num_input, self.num_hidden)
self.weights_hidden_hidden = self.generate_matrix(True, self.num_hidden, self.num_hidden)
self.weights_hidden_outputs = self.generate_matrix(True, self.num_hidden, self.num_output)
# Set the biases for hidden and output nodes with random values
self.hidden_biases_1 = self.generate_matrix(True, 1, self.num_hidden)[0]
self.hidden_biases_2 = self.generate_matrix(True, 1, self.num_hidden)[0]
self.output_biases = self.generate_matrix(True, 1, self.num_output)[0]
# Set a support list for the hidden gradients
self.hidden_outputs = self.generate_matrix(False, 1, self.num_hidden)[0]
self.hidden_hidden = self.generate_matrix(False, 1, self.num_hidden)[0]
# -------------------------------------------- #
# -------------- TRAIN NETWORK -------------- #
def train(self, train_set, validation_set):
# Define two errors (arbitrary values)
old_error = 100001
new_error = 100000
# Execute the train steps many times
for epoch in range(self.num_epochs):
# Iterate over the set elements
for train_i in range(len(train_set)):
# Get attribute and target values
values = self.divide_tuple(train_set, train_i)
# Compute the classification given the attribute values
self.evaluate_classification(values[0])
# Execute Backpropagation given the target values
self.backpropagation(values[1])
# Check if validation set is defined then validate the network
if len(validation_set) > 0:
tmp_error = old_error
old_error = new_error
# Compute the partial accuracy of network
error_sum = 0
for val_i in range(len(validation_set)):
# Get attribute and target values
values = self.divide_tuple(validation_set, val_i)
# Compute Output on the attribute values
self.evaluate_classification(values[0])
# Get the sum of output errors
for i in range(len(values[1])):
error_sum += (values[1][i] - self.outputs[i]) ** 2
# Compute Error = SUM[(Xt - Xn)^2] / 2
new_error = error_sum / len(validation_set)
# If minimum error has been reached stop
# It uses two errors to ensure that small fluctuations don't change early stopping in premature stopping
if ((old_error - new_error) <= self.early_stop) or ((tmp_error - old_error) <= self.early_stop):
break
# -------------------------------------------- #
# --------------- TEST NETWORK -------------- #
def test(self, test_set):
# Number of tests classified correctly
corrects = 0
# Iterate over the set elements
for test_i in range(len(test_set)):
# Get attribute and target values
values = self.divide_tuple(test_set, test_i)
# Compute Output on the attribute values
self.evaluate_classification(values[0])
# Get the index of the MAX value in the output
index = self.outputs.index(max(self.outputs))
# Get the index of the real class
real_max = values[1].index(max(values[1]))
# Update the confusion matrix
self.confusion_matrix[real_max][index] += 1
# Check if the index of the classification is the same of the target
if real_max == index:
corrects += 1
return [corrects, (len(test_set) - corrects)]
# -------------------------------------------- #
# ---------- NETWORK CLASSIFICATION --------- #
def evaluate_classification(self, attribute_values):
hidden_sums_1 = self.generate_matrix(False, 1, self.num_hidden)[0]
hidden_sums_2 = self.generate_matrix(False, 1, self.num_hidden)[0]
output_sums = self.generate_matrix(False, 1, self.num_output)[0]
############################################
# STEP 0: Initialization of Input nodes #
############################################
# Copy attribute values to input nodes. #
############################################
self.inputs = attribute_values[:]
############################################
# STEP 1: Go from Input to Hidden1 nodes #
############################################
# 1A. Initialize hidden_sums with the sum #
# of input value multiplied by his weight. #
# 1B. Sum the previous number by the bias #
# of the respective hidden node. #
# 1C. Active the hidden node. #
############################################
for eo_i in range(self.num_hidden):
# Step 1A
for eo_j in range(self.num_input):
hidden_sums_1[eo_i] += (self.inputs[eo_j] * self.weights_inputs_hidden[eo_j][eo_i])
# Step 1B
hidden_sums_1[eo_i] += self.hidden_biases_1[eo_i]
# Step 1C
self.hidden_hidden[eo_i] = self.hidden_activation(hidden_sums_1[eo_i])
############################################
# STEP 2: Go from Hidden1 to Hidden2 nodes #
############################################
# 1A. Initialize hidden_sums with the sum #
# of input value multiplied by his weight. #
# 1B. Sum the previous number by the bias #
# of the respective hidden node. #
# 1C. Active the hidden node. #
############################################
for eo_i in range(self.num_hidden):
# Step 1A
for eo_j in range(self.num_hidden):
hidden_sums_2[eo_i] += (self.hidden_hidden[eo_j] * self.weights_hidden_hidden[eo_j][eo_i])
# Step 1B
hidden_sums_2[eo_i] += self.hidden_biases_2[eo_i]
# Step 1C
self.hidden_outputs[eo_i] = self.hidden_activation(hidden_sums_2[eo_i])
############################################
# STEP 3: Go from Hidden1 to Output nodes #
############################################
# 1A. Initialize output_sums with the sum #
# of hidden value multiplied by his weight.#
# 1B. Sum the previous number by the bias #
# of the respective output node. #
# 1C. Active the output node. #
############################################
for eo_i in range(self.num_output):
# Step 2A
for eo_j in range(self.num_hidden):
output_sums[eo_i] += (self.hidden_outputs[eo_j] * self.weights_hidden_outputs[eo_j][eo_i])
# Step 2B
output_sums[eo_i] += self.output_biases[eo_i]
# Step 2C
self.outputs = self.output_activation(output_sums)
# -------------------------------------------- #
# ------------- BACKPROPAGATION ------------- #
def backpropagation(self, target):
#############################################
# STEP 0: Initialization of required Lists #
#############################################
# Set the gradient lists
hidden_gradients_1 = self.generate_matrix(False, 1, self.num_hidden)[0]
hidden_gradients_2 = self.generate_matrix(False, 1, self.num_hidden)[0]
output_gradients = self.generate_matrix(False, 1, self.num_output)[0]
# Set the lists to store delta values. This deltas will be used with momentum
input_hidden_deltas = self.generate_matrix(False, self.num_input, self.num_hidden)
hidden_hidden_deltas = self.generate_matrix(False, self.num_hidden, self.num_hidden)
hidden_output_deltas = self.generate_matrix(False, self.num_hidden, self.num_output)
# Set the lists to store bias values. This biases will be used with momentum
hidden_bias_deltas_1 = self.generate_matrix(False, 1, self.num_hidden)[0]
hidden_bias_deltas_2 = self.generate_matrix(False, 1, self.num_hidden)[0]
output_bias_deltas = self.generate_matrix(False, 1, self.num_output)[0]
#############################################
# STEP 1: Compute Gradients #
#############################################
# Gradients are values that measure of how #
# far off, and in what direction (positive #
# or negative), the current neural network #
# output values are, compared to the target #
#############################################
# The gradient must be calculated from the #
# right to the left, because the gradients #
# of the hidden nodes depend on the values #
# of the gradients of output nodes. #
#############################################
# Output Gradients: output_activation_derivative * (desired_target - computed_output)
for bp_i in range(self.num_output):
# Calculate the error: difference between target and output
error = target[bp_i] - self.outputs[bp_i]
# Calculate gradient
output_gradients[bp_i] = self.output_activation_derivative(self.outputs[bp_i]) * error
# Hidden Gradients: hidden_activation_derivative * sum(G * W)
# G = All output gradients
# W = Weights from Hidden_2 to Output
for bp_i in range(self.num_hidden):
# Calculate the error: the sum of output gradients * hidden to output weights
error = 0
for bp_j in range(self.num_output):
error += output_gradients[bp_j] * self.weights_hidden_outputs[bp_i][bp_j]
# Calculate gradient
hidden_gradients_2[bp_i] = self.hidden_activation_derivative(self.hidden_outputs[bp_i]) * error
# Hidden Gradients: hidden_activation_derivative * sum(G * W)
# G = All hidden gradients
# W = Weights from Hidden_1 to Hidden_2
for bp_i in range(self.num_hidden):
# Calculate the error: the sum of hidden gradients * hidden to hidden weights
error = 0
for bp_j in range(self.num_hidden):
error += hidden_gradients_2[bp_j] * self.weights_hidden_hidden[bp_i][bp_j]
# Calculate gradient
hidden_gradients_1[bp_i] = self.hidden_activation_derivative(self.hidden_hidden[bp_i]) * error
#############################################
# STEP 2: Update Weights #
#############################################
# Weight Formula: Delta + (PWD * Momentum) #
#############################################
# Delta = GTN * OFN * LR #
# GTN = Gradient To-Node #
# OFN = Output From-Node #
# LR = Learning Rate #
# PWD = Previous Weight Delta #
#############################################
# Hidden_2 -> Output
for bp_i in range(self.num_hidden):
for bp_j in range(self.num_output):
# Calculate the delta
delta_value = output_gradients[bp_j] * self.hidden_outputs[bp_i] * self.learning_rate
# Calculate the increment of the new weight
increment = hidden_output_deltas[bp_i][bp_j] * self.momentum
# Calculate the new weight
self.weights_hidden_outputs[bp_i][bp_j] += delta_value + increment
# Store delta for the next iteration
hidden_output_deltas[bp_i][bp_j] = delta_value
# Hidden_1 -> Hidden_2
for bp_i in range(self.num_hidden):
for bp_j in range(self.num_hidden):
# Calculate the delta
delta_value = hidden_gradients_2[bp_j] * self.hidden_hidden[bp_i] * self.learning_rate
# Calculate the increment of the new weight
increment = hidden_hidden_deltas[bp_i][bp_j] * self.momentum
# Calculate the new weight
self.weights_hidden_hidden[bp_i][bp_j] += delta_value + increment
# Store delta for the next iteration
hidden_hidden_deltas[bp_i][bp_j] = delta_value
# Input -> Hidden_1
for bp_i in range(self.num_input):
for bp_j in range(self.num_hidden):
# Calculate the delta
delta_value = hidden_gradients_1[bp_j] * self.inputs[bp_i] * self.learning_rate
# Calculate the increment of the new weight
increment = input_hidden_deltas[bp_i][bp_j] * self.momentum
# Calculate the new weight
self.weights_inputs_hidden[bp_i][bp_j] += delta_value + increment
# Store delta for the next iteration
input_hidden_deltas[bp_i][bp_j] = delta_value
#############################################
# STEP 3: Update Biases #
#############################################
# Bias Formula: Delta + (PWD * Momentum) #
#############################################
# Delta = CG * LR #
# CG = Gradient of Current Node #
# LR = Learning Rate #
# PWD = Previous Weight Delta #
#############################################
# Output Biases
for bp_i in range(self.num_output):
# Calculate the delta
delta_value = output_gradients[bp_i] * self.learning_rate
# Calculate the new bias
self.output_biases[bp_i] += delta_value + (output_bias_deltas[bp_i] * self.momentum)
# Store delta for the next iteration
output_bias_deltas[bp_i] = delta_value
# Hidden_2 Biases
for bp_i in range(self.num_hidden):
# Calculate the delta
delta_value = hidden_gradients_2[bp_i] * self.learning_rate
# Calculate the new bias
self.hidden_biases_2[bp_i] += delta_value + (hidden_bias_deltas_2[bp_i] * self.momentum)
# Store delta for the next iteration
hidden_bias_deltas_2[bp_i] = delta_value
# Hidden_1 Biases
for bp_i in range(self.num_hidden):
# Calculate the delta
delta_value = hidden_gradients_1[bp_i] * self.learning_rate
# Calculate the new bias
self.hidden_biases_1[bp_i] += delta_value + (hidden_bias_deltas_1[bp_i] * self.momentum)
# Store delta for the next iteration
hidden_bias_deltas_1[bp_i] = delta_value
# -------------------------------------------- #
# ---------- ACTIVATION FUNCTIONS ----------- #
################################################
# HYPERBOLIC TANGENT #
################################################
# Formula: #
# (1 - e^(-2 * x)) #
# TANH = ---------------- #
# (1 + e^(-2 * x)) #
# #
################################################
# Derivative: #
# #
# TANH' = (1 - TANH(x)) * (1 + TANH(x)) #
# #
################################################
# HIDDEN ACTIVATION NODES #
################################################
@staticmethod
def hidden_activation(x):
if x > 20:
return 1.0
elif x < -20:
return -1.0
else:
return (1 - math.exp(-2 * x)) / (1 + math.exp(-2 * x))
@staticmethod
def hidden_activation_derivative(x):
return (1 - x) * (1 + x)
################################################
# SOFTMAX FUNCTION (NORMALIZED EXPONENTIAL) #
################################################
# It normalizes the values of a list in order #
# to reduce the influence of extreme values #
# or outliers in the data without removing #
# them from the dataset. #
################################################
# Formula: #
# e^(x[i]) #
# SM = --------------- #
# sum(e^(x[i])) #
# #
################################################
# Derivative: #
# #
# SM' = (1 - SM(x)) * SM(x) #
# #
################################################
# OUTPUT ACTIVATION NODES #
################################################
@staticmethod
def output_activation(x):
normalized = [0] * len(x)
summation = 0
# Compute the sum
for sm_i in range(len(x)):
summation += math.exp(x[sm_i])
# Normalize all values
for sm_j in range(len(x)):
normalized[sm_j] = math.exp(x[sm_j]) / summation
return normalized
@staticmethod
def output_activation_derivative(x):
return (1 - x) * x
# -------------------------------------------- #
# ------------ HELPER FUNCTIONS ------------- #
# Generate a random number between lower and upper bound
def generate_rand(self):
return ((self.lower_bound - self.upper_bound) * random.random()) + self.upper_bound
# Generate a matrix given the numbers of rows and columns and fill it with 0 or random number
def generate_matrix(self, rand, rows, columns):
matrix = []
for i in range(rows):
row = []
for j in range(columns):
if rand:
row.append(self.generate_rand())
else:
row.append(0)
matrix.append(row)
return matrix
# Divide the tuples of dataset in attribute and target lists
def divide_tuple(self, data, index):
attribute_values = [0] * self.num_input
target_values = [0] * self.num_output
# Get attributes elements {[1,2,3,4,5] => A=[1,2,3]}
for data_j in range(self.num_input):
attribute_values[data_j] = data[index][data_j]
# Get target elements {[1,2,3,4,5] => T=[4,5]}
for data_j in range(self.num_output):
target_values[data_j] = data[index][data_j + self.num_input]
return [attribute_values, target_values]
# -------------------------------------------- #