-
Notifications
You must be signed in to change notification settings - Fork 0
/
learningOR.py
102 lines (85 loc) · 2.68 KB
/
learningOR.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
from random import uniform
import matplotlib.pyplot as plt
or_examples = [ [0,0,0],
[0,1,1],
[1,0,1],
[1,1,1] ]
to_plot = []
current = 0
iterations = 20
def get_x1(example):
return example[0]
def get_x2(example):
return example[1]
def get_y(example):
return example[2]
class Perceptron:
def __init__(self, w1, w2, b):
self._w1 = w1
self._w2 = w2
self._b = b
self._alpha = 0.15 #learning rate
def binary_step(self, output):
if(output > 0):
return 1
return 0
def output(self, example):
return self._w1*get_x1(example) + self._w2*get_x2(example) + self._b
def update(self, example):
output = self.binary_step(self.output(example))
y = get_y(example)
if(output != y):
self.update_weights(example, output)
return self.percent_error(output, y)
def update_weights(self, example, output):
delta = get_y(example) - output
self._w1 += self._alpha * delta * get_x1(example)
self._w2 += self._alpha * delta * get_x2(example)
self._b += self._alpha * delta
def percent_error(self, output, y):
return abs(self.binary_step(output)-y)*100
def init_perceptron():
w1 = uniform(-1, 1)
w2 = uniform(-1, 1)
b = uniform(-1, 1)
# w1 = 0.5
# w2 = 0.5
# b = 0.5
return Perceptron(w1, w2, b)
def learn(perceptron):
for i in range(iterations):
avg_error = 0
for example in or_examples:
avg_error += perceptron.update(example)
avg_error /= len(or_examples)
to_plot.append(avg_error)
def test(perceptron):
avg_error = 0
for example in or_examples:
print_example(example)
y = get_y(example)
output = perceptron.output(example)
avg_error += perceptron.percent_error(output, y)
print(" ["+str( perceptron.binary_step(output) ) +"]")
avg_error /= len(or_examples)
to_plot.append(avg_error)
def print_example(example):
print(str(get_x1(example)) + " OR "+str(get_x2(example))+" = "+str(get_y(example)), end="")
def plot():
plt.close()
plt.plot([i for i in range(len(to_plot))], to_plot, '-o')
plt.ylim(0,100)
# plt.show()
plt.savefig("error_rate"+str(current)+".png")
def run():
global current, to_plot
print(" "+str(current)+" =====================================================")
to_plot = []
perceptron = init_perceptron()
learn(perceptron)
test(perceptron)
plot()
current += 1
if __name__ == "__main__":
for i in range(10):
run()