2014-12-21 74 views
2

我試圖用nolearn訓練dbm,但形成一些失敗的原因。每個火車的例子都是1.0的矢量,與Test相同。當我使用「真實」輸入時,我得到了同樣的錯誤。Nolearn DBM無法訓練

培訓代碼幾乎與在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... 

回答

0

驗證比trainY.shape[1]相同的類的數量。

我曾經遇到過這個問題,並確保這些匹配的問題解決了它。

+0

這並沒有真正回答這個問題。如果您有不同的問題,可以通過單擊[提問](http://stackoverflow.com/questions/ask)來提問。您還可以[添加賞金](http://stackoverflow.com/help/privileges/set-bounties)在您擁有足夠的[聲譽](http://stackoverflow.com/help/)時吸引更多人關注此問題什麼聲譽)。 – Barranka

+0

@Barranka,正如我所說的,我有同樣的問題,至少對我來說這是解決方案(其中一個參數不是正確的)。 – Ins

0

我從熊貓數據框切換到numpy數組以避免此錯誤。

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)