-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
67 lines (57 loc) · 1.75 KB
/
train.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
import layer
import data
import numpy as np
import modelParser as mp
filepath = "./model.txt"
print("Running...\n\n")
train_images, train_labels = data.get_training_data()
parsed_file = mp.Parser(filepath, 3)
"""
Initialize Neural layers.
"""
l1 = layer.Layer(784, 784, parsed_file.data[0])
l2 = layer.Layer(784, 10, parsed_file.data[1])
l3 = layer.Layer(10, 10, parsed_file.data[2])
"""
Loops 3 times over the entire dataset of 60000 pictures.
Prints the overall accuracy of our network after each iteration
"""
for i in range(3):
number_of_correct = 0
total_number = 0
counter = 0
for img, label in zip(train_images, train_labels):
"""
Forward propagation
"""
l1.forward(img)
l2.forward(l1.output)
l3.forward(l2.output)
pred_max = np.argmax(l3.output)
target_max = np.argmax(label)
if pred_max == target_max:
number_of_correct += 1
total_number += 1
if counter % 100 == 0:
print("running accuracy: " + str((number_of_correct / total_number) * 100) + "%")
counter += 1
"""
Back Propagation to adjust weights
"""
l3.backward_start(label)
l2.backward_next(l3.output, l3.weight_matrix)
l1.backward_next(l2.output, l2.weight_matrix)
print("FINAL ACCURACY OF ITERATION " + str(i) + ": " + str((number_of_correct / total_number) * 100) + "%")
file_mode = "w"
content_to_write = ""
for n in l1.weight_matrix:
content_to_write += str(n)
content_to_write += "%"
for n in l2.weight_matrix:
content_to_write += str(n)
content_to_write += "%"
for n in l3.weight_matrix:
content_to_write += str(n)
with open(filepath, file_mode) as file:
file.write(content_to_write)
print("\n\nFinished...")