简体   繁体   English

Pybrain神经网络:_convertToOneOfMany错误

[英]Pybrain neural network: _convertToOneOfMany error

I am new to Pybrain and trying to put together a neural network. 我是Pybrain的新手并试图组建一个神经网络。 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': 3个输入属性,2个类,将读取数组拆分,前3列为“输入”,最后一列为“目标”:

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: 构建一个简单网络的部分,非常适合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

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. 我不知道你的'target'字段元素有什么值,但是由于类标签从1开始而不是0,我在_convertToOneOfMany()中得到了同样的错误。

_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). _convertToOneOfMany()将数据集的'target'字段从类标签数组(如0,1,2,大小为[n_samples,1]]转换为标签数组,如100,010,001,大小为[n_samples,n_classes](所以它确实:0 - > 100,1-> 010和2-> 001)。 Consequently if you have 3 classes labelled as 1, 2 and 3 _convertToOneOfMany() will do 1->010, 2->001, 3-> error! 因此,如果您有3个标记为1,2和3的类_convertToOneOfMany()将执行1-> 010,2-> 001,3->错误!

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. 这个函数的代码在这里: https//github.com/pybrain/pybrain/blob/master/pybrain/datasets/classification.py和第144行,类标签(oldtarg [i])用作列索引为newtarg。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM