-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathperceptron .py
85 lines (64 loc) · 1.91 KB
/
perceptron .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
import numpy as np
import time
def threshold(x):
if x >=0:
return 1
else:
return -1
def print_func(loop_var, net, sig_net, teacher_signal, w, delta_w = None):
print("i: "+ str(loop_var))
print("net: "+ str(net))
print("sig_net: "+ str(sig_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(float(ele))
np_wlist = np.array(w_list, dtype=np.float64)
#print(np_wlist)
delta_w = 0
for i in range(0,n):
#print(x[i])
#print(w_list)
#print(x[i])
net = np.transpose(np.asarray(w_list)).dot(np.asarray(x[i]))
#print(net)
sig_net = threshold(net)
#print(sig_net)
if sig_net != teacher_signal[i]:
delta_w = r * (teacher_signal[i] - sig_net) * x[i]
#print(delta_w)
w_list = np.add(np.asarray(w_list),delta_w)
else:
w_list = w_list
print_func(i, net, sig_net, teacher_signal[i], w_list, delta_w)
except Exception as e:
print("Error.. "+(str(e)))
if __name__ == '__main__':
compute()