千层面功能参数的形状不正确

时间:2015-11-26 19:52:28

标签: python scikit-learn theano lasagne

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

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

1 个答案:

答案 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))

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