Search code examples
pythonscikit-learntheanolasagne

Incorrect shape for arguments to lasagne function


I'm trying to build a neural network regressor using the scikit-neuralnetwork library.

As I understand it, ny NN seems to be being built fine, but I keep running into the following error at the nn.predict() call:

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$ 

My code is as follows:

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])

Might someone here know what's going wrong here? Any help would be greatly appreciated.


Solution

  • The problem is that the model expects its input to be a matrix but you're providing a vector.

    In the line

    print nn.predict(X_train[0])
    

    why do you only pass the first row of X_train?

    I expect if you passed the whole matrix, i.e.

    print nn.predict(X_train)
    

    or stacked the first row so it's passed as a matrix with only one row:

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

    then it may work as expected.