Nolearn DBM未能训练

时间:2014-12-21 14:31:17

标签: python neural-network

我正在尝试使用nolearn训练一个dbm,但是形成一些失败的原因。 每个列车示例都是1.0的向量,对于Test来说也是如此。当我使用“真实”输入时,我得到了同样的错误。

训练代码几乎与在nolearn文档中训练MNIST的训练代码相同。

# import the necessary packages
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn import datasets
from nolearn.dbn import DBN

# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split( features , targets , test_size = 0.33)

print trainX.shape
print trainY.shape

dbn = DBN(
[trainX.shape[1], 80, 80, trainY.shape[1]],
learn_rates = 0.3,
learn_rate_decays = 0.9,
epochs = 10,
verbose = 1)
dbn.fit(trainX, trainY)

# compute the predictions for the test data and show a classification
# report
preds = dbn.predict(testX)

由于某些原因我无法找到它失败:

100%

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-4-5324a7be4932> in <module>()
     19 epochs = 10,
     20 verbose = 1)
---> 21 dbn.fit(trainX, trainY)
     22 
     23 # compute the predictions for the test data and show a classification

/usr/local/lib/python2.7/dist-packages/nolearn/dbn.pyc in fit(self, X, y)
    388                 loss_funct,
    389                 self.verbose,
--> 390                 self.use_dropout,
    391                 )):
    392             losses_fine_tune.append(loss)

/usr/local/lib/python2.7/dist-packages/gdbn/dbn.pyc in fineTune(self, minibatchStream, epochs, mbPerEpoch, loss, progressBar, useDropout)
    207                 prog.tick()
    208             prog.done()
--> 209             yield sumErr/float(totalCases), sumLoss/float(totalCases)
    210 
    211     def totalLoss(self, minibatchStream, lossFuncts):

ZeroDivisionError: float division by zero

gnumpy: failed to import cudamat. Using npmat instead. No GPU will be used.
(1, 200)
(1, 125)
[DBN] fitting X.shape=(1, 200)
[DBN] layers [200, 80, 80, 125]
[DBN] Fine-tune...

3 个答案:

答案 0 :(得分:0)

验证trainY.shape[1]与您的班级数相同。

我曾经遇到过这个问题,并确保匹配修复它。

答案 1 :(得分:0)

我从pandas数据帧切换到numpy数组以避免此错误。

答案 2 :(得分:0)

我相信您的训练数据集可能很小。只需在DBN对象minibatch_size=1中再添加一个参数,默认情况下为minibatch_size=64。您可以根据自己的要求进一步调整。 例如:

dbn = DBN(
    [trainX.shape[1], 300, 5],
    learn_rates = 0.3,
    learn_rate_decays = 0.9,
    epochs = 10,
    verbose = 1,
    minibatch_size=1)