3

I am new to Pybrain and trying to put together a neural network. First, I came across the error described here:

AttributeError: 'SupervisedDataSet' object has no attribute '_convertToOneOfMany'

I tried the workaround described in the accepted answer of that thread. While it seems it works, it now gives me a new error. These are the relevant chunks of my code:

The part that reads the file into a classification dataset. 3 input attributes, 2 classes, splits the read array, first 3 columns to 'input' and the last one to 'target':

ds = ClassificationDataSet(inp=3, target=1, nb_classes=2)
tf = open('datafile.txt')
a = np.loadtxt(tf) 
a = np.hsplit(a, (3,4))
ds.setField('input', a[0])
ds.setField('target', a[1])

The part that builds a simple network, pretty standard for pybrain:

inLayer = SigmoidLayer(3)
hiddenLayer = SigmoidLayer(5)
outLayer = SigmoidLayer(2)

fnn.addInputModule(inLayer)
fnn.addModule(hiddenLayer)
fnn.addOutputModule(outLayer)

in_to_hidden = FullConnection(inLayer, hiddenLayer)
hidden_to_out = FullConnection(hiddenLayer, outLayer)

fnn.addConnection(in_to_hidden)
fnn.addConnection(hidden_to_out)

fnn.sortModules()

This is the workaround, as described above:

tstdata_temp, trndata_temp = ds.splitWithProportion(0.25)

tstdata = ClassificationDataSet(3, target=1, nb_classes=2)
for n in xrange(0, tstdata_temp.getLength()):
     tstdata.addSample( tstdata_temp.getSample(n)[0], tstdata_temp.getSample(n)[1] )

trndata = ClassificationDataSet(3, target=1, nb_classes=2)
for n in xrange(0, trndata_temp.getLength()):
     trndata.addSample( trndata_temp.getSample(n)[0], trndata_temp.getSample(n)[1] )

trndata._convertToOneOfMany()
tstdata._convertToOneOfMany()

And this is the error I am getting on the first convert line:

IndexError: index 2 is out of bounds for axis 1 with size 2
Community
  • 1
  • 1
Alex
  • 39
  • 5

1 Answers1

1

I don't know what values your 'target' field elements have, but I got the same error with _convertToOneOfMany() as a result of having class labels starting from 1 not 0.

_convertToOneOfMany() converts the 'target' field of a dataset from an array of class labels like 0, 1, 2 of size [n_samples,1] to an array of labels like 100, 010, 001 of size [n_samples,n_classes] (so it does: 0 -> 100, 1->010 and 2->001). Consequently if you have 3 classes labelled as 1, 2 and 3 _convertToOneOfMany() will do 1->010, 2->001, 3-> error!

The code for this function is here: https://github.com/pybrain/pybrain/blob/master/pybrain/datasets/classification.py and on line 144 the class labels (oldtarg[i]) are used as the column indices for newtarg.

mkw123
  • 11
  • 2