Skip to content

Instantly share code, notes, and snippets.

@rukshn
Created July 25, 2016 16:31
Show Gist options
  • Save rukshn/361a54eaec0266167051d2705ea08a5f to your computer and use it in GitHub Desktop.
Save rukshn/361a54eaec0266167051d2705ea08a5f to your computer and use it in GitHub Desktop.
XOR perseptron
import numpy as np
def S(x):
return 1/(1+np.exp(-x))
win = np.random.randn(2,2)
wout = np.random.randn(2,1)
eta = 0.25
# dummy values used to see if everything is correct manually
# win = [[1,1], [2,2]]
# wout = [[1],[2]]
obj = [[0,0],[1,0],[0,1],[1,1]]
target = [0,1,1,0]
epoch = int(100000)
emajor = ""
for r in range(0,epoch):
finalError = [0]
for xy in range(len(target)):
tar = target[xy]
fdata = obj[xy]
fdata = S(np.dot(1,fdata))
# print(fdata)
hnw = np.dot(fdata,win)
hnw = S(np.dot(fdata,win))
# print(hnw)
out = np.dot(hnw,wout)
out = S(out)
diff = tar-out
E = 0.5 * np.power(diff,2)
finalError += E
delta_out = (out-tar)*(out*(1-out))
# print(delta_out)
nindelta_out = delta_out * eta
wout_change = np.dot(nindelta_out[0], hnw)
for x in range(len(wout_change)):
change = wout_change[x]
wout[x] -= change
# I think this np.dot is wrong
# delta_in = np.dot(hnw,(1-hnw)) * np.dot(delta_out[0], wout)
# so I looped in in the old fashioned way
delta_in = []
for x in range(len(hnw)):
delta_in = np.append(delta_in, hnw[x]*(1-hnw[x])*delta_out[0]*wout[x])
#eta into delta
nindelta_in = eta * delta_in
for x in range(len(nindelta_in)):
midway = np.dot(nindelta_in[x], fdata)
for y in range(len(win)):
win[y][x] -= midway[y]
emajor += str(finalError[0]) + ",\n"
# save it in a file to plot it up
f = open('xor.csv','w')
f.write(emajor) # python will convert \n to os.linesep
f.close() # you can omit in most cases as the destructor will call it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment