-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
77 lines (58 loc) · 2.55 KB
/
main.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
import numpy as np
import matplotlib.pyplot as plt
import utils
images, labels = utils.load_dataset()
weights_input_to_hidden = np.random.uniform(-0.5, 0.5, (20, 784))
weights_hidden_to_output = np.random.uniform(-0.5, 0.5, (10, 20))
bias_input_to_hidden = np.zeros((20, 1))
bias_hidden_to_output = np.zeros((10, 1))
epochs = 3
e_loss = 0
e_correct = 0
learning_rate = 0.01
for epoch in range(epochs):
print(f"Epoch №{epoch}")
for image, label in zip(images, labels):
image = np.reshape(image, (-1, 1))
label = np.reshape(label, (-1, 1))
# Forward propagation (to hidden layer)
hidden_raw = bias_input_to_hidden + weights_input_to_hidden @ image
hidden = 1 / (1 + np.exp(-hidden_raw)) # sigmoid
# Forward propagation (to output layer)
output_raw = bias_hidden_to_output + weights_hidden_to_output @ hidden
output = 1 / (1 + np.exp(-output_raw))
# Loss / Error calculation
e_loss += 1 / len(output) * np.sum((output - label) ** 2, axis=0)
e_correct += int(np.argmax(output) == np.argmax(label))
# Backpropagation (output layer)
delta_output = output - label
weights_hidden_to_output += -learning_rate * delta_output @ np.transpose(hidden)
bias_hidden_to_output += -learning_rate * delta_output
# Backpropagation (hidden layer)
delta_hidden = np.transpose(weights_hidden_to_output) @ delta_output * (hidden * (1 - hidden))
weights_input_to_hidden += -learning_rate * delta_hidden @ np.transpose(image)
bias_input_to_hidden += -learning_rate * delta_hidden
# DONE
# print some debug info between epochs
print(f"Loss: {round((e_loss[0] / images.shape[0]) * 100, 3)}%")
print(f"Accuracy: {round((e_correct / images.shape[0]) * 100, 3)}%")
e_loss = 0
e_correct = 0
# CHECK CUSTOM
test_image = plt.imread("custom.png", format="png")
# Grayscale + Unit RGB + inverse colors
gray = lambda rgb : np.dot(rgb[... , :3] , [0.299 , 0.587, 0.114])
test_image = 1 - (gray(test_image).astype("float32") / 255)
# Reshape
test_image = np.reshape(test_image, (test_image.shape[0] * test_image.shape[1]))
# Predict
image = np.reshape(test_image, (-1, 1))
# Forward propagation (to hidden layer)
hidden_raw = bias_input_to_hidden + weights_input_to_hidden @ image
hidden = 1 / (1 + np.exp(-hidden_raw)) # sigmoid
# Forward propagation (to output layer)
output_raw = bias_hidden_to_output + weights_hidden_to_output @ hidden
output = 1 / (1 + np.exp(-output_raw))
plt.imshow(test_image.reshape(28, 28), cmap="Greys")
plt.title(f"NN suggests the CUSTOM number is: {output.argmax()}")
plt.show()