简体   繁体   中英

“ValueError: Shape mismatch” error in Lasagne for Neural Network

I'm trying to create a neural network based on theano/lasagne that will (essentially) attempt to do a multi-variable regression.

The meat of the code is:

    train_value = train_df.values[:, 0]
    train_data = train_df.values[:, 1:]
    #print "train:", train_data.shape, train_label.shape


    #test_data = test_df.values
    #print "test:", test_data.shape


    train_data = train_data.astype(np.float)
    train_value = train_value.astype(np.int32)



    fc_1hidden = NeuralNet(
        layers = [  # three layers: one hidden layer
            ('input', layers.InputLayer),
            ('hidden', layers.DenseLayer),
            ('dropout', layers.DropoutLayer),
            ('output', layers.DenseLayer),
            ],
        # layer parameters:
        input_shape = (None, 36),  # 36 rows of data 
        hidden_num_units = 100,  # number of units in hidden layer
        dropout_p = 0.25, # dropout probability
        output_nonlinearity = softmax,  # output layer uses softmax function
        output_num_units = 10,  # 10 labels

        # optimization method:
        #update = nesterov_momentum,
        update = sgd,
        update_learning_rate = 0.001,
        #update_momentum = 0.9,

        eval_size = 0.1,

        # batch_iterator_train = BatchIterator(batch_size = 20),
        # batch_iterator_test = BatchIterator(batch_size = 20),

        max_epochs = 100,  # we want to train this many epochs
        verbose = 1,
        )

fc_1hidden.fit(train_data, train_value)
plot_loss(fc_1hidden)

Here, train_value is just 1 column of (numerical) data that I want to train my NN to predict, and the following 57 columns (train_data) are all the parameters/values (all numbers) which should be weighted appropriately to predict the value in the first column.

However, when I run this script, I get the following error:

  Epoch  |  Train loss  |  Valid loss  |  Train / Val  |  Valid acc  |  Dur
--------|--------------|--------------|---------------|-------------|-------
Traceback (most recent call last):
  File "neuralnetwork.py", line 77, in <module>
    fc_1hidden.fit(train_data, train_value)
  File "/Users/spadavec/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py", line 150, in fit
    self.train_loop(X, y)
  File "/Users/spadavec/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py", line 188, in train_loop
    batch_train_loss = self.train_iter_(Xb, yb)
  File "/Users/spadavec/anaconda/lib/python2.7/site-packages/theano/compile/function_module.py", line 606, in __call__
    storage_map=self.fn.storage_map)
  File "/Users/spadavec/anaconda/lib/python2.7/site-packages/theano/compile/function_module.py", line 595, in __call__
    outputs = self.fn()
ValueError: Shape mismatch: x has 83 cols (and 29 rows) but y has 36 rows (and 100 cols)
Apply node that caused the error: Dot22(x_batch, W)
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]
Inputs shapes: [(29, 83), (36, 100)]
Inputs strides: [(664, 8), (800, 8)]
Inputs values: ['not shown', 'not shown']

I'm not sure where it is getting this shape--none of my data has 83 columns or rows. (note: I've tried to adapt this script, which originally was written to look at pictures of faces and guess where different parts were (eyes, nose, mouth, etc)).

I have written a much simpler version of this (sans-dropout method) in pybrain, but am trying to migrate to sklearn/lasagne/theano as it opens more doors.

Since you want to do regression, make sure to set the output type correctly:

output_nonlinearity = linear

Are you sure you also have 10 output units? I experienced some weird behavior in Lasagne. I think the API has changed over time and contains some bugs. I succeeded using the latest API demo and adapting it to my needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM