-
Notifications
You must be signed in to change notification settings - Fork 3
/
ANNtest.py
71 lines (58 loc) · 1.71 KB
/
ANNtest.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
import numpy as np
import matplotlib.pyplot as plt
# Simple implementation of an ANN, provides graphs showing
# change in total error per epoch and change in output per epoch.
def sigmoid(x):
return 1 / (1 + np.exp(-x))
WO = np.array([[0.1, 0.3], [0.4, 0.1], [0.5, 0.2]])
WH = np.array([[0.1, 0.2, 0.4], [0.3, 0.1, 0.5]])
I = np.array([0.1, 0.2, 0.3]).T
O = np.array([0.0, 0.5, 1.0])
a = 0.5
EM = list()
EPOCHS = 1000
OUT = np.empty([3, EPOCHS])
for i in range(0, EPOCHS):
netH = np.array([np.dot(I, WH[0]), np.dot(I, WH[1])])
# print("netH: ", netH)
outH = sigmoid(netH)
# print("outH: ", outH)
netO = np.array([np.dot(outH, WO[0]), np.dot(outH, WO[1]), np.dot(outH, WO[2])])
# print("netO", netO)
outO = sigmoid(netO)
# print("outO", outO)
sub = O - outO
E = (1 / 2) * np.dot(sub.T, sub)
print("TOTAL ERROR: ", E)
EM.append(E)
OUT[:, i] = outO
# do output layer
dOutOdNetO = outO * (1 - outO)
dEdOutO = (outO - O)
dNetOdWo = outH
dEdWo = dEdOutO * dOutOdNetO
dEdWo = np.outer(dEdWo, dNetOdWo)
# print("dEdWo: ", dEdWo)
# do hidden layer
dEdNetO = dEdOutO * dOutOdNetO
dNetOdOutH = WO
dEdOutH = np.dot(dEdNetO, dNetOdOutH)
dOutHdNetH = outH * (1 - outH)
dNetHdWh = I
dEdWh = dEdOutH * dOutHdNetH
dEdWh = np.outer(dEdWh, dNetHdWh)
# update weights
WO -= (a * dEdWo)
WH -= (a * dEdWh)
print(OUT)
plt.plot(EM)
plt.xlabel("Epoch")
plt.ylabel("Total Error")
plt.show()
plt.plot(OUT[0, :EPOCHS])
plt.plot(OUT[1, :EPOCHS])
plt.plot(OUT[2, :EPOCHS])
plt.xlabel("Epoch")
plt.ylabel("Output")
plt.axhline(0.5, color='0.8', linestyle='--')
plt.show()