2015-11-26 29 views
1

我正在嘗試使用scikit-neuralnetwork庫構建神經網絡迴歸器。千層麪函數參數的形狀不正確

據我瞭解,NY NN似乎正在建設很好,但我一直運行到下面的錯誤在nn.predict()電話:

[email protected]:~/Sandbox$ sudo python NNScript.py 
Traceback (most recent call last): 
    File "NNScript.py", line 15, in <module> 
    print nn.predict(X_train[0]) 
    File "https://stackoverflow.com/users/rmichael/scikit-neuralnetwork/sknn/mlp.py", line 309, in predict 
    return super(Regressor, self)._predict(X) 
    File "https://stackoverflow.com/users/rmichael/scikit-neuralnetwork/sknn/mlp.py", line 256, in _predict 
    return self._backend._predict_impl(X) 
    File "https://stackoverflow.com/users/rmichael/scikit-neuralnetwork/sknn/backend/lasagne/mlp.py", line 242, in _predict_impl 
    return self.f(X) 
    File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 786, in __call__ 
    allow_downcast=s.allow_downcast) 
    File "/usr/local/lib/python2.7/dist-packages/theano/tensor/type.py", line 177, in filter 
    data.shape)) 
TypeError: ('Bad input argument to theano function with name "https://stackoverflow.com/users/rmichael/scikit-neuralnetwork/sknn/backend/lasagne/mlp.py:199" at index 0(0-based)', 'Wrong number of dimensions: expected 2, got 1 with shape (59,).') 
[email protected]:~/Sandbox$ 

我的代碼如下:

import numpy as np 
from sknn.mlp import Regressor, Layer 

X_train = np.genfromtxt("OnlineNewsPopularity.csv", dtype=float, delimiter=',', skip_header=1, usecols=range(1,60)) 
y_train = np.genfromtxt("OnlineNewsPopularity.csv", dtype=float, delimiter=',', names=True, usecols=(60)) 

nn = Regressor(
    layers=[ 
     Layer("Rectifier", units=1), 
     Layer("Linear")], 
    learning_rate=0.02, 
    n_iter=1) 
nn.fit(X_train, y_train) 

print nn.predict(X_train[0]) 

這裏可能有人知道這裏出了什麼問題?任何幫助將不勝感激。

回答

1

問題是,模型期望它的輸入是一個矩陣,但你提供一個向量。

在行

print nn.predict(X_train[0]) 

你爲什麼只通過的X_train第一排?

我希望,如果你通過了整個矩陣,即

print nn.predict(X_train) 

左右,它是作爲一個矩陣通過只有一行堆疊的第一行:

print nn.predict(np.expand_dims(X_train[0], 0)) 

那麼它可以按預期工作。