【问题标题】:Incorrect shape for arguments to lasagne function千层面函数的参数形状不正确
【发布时间】:2015-11-26 19:52:28
【问题描述】:

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

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

【问题讨论】:

    标签: python scikit-learn theano lasagne


    【解决方案1】:

    问题在于模型希望其输入是矩阵,但您提供的是向量。

    排队

    print nn.predict(X_train[0])
    

    为什么只通过X_train的第一行?

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

    print nn.predict(X_train)
    

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多