-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdelta learning rule.py
94 lines (70 loc) · 2.37 KB
/
delta learning rule.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
import numpy as np
import time, math
def threshold(x):
return round(((2 / (1 + math.exp(-x))) - 1), 3)
def print_func(loop_var, net, sig_net, sig_dash_net, teacher_signal, w, delta_w = None):
print("i: "+ str(loop_var))
print("net: "+ str(net))
print("sig_net: "+ str(sig_net))
print("sig_dash_net: "+ str(sig_dash_net))
print("delta_w: "+ str(delta_w))
print("teacher_signal: "+ str(teacher_signal))
print("w: "+ str(w))
print("-------------------\n")
def compute():
try:
n = int(input("Enter number of input vectors: "))
x = []
r = 0.1 #Learning rate
for i in range(0,n):
raw_str1 = str(input("Enter values for vector " + str(i+1) + ": "))
input_vector = raw_str1.split(' ')
#print(input_vector)
ip_list = []
for ele in input_vector:
ip_list.append(float(ele))
#print(ip_list)
np_list = np.array(ip_list, dtype=np.float64)
x.append(np_list)
raw_str2 = str(input("Enter values for teacher signal: "))
teacher_signal = raw_str2.split(' ')
teacher_signal = [int(x) for x in teacher_signal]
if len(teacher_signal) != n:
print("Teacher Signal length Error..")
time.sleep(3)
exit()
raw_str3 = str(input("Enter initial weight vector: "))
w = raw_str3.split(' ')
w_list = []
for ele in w:
w_list.append(round(float(ele), 3))
np_wlist = np.array(w_list, dtype=np.float64)
#print(np_wlist)
delta_w = 0
for i in range(0,n):
net = round(np.transpose(np.asarray(w_list)).dot(np.asarray(x[i])), 3)
#print(net)
sig_net = threshold(net)
sig_dash_net = round(0.5 * ( 1 - ((sig_net)**2)), 3)
print(sig_dash_net)
if sig_net != teacher_signal[i]:
rounded_delta_w = []
rounded_w_list = []
delta_w = (r * (teacher_signal[i] - sig_net) * sig_dash_net * x[i])
for ele in delta_w:
rounded_delta_w.append(round(ele, 3))
#print(delta_w)
w_list = np.add(np.asarray(w_list), rounded_delta_w)
for ele in w_list:
rounded_w_list.append(round(ele, 3))
w_list = rounded_w_list
else:
rounded_w_list = []
for ele in w_list:
rounded_w_list.append(round(ele, 3))
w_list = rounded_w_list
print_func(i, net, sig_net, sig_dash_net, teacher_signal[i], w_list, rounded_delta_w)
except Exception as e:
print("Error.. "+(str(e)))
if __name__ == '__main__':
compute()