烤宽面条函数参数的形状不正确

Incorrect shape for arguments to lasagne function

我正在尝试使用 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])

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

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

行中

print nn.predict(X_train[0])

为什么只传第一行X_train

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

print nn.predict(X_train)

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

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

然后它可能会按预期工作。