1

我正在尝试使用scikit-neuralnetwork库构建神经网络回归器。

据我了解,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])

可能有人知道这里出了什么问题吗?任何帮助将不胜感激。

4

1 回答 1

1

问题是模型期望它的输入是一个矩阵,但你提供了一个向量。

在行

print nn.predict(X_train[0])

为什么你只通过第一排X_train

我希望你通过整个矩阵,即

print nn.predict(X_train)

或堆叠第一行,使其作为只有一行的矩阵传递:

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

那么它可能会按预期工作。

于 2015-11-27T10:26:51.427 回答