-
Notifications
You must be signed in to change notification settings - Fork 0
/
normal_gates.py
60 lines (49 loc) · 1.97 KB
/
normal_gates.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
from Perceptron import Perceptron
from functions import *
mean_square_error = 0.0
def calculate(w1, x1, w2, x2, threshold, func):
test_perceptron = Perceptron()
test_perceptron.add_input()
test_perceptron.threshold = threshold
test_perceptron.In[0] = x1
test_perceptron.In[1] = x2
test_perceptron.weight[0] = w1
test_perceptron.weight[1] = w2
test_perceptron.activation_fun = determine_activation_function(func)
print('\n\nTesting Process')
test_perceptron.get_output()
return test_perceptron.out
def learn(in_data, out_data, ep, alpha, func):
p = Perceptron()
p.add_input()
p.activation_fun = determine_activation_function(func)
for epoch in range(ep):
print(f'Epoch # {epoch + 1}')
errors = list()
for i in range(4):
p.In[0], p.In[1] = in_data[i]
print(f'{p.In[0]} {p.In[1]}')
p.get_output()
e = my_formatter(out_data[i] - p.out)
errors.append(my_formatter(e ** 2))
print(f'a is {p.out}, error = {e}')
for j in range(len(p.weight)):
tmp = p.weight[j]
p.weight[j] += alpha * p.In[j] * e
p.weight[j] = my_formatter(p.weight[j])
p.threshold += alpha * p.wIn * e
p.threshold = my_formatter(p.threshold)
print(f'Weight is updated by, {my_formatter(p.weight[j]-tmp)}, so weight[{j}] = {p.weight[j]}')
print(f'New threshold = {p.threshold}')
global mean_square_error
for error in errors:
mean_square_error += error
mean_square_error /= 4
mean_square_error = my_formatter(mean_square_error)
print(f'errors = {errors}')
errors.clear()
print(f'mean_square_error = {mean_square_error}, epoch = {epoch + 1}')
if mean_square_error <= 0.0001:
break
print(f'Threshold = {p.threshold}\n')
return p.weight[0], p.weight[1], p.threshold