Skip to content

Instantly share code, notes, and snippets.

@maazrk
Created September 26, 2017 15:03
Show Gist options
  • Save maazrk/c351a1ba3dc0887a7657139f94df4f00 to your computer and use it in GitHub Desktop.
Save maazrk/c351a1ba3dc0887a7657139f94df4f00 to your computer and use it in GitHub Desktop.
#Hebbian
from math import exp
def hebbian(x_list, w, c, flg):
o = 0
dptr = 0
for xx in x_list:
net = []
for i in range(len(xx)):
net.append(xx[i]*w[i])
print("net is ", sum(net))
if flg == 'bb':
if sum(net) < 0:
o = -1
else:
o = 1
else:
o = (2/(1 + exp(-1*sum(net))))-1
print("o is ", o)
delw = [c*o*x for x in xx]
print("Delta w is ", delw)
wtemp = w
w = []
for j in range(len(wtemp)):
w.append(delw[j]+wtemp[j])
print("w is ", w)
dptr += 1
print("Hebbian Learning Algorithm")
print("Enter no. of input arrays")
n = int(input())
x_list = []
print("Enter space separated arrays")
for h in range(n):
ss = list(map(float, input().split(" ")))
x_list.append(ss)
print("Enter bb for bipolar-binary and bc for bipolar-continuous")
flg = input()
print("Enter value of c")
c = int(input())
print("Enter space separated initial weights")
w = list(map(float, input().split(" ")))
hebbian(x_list, w, c, flg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment