-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
116 lines (102 loc) · 3.23 KB
/
test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from learningXOR2 import Perceptron
from learningXOR2 import sigmoid
from learningXOR2 import d_sigmoid
from learningXOR2 import activation
from learningXOR2 import d_activation
from learningXOR2 import alpha
xor_examples = [ [0,0,0],
[0,1,1],
[1,0,1],
[1,1,0] ]
def get_x1(example):
return example[0]
def get_x2(example):
return example[1]
def get_y(example):
return example[2]
def run_tests():
test1()
test2()
test3()
test4()
test5()
test6()
test7()
print("all tests gud")
epsilon = 0.000001
def test1():
print("running test1")
p = Perceptron(1,1,1)
output = p.output(xor_examples[0])
if(output != 1):
raise ValueError("test1 failed")
def test2():
print("running test2")
p = Perceptron(1,1,1)
z = activation(p.output(xor_examples[0]))
if(abs(z - 0.7310585786300049) > epsilon):
raise ValueError("test2 failed")
def test3():
print("running test3")
a = Perceptron(1,1,1)
b = Perceptron(1,1,1)
p = Perceptron(1,1,1)
p.add_adjacent_perceptrons(a,b)
output = p.output(xor_examples[0])
if(abs(output - 2.4621171572600096) > epsilon):
raise ValueError("test3 failed")
def test4():
print("running test4")
a = Perceptron(1,1,1)
b = Perceptron(1,1,1)
p = Perceptron(1,1,1)
p.add_adjacent_perceptrons(a,b)
z = activation(p.output(xor_examples[0]))
if(abs(z - 0.9214430516601156) > epsilon):
raise ValueError("test4 failed")
def test5():
print("running test5")
p = Perceptron(1,1,1)
example = xor_examples[2]
s = p.output(example)
z = activation(s)
y = get_y(example)
updateW1 = alpha * (z - y) * d_activation(s) * get_x1(example) / len(xor_examples)
p.learn(example)
p.update()
if(abs(p.w1 - (1 - updateW1)) > epsilon):
raise ValueError("test5 failed. Expected: "+str(1 - updateW1)+" .Got: "+str(p.w1))
def test6():
print("running test6")
a = Perceptron(1,1,1)
b = Perceptron(1,1,1)
p = Perceptron(1,1,1)
p.add_adjacent_perceptrons(a,b)
example = xor_examples[2]
s = p.output(example)
z = activation(s)
zA = activation(p.adj_p1.output(example))
y = get_y(example)
updateW1 = alpha * (z - y) * d_activation(s) * zA / len(xor_examples)
p.learn(example)
p.update()
if(abs(p.w1 - (1 - updateW1)) > epsilon):
raise ValueError("test6 failed. Expected: "+str(1 - updateW1)+" .Got: "+str(p.w1))
def test7():
print("running test7")
a = Perceptron(1,1,1)
b = Perceptron(1,1,1)
p = Perceptron(1,1,1)
p.add_adjacent_perceptrons(a,b)
example = xor_examples[2]
s = p.output(example)
sA = p.adj_p1.output(example)
z = activation(s)
y = get_y(example)
updateWa1 = alpha * (z - y) * d_activation(s) * p.w1 * d_activation(sA) * get_x1(example) / len(xor_examples)
p.learn(example)
p.update()
if(abs(p.adj_p1.w1 - (1 - updateWa1)) > epsilon):
raise ValueError("test7 failed. Expected: "+str(1 - updateWa1)+" .Got: "+str(p.adj_p1.w1))
if __name__ == "__main__":
run_tests()