TensorFlow Model Training: InvalidArgumentError: Incompatible shapes: [8,10] vs. [32,1]

Viewed 340

I am implementing LeNet-5 Model. I get the following error at random epochs and at random steps within an epoch. And sometimes it just works without any issues.

I tried to replace the input x_train and y_train with imageDataGenerator

Here is the link to the code where I'm facing the issue.

Epoch 1/200
 1/10 [==>...........................] - ETA: 3s - loss: 0.1823 - accuracy: 0.9375
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-20-a90687551bd7> in <module>()
      8           , epochs=number_of_epochs
      9           , steps_per_epoch = 10
---> 10           , validation_data = datagen.flow(x_train, y_train, batch_size=batch_size, subset='validation')
     11           )

6 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  Incompatible shapes: [8,10] vs. [32,1]
     [[node gradient_tape/categorical_crossentropy/softmax_cross_entropy_with_logits/mul (defined at <ipython-input-20-a90687551bd7>:10) ]] [Op:__inference_train_function_129979]

Function call stack:
train_function
2 Answers

After some trial and error, this is the finding: The model had initially used Average Pooling, but replacing it with Max Pooling removed this error.

I would still like to know the reason behind the behavior. Any further insights would be highly appreciated

From what I see the problem comes from the computation of the loss, your label array is of shape (8,10) while the model output array is of shape (32,1). Is it possible that you corrected your model output dimension at the same time, because I don't see why changing Average Pooling to Max Pooling could change that. Moroever, in your Colab notebook the cell with Average Pooling seems to ran normally.

Related