-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhebbian learning .py
More file actions
69 lines (52 loc) · 1.49 KB
/
hebbian learning .py
File metadata and controls
69 lines (52 loc) · 1.49 KB
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
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, w, delta_w):
print("i: "+ str(loop_var))
print("net: "+ str(net))
print("sig_net: "+ str(sig_net))
print("delta_w: "+ str(delta_w))
print("w: "+ str(w))
print("-------------------\n")
def compute():
try:
n = int(input("Enter number of input vectors: "))
x = []
r = 1 #Learning constant(c)
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_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)
#if len(np_wlist) != n:
# print("Init Weight Vector Error..")
delta_w = 0
for i in range(0,n):
net = np.transpose(np.asarray(w_list)).dot(np.asarray(x[i]))
#print(net)
sig_net = threshold(net)
#print(sig_net)
delta_w = r * sig_net * x[i]
#print(delta_w)
w_list = np.add(np.asarray(w_list),delta_w)
print_func(i, net, sig_net, w_list, delta_w)
except Exception as e:
print("Error.. "+(str(e)))
if __name__ == '__main__':
compute()