-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmadaline.py
73 lines (67 loc) · 2.64 KB
/
madaline.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
from adaline import Adaline
class Madaline:
def __init__(self, n, hid, rate=0.3, thresh=1.2):
self.count = n+1;
self.hid_count = hid+1
self.rate = rate
self.thresh = thresh
self.units = [Adaline(n, rate, thresh) for i in range(hid)]
self.edges = [1]*(hid+1)
return
def activation(self, x):
if x > self.thresh:
return 1
else:
return -1
def fit(self, s, z, info=False):
# s is vector of training inputs
# z is a vector of outputs
# for each training pair
flag = True
while(flag):
flag = False
for i in range(len(s)):
result = [1]
result += [j.predict(s[i]) for j in self.units]
print(list(self.edges[j]*k for j, k in enumerate(result)))
result_sum = sum(self.edges[j]*k for j, k in enumerate(result))
to_update = []
# mark hidden nodes to update
for j in range(len(self.edges)):
alternative = 1 if result[j] == -1 else -1
new_activation = self.activation(result_sum - result[j] + alternative)
new_z = self.activation(new_activation)
if new_z == self.predict(s[i]):
to_update.append(j)
else:
print("correct")
print("UPDATE",to_update)
# update the marked nodes
for j in to_update:
flag = True
if j == 0:
self.edges[0] += z[i]*self.rate
else:
self.units[j-1].fit([s[i]],[z[i]])
if not info:
return
print("Training Complete")
for i in range(len(s)):
out = self.predict(s[i])
print("Output is: ",out," should be: ",z[i])
return
def predict(self, s):
result = [self.edges[0]]
result += [j.predict(s)*self.edges[i+1] for i, j in enumerate(self.units)]
result = self.activation(sum(result))
return result
if __name__ == "__main__":
n = int(input("The number of neurons:"))
h = int(input("The number of hidden neurons:"))
t = int(input("The number of inputs(training_data_count):"))
s = [list(map(int,input("Input? :").strip(" ").split(" "))) for i in range(t)]
z = [int(input("Output? :")) for i in range(t)]
net = Madaline(n, h, 0.2, 0.5)
net.fit(s, z, True)
for i in s:
print(net.predict(i))