Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- class XORNeuralNetwork(object):
- def __init__(self, inputs, outputs, f=None, fp=None, beta=1.2):
- self.inputs = []
- for i in inputs:
- self.inputs.append(list(i) + [1.])
- self.outputs = outputs
- if not f:
- self.f = lambda u: 1./(1. + math.e**(-beta*u))
- else:
- self.f = f
- if not fp:
- self.fp = lambda u: beta*self.f(u)*(1.-self.f(u))
- else:
- self.fp = fp
- self.s = [random.random() for i in range(3)]
- self.w = [[random.random() for i in range(3)], [random.random() for i in range(3)]] #could be better
- def printParams(self):
- print "\ts_1 = ", self.s[0]
- print "\ts_2 = ", self.s[1]
- print "\ts_3 = ", self.s[2]
- print "\tw_11 = ", self.w[0][0]
- print "\tw_12 = ", self.w[0][1]
- print "\tw_13 = ", self.w[0][2]
- print "\tw_21 = ", self.w[1][0]
- print "\tw_22 = ", self.w[1][1]
- print "\tw_23 = ", self.w[1][2]
- def calcX_pi(self, input, i, derive=False):
- if i == 3:
- return 1.
- if derive:
- fout = self.fp
- else:
- fout = self.f
- return fout(self.w[i-1][0]*input[0] + self.w[i-1][1]*input[1] + self.w[i-1][2]*input[2])
- def calcOutput(self, input, derive=False):
- if len(input) == 2:
- input = list(input) + [1.] # add u_p3
- if derive:
- fout = self.fp
- else:
- fout = self.f
- x_pi = [self.calcX_pi(input, i+1) for i in range(3)]
- return fout(self.s[0]*x_pi[0] + self.s[1]*x_pi[1] + self.s[2]*x_pi[2])
- def learn(self, printflag=False, c=0.1, eps=0.001):
- network_err = float("inf")
- n = 0
- while network_err > eps:
- new_s = [0,0,0]
- new_w = [[0,0,0],[0,0,0]]
- network_err = 0.
- for i in range(3):
- new_s[i] = self.s[i] - c * sum((self.calcOutput(self.inputs[p]) - self.outputs[p]) * self.calcOutput(self.inputs[p], True) * self.calcX_pi(self.inputs[p], i+1) for p in range(len(self.inputs)))
- for i in range(2):
- for j in range(3):
- new_w[i][j] = self.w[i][j] - c * sum((self.calcOutput(self.inputs[p]) - self.outputs[p]) * self.calcOutput(self.inputs[p], True) * self.s[i] * self.calcX_pi(self.inputs[p], i+1, True) * self.inputs[p][j] for p in range(len(self.inputs)))
- self.s = new_s[:]
- self.w[0] = new_w[0][:]
- self.w[1] = new_w[1][:]
- for p in range(len(self.inputs)):
- network_err += (self.calcOutput(self.inputs[p]) - self.outputs[p])**2
- n += 1
- if printflag:
- if n%2000 ==0:
- print n, network_err
- return n
- if __name__ == "__main__":
- inputs = ((0,0), (0,1), (1,0), (1,1))
- outputs = (0, 1, 1, 0)
- network = XORNeuralNetwork(inputs, outputs)
- print 'learning...'
- print '[iteration | current_error]'
- network.learn(True)
- print "\nparameters:"
- network.printParams()
- print "\noutputs:"
- for i in range(4):
- print inputs[i], '-->', network.calcOutput(inputs[i])
Add Comment
Please, Sign In to add comment