
Found the following public domain code for a simple neural network implementation in Python (The training data is mine) -
# Back-Propagation Neural Networks
#
# Written in Python. See http://www.python.org/
# Placed in the public domain.
# Neil Schemenauer
import math
import random
import string
random.seed(0)
# calculate a random number where: a <= rand < fill="0.0):" m =" []" ni =" ni" nh =" nh" no =" no" ai =" [1.0]*self.ni" ah =" [1.0]*self.nh" ao =" [1.0]*self.no" wi =" makeMatrix(self.ni," wo =" makeMatrix(self.nh," ci =" makeMatrix(self.ni," co =" makeMatrix(self.nh," sum =" 0.0" sum =" sum" sum =" 0.0" sum =" sum" output_deltas =" [0.0]" error =" targets[k]-self.ao[k]" hidden_deltas =" [0.0]" error =" 0.0" error =" error" change =" output_deltas[k]*self.ah[j]" change =" hidden_deltas[j]*self.ai[i]" error =" 0.0" error =" error">', self.update(p[0])
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]
def train(self, patterns, iterations=10000, N=0.5, M=0.1):
# N: learning rate
# M: momentum factor
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:
print 'error %-14f' % error
def demo():
# Teach network
pat = [
[[0,0,0,1], [0]],
[[0,0,1,0], [0]],
[[0,1,0,0], [0]],
[[1,0,0,0], [0]],
[[0,0,1,1], [0]],
[[0,1,1,0], [1]],
[[1,1,0,0], [1]],
[[1,0,0,1], [0]],
[[0,1,0,1], [0]],
[[1,0,1,0], [0]],
[[1,1,1,0], [0]],
[[1,1,0,1], [0]],
[[1,0,1,1], [0]],
[[0,1,1,1], [0]],
[[1,1,1,1], [1]],
]
# create a network with two input, two hidden, and one output nodes
n = NN(4, 16, 1)
# train it with some patterns
n.train(pat)
# test it
n.test(pat)
if __name__ == '__main__':
demo()