Skip to content
Advertisement

Tensorflow ValueError: Shapes (64, 1) and (1, 1) are incompatible

I’m trying to build a Siamese Neural Network to analyze the MNIST dataset, however when trying to fit the model to the dataset I encounter this problem according to which I have training data and labels shapes’ mismatch. I tried changing the loss function as well as tried to squeeze the labels array, and neither of “solutions” worked.

Here are the train and labels arrays’ shapes:

JavaScript

Here’s my model:

JavaScript

And finally here’s the code that generates the error itself:

JavaScript

Please help me to resolve the problem.

Advertisement

Answer

I was not able to reproduce the error using the below code. I suspect that your labels shape is different than the one you reported or it does not contain strictly binary data (0s and 1s) only.

Also, you should use tf.keras.losses.BinaryCrossentropy instead of tf.keras.losses.CategoricalCrossentropy as your labels should be binary with the sigmoid activation in the last layer.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement