-
Notifications
You must be signed in to change notification settings - Fork 60
/
nn.py
62 lines (53 loc) · 1.92 KB
/
nn.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
from data import get_mnist
import numpy as np
import matplotlib.pyplot as plt
"""
w = weights, b = bias, i = input, h = hidden, o = output, l = label
e.g. w_i_h = weights from input layer to hidden layer
"""
images, labels = get_mnist()
w_i_h = np.random.uniform(-0.5, 0.5, (20, 784))
w_h_o = np.random.uniform(-0.5, 0.5, (10, 20))
b_i_h = np.zeros((20, 1))
b_h_o = np.zeros((10, 1))
learn_rate = 0.01
nr_correct = 0
epochs = 3
for epoch in range(epochs):
for img, l in zip(images, labels):
img.shape += (1,)
l.shape += (1,)
# Forward propagation input -> hidden
h_pre = b_i_h + w_i_h @ img
h = 1 / (1 + np.exp(-h_pre))
# Forward propagation hidden -> output
o_pre = b_h_o + w_h_o @ h
o = 1 / (1 + np.exp(-o_pre))
# Cost / Error calculation
e = 1 / len(o) * np.sum((o - l) ** 2, axis=0)
nr_correct += int(np.argmax(o) == np.argmax(l))
# Backpropagation output -> hidden (cost function derivative)
delta_o = o - l
w_h_o += -learn_rate * delta_o @ np.transpose(h)
b_h_o += -learn_rate * delta_o
# Backpropagation hidden -> input (activation function derivative)
delta_h = np.transpose(w_h_o) @ delta_o * (h * (1 - h))
w_i_h += -learn_rate * delta_h @ np.transpose(img)
b_i_h += -learn_rate * delta_h
# Show accuracy for this epoch
print(f"Acc: {round((nr_correct / images.shape[0]) * 100, 2)}%")
nr_correct = 0
# Show results
while True:
index = int(input("Enter a number (0 - 59999): "))
img = images[index]
plt.imshow(img.reshape(28, 28), cmap="Greys")
img.shape += (1,)
# Forward propagation input -> hidden
h_pre = b_i_h + w_i_h @ img.reshape(784, 1)
h = 1 / (1 + np.exp(-h_pre))
# Forward propagation hidden -> output
o_pre = b_h_o + w_h_o @ h
o = 1 / (1 + np.exp(-o_pre))
plt.title(f"Subscribe if its a {o.argmax()} :)")
plt.show()