# Created by Leo from: C:\Python23\Tom\leo\bpnn.leo # << bpnn declarations >> # Back-Propagation Neural Networks # # Written in Python. See http://www.python.org/ # # Neil Schemenauer import math import random import operator import string import gnosis.xml.pickle as XMLpickle random.seed(0) # calculate a random number where: a <= rand < b # -- end -- << bpnn declarations >> # << bpnn methods >> (1 of 4) def rand(a, b): return (b-a)*random.random() + a # << bpnn methods >> (2 of 4) # Make a matrix (we could use NumPy to speed this up) def makeMatrix(I, J, fill=0.0): m = [] for i in range(I): m.append([fill]*J) return m # << bpnn methods >> (3 of 4) class XML_NN: # << class NN2 methods >> (1 of 10) def __init__(self, ni, nh, no): self.ni = ni + 1 self.nh = nh self.no = no self.NNerror = '' self.testpattern = [] self.ai = [1.0]*self.ni self.ah = [1.0]*self.nh self.ao = [1.0]*self.no self.wi = makeMatrix(self.ni, self.nh) self.wo = makeMatrix(self.nh, self.no) for i in range(self.ni): for j in range(self.nh): self.wi[i][j] = rand(-2.0, 2.0) for j in range(self.nh): for k in range(self.no): self.wo[j][k] = rand(-2.0, 2.0) self.ci = makeMatrix(self.ni, self.nh) self.co = makeMatrix(self.nh, self.no) # << class NN2 methods >> (2 of 10) def update(self, inputs): if len(inputs) != self.ni-1: raise ValueError, 'wrong number of inputs' for i in range(self.ni-1): self.ai[i] = inputs[i] for j in range(self.nh): sum = 0.0 for i in range(self.ni): sum = sum + self.ai[i] * self.wi[i][j] self.ah[j] = 1.0/(1.0+math.exp(-sum)) for k in range(self.no): sum = 0.0 for j in range(self.nh): sum = sum + self.ah[j] * self.wo[j][k] self.ao[k] = 1.0/(1.0+math.exp(-sum)) return self.ao[:] # << class NN2 methods >> (3 of 10) def backPropagate(self, targets, N, M): if len(targets) != self.no: raise ValueError, 'wrong number of target values' output_deltas = [0.0] * self.no for k in range(self.no): ao = self.ao[k] output_deltas[k] = ao*(1-ao)*(targets[k]-ao) hidden_deltas = [0.0] * self.nh for j in range(self.nh): sum = 0.0 for k in range(self.no): sum = sum + output_deltas[k]*self.wo[j][k] hidden_deltas[j] = self.ah[j]*(1-self.ah[j])*sum for j in range(self.nh): for k in range(self.no): change = output_deltas[k]*self.ah[j] self.wo[j][k] = self.wo[j][k] + N*change + M*self.co[j][k] self.co[j][k] = change for i in range(self.ni): for j in range(self.nh): change = hidden_deltas[j]*self.ai[i] self.wi[i][j] = self.wi[i][j] + N*change + M*self.ci[i][j] self.ci[i][j] = change error = 0.0 for k in range(len(targets)): error = error + 0.5*(targets[k]-self.ao[k])**2 return error # << class NN2 methods >> (4 of 10) def test(self, patterns): self.testpattern = [] for p in patterns: self.testpattern.append(self.update(p[0])) return self.testpattern # << class NN2 methods >> (5 of 10) def XML_fromfile(self, filename): tom_file = file(filename,'r') newNetty = XMLpickle.load(tom_file) self.__init__(newNetty.ni -1 , newNetty.nh , newNetty.no) self.ah = newNetty.ah self.ai = newNetty.ai self.ao = newNetty.ao self.ci = newNetty.ci self.co = newNetty.co self.testpattern = newNetty.testpattern self.wi = newNetty.wi self.wo = newNetty.wo # << class NN2 methods >> (6 of 10) def XML_tofile(self, filename): openfile = file(filename , 'w+') XMLpickle.dump(self,openfile) openfile.close() # << class NN2 methods >> (7 of 10) def XML_fromstring(self, fromstring): newNetty = XMLpickle.loads(fromstring) self.__init__( newNetty.ni -1,newNetty.nh,newNetty.no) self.ah = newNetty.ah self.ai = newNetty.ai self.ao = newNetty.ao self.ci = newNetty.ci self.co = newNetty.co self.testpattern = newNetty.testpattern self.wi = newNetty.wi self.wo = newNetty.wo # << class NN2 methods >> (8 of 10) def XML_tostring(self): return XMLpickle.dumps(self) # << class NN2 methods >> (9 of 10) def weights(self): print 'Input weights:' for i in range(self.ni): print self.wi[i] print print 'Output weights:' for j in range(self.nh): print self.wo[j] # << class NN2 methods >> (10 of 10) def train(self, patterns, iterations=2000, N=0.5, M=0.1): for i in xrange(iterations): error = 0.0 for p in patterns: inputs = p[0] targets = p[1] self.update(inputs) error = error + self.backPropagate(targets, N, M) if i % 100 == 0: self.NNerror = 'error %-14f' % error # -- end -- << class NN2 methods >> # << bpnn methods >> (4 of 4) def demo(): # Teach network XOR function pat = [[[1,1], [0]],[[0,1], [1]],[[1,0], [1]],[[1,1], [0]]] # create a network with two input, three hidden, and one output nodes #n = NN(2, 3, 1) n = XML_NN(2, 3, 1) # train it with some patterns n.train(pat) # test it print n.test(pat) # to XML print n.XML_tostring() # -- end -- << bpnn methods >> if __name__ == '__main__': demo()