dnouri / nolearn

Combines the ease of use of scikit-learn with the power of Theano/Lasagne
http://danielnouri.org/notes/2014/12/17/using-convolutional-neural-nets-to-detect-facial-keypoints-tutorial/
MIT License
948 stars 260 forks source link

dimension problems when regression=False? #150

Closed stevenydc closed 9 years ago

stevenydc commented 9 years ago

I have a binary classification problem... Initially I had :

    nnet = NeuralNet(
        layers=[
            ('Input', InputLayer),
            ('Conv1', Conv1DLayer),
            ('Drop', DropoutLayer),
            ('Hidden', DenseLayer),
            ('Output', DenseLayer)
        ],
        ...
        Output_num_units=1,
        Output_nonlinearity=sigmoid,
        ...
        regression = True
    )

The network worked fine (as in no error) But then I realized that it doesn't make sense to have regression=True for a classification problem. So set regression = False.... but that gives me error when I try to train the net:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/stevenydc/anaconda/envs/EEG_venv/lib/python2.7/site-packages/nolearn/lasagne/base.py", line 415, in fit
    self.train_loop(X, y)
  File "/Users/stevenydc/anaconda/envs/EEG_venv/lib/python2.7/site-packages/nolearn/lasagne/base.py", line 461, in train_loop
    self.train_iter_, Xb, yb)
  File "/Users/stevenydc/anaconda/envs/EEG_venv/lib/python2.7/site-packages/nolearn/lasagne/base.py", line 515, in apply_batch_func
    return func(Xb) if yb is None else func(Xb, yb)
  File "/Users/stevenydc/anaconda/envs/EEG_venv/lib/python2.7/site-packages/theano/compile/function_module.py", line 534, in __call__
    allow_downcast=s.allow_downcast)
  File "/Users/stevenydc/anaconda/envs/EEG_venv/lib/python2.7/site-packages/theano/tensor/type.py", line 168, in filter
    data.shape))
TypeError: ('Bad input argument to theano function with name "/Users/stevenydc/anaconda/envs/EEG_venv/lib/python2.7/site-packages/nolearn/lasagne/base.py:390"  at index 1(0-based)', 'Wrong number of dimensions: expected 2, got 1 with shape (8,).')

So I forced the shape to be (8,1) by doing trainY = trainY.reshape(8,1)

This allowed me to train the network without error _IF_ eval_size = 0 if I set eval_size to 0.2, then I get the following error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/stevenydc/anaconda/envs/EEG_venv/lib/python2.7/site-packages/nolearn/lasagne/base.py", line 415, in fit
    self.train_loop(X, y)
  File "/Users/stevenydc/anaconda/envs/EEG_venv/lib/python2.7/site-packages/nolearn/lasagne/base.py", line 421, in train_loop
    X_train, X_valid, y_train, y_valid = self.train_split(X, y, self)
  File "/Users/stevenydc/anaconda/envs/EEG_venv/lib/python2.7/site-packages/nolearn/lasagne/base.py", line 110, in __call__
    kf = StratifiedKFold(y, round(1. / self.eval_size))
  File "/Users/stevenydc/anaconda/envs/EEG_venv/lib/python2.7/site-packages/sklearn/cross_validation.py", line 430, in __init__
    label_test_folds = test_folds[y == label]
IndexError: too many indices for array

And I can't seem to resolve this problem... please help!

dnouri commented 9 years ago

You want Output_num_units=2. y should be one dimensional and have shape (8,).

I've put together a little script with toy examples that demonstrates what kind of data is expected for classification and regression problems.

cancan101 commented 9 years ago

Perhaps give an example of how the user could one-hot encode the values of y him self? Right now nolearn, etc chokes if the y passed in for classification is one-hot encoded already.

cancan101 commented 9 years ago

Related #10.