我正试图使用scikit神经网络库构建一个神经网络回归器。
据我所知,ny NN似乎构建得很好,但我在nn.predict()
调用时不断遇到以下错误:
rmichael@node:~/Sandbox$ sudo python NNScript.py
Traceback (most recent call last):
File "NNScript.py", line 15, in <module>
print nn.predict(X_train[0])
File "/users/rmichael/scikit-neuralnetwork/sknn/mlp.py", line 309, in predict
return super(Regressor, self)._predict(X)
File "/users/rmichael/scikit-neuralnetwork/sknn/mlp.py", line 256, in _predict
return self._backend._predict_impl(X)
File "/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 "/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,).')
rmichael@node:~/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])
这里有人知道这里出了什么问题吗?如有任何帮助,我们将不胜感激。
问题是模型期望其输入是矩阵,但您提供的是向量。
在线
print nn.predict(X_train[0])
为什么只通过X_train
的第一行?
我想如果你通过了整个矩阵,即
print nn.predict(X_train)
或者堆叠第一行,使其作为只有一行的矩阵通过:
print nn.predict(np.expand_dims(X_train[0], 0))
那么它可以如预期的那样工作。