I am experimenting with neural networks in python with PyBrain.
Here is the problem I'm trying to solve: Given a sorted list of integers of size 5, where each integer is in the range 0-12, determine whether at least two of the numbers are identical.
For example, [1,3,5,7,11] has 5 distinct numbers whereas [1,1,3,4,5] does not.
I'm using PyBrain's backpropagation trainer with 2 hidden layers -- 20 neurons on each. I have experimented with a lot of different topologies, so this shouldn't be the source of the problem.
I divide each number by 12 so that each value is in the range 0-1.
When training the network, the trainer reports a constant error and the network never identifies an input as having duplicate entries.
Here is my code:
from pybrain.datasets import ClassificationDataSet
from pybrain.utilities import percentError
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure.modules import SoftmaxLayer
alldata = ClassificationDataSet(5,1, nb_classes=2)
#Build our training data
for a in range(0,13):
for b in range(a, 13):
for c in range(b, 13):
for d in range(c, 13):
for e in range(d, 13):
inputList = [a,b,c,d,e]
for j in range(len(inputList)):
inputList[j] = float(inputList[j])/12
inputSet = set(inputList)
kclass = 0
if (len(inputSet) == len(inputList)):
kclass = 1
alldata.addSample(inputList, [kclass])
tstdata, trndata = alldata.splitWithProportion(0.25)
trndata._convertToOneOfMany()
tstdata._convertToOneOfMany()
#Build network with 20 neurons on each of 2 hidden layers
fnn = buildNetwork(trndata.indim, 20,20, trndata.outdim, outclass=SoftmaxLayer)
trainer = BackpropTrainer(fnn, dataset=trndata, momentum=0.1, verbose=True, weightdecay=0.01)
#Train network for 5 epochs
trainer.trainEpochs(5)
trnresult = percentError( trainer.testOnClassData(),
trndata['class'] )
tstresult = percentError( trainer.testOnClassData(
dataset=tstdata ), tstdata['class'] )
print "epoch: %4d" % trainer.totalepochs, \
" train error: %5.2f%%" % trnresult, \
" test error: %5.2f%%" % tstresul