Search code examples
pythontensorflowmachine-learningdata-sciencetraining-data

TensorFlow GradCAM - model.fit() - ValueError: Shapes (None, 1) and (None, 2) are incompatible


As part of assignment 4, Coursera CV TF course, my code fails in model.fit()

model.compile(loss='categorical_crossentropy',metrics=
   ['accuracy'],optimizer=tf.keras.optimizers.RMSprop(lr=0.001))
# shuffle and create batches before training

model.fit(train_batches,epochs=25)

with error:

ValueError: Shapes (None, 1) and (None, 2) are incompatible

Any hint at where problem might come from? I suspect bad format or type for train_batches:

train_data = tfds.load('cats_vs_dogs', split='train[:80%]', as_supervised=True) 
augmented_training_data = train_data.map(augmentimages)
train_batches = augmented_training_data.batch(32)

Solution

  • Although I am not familiar with the exact code of the architecture, I suspect it is this line:

    model.compile(loss='categorical_crossentropy',metrics=
       ['accuracy'],optimizer=tf.keras.optimizers.RMSprop(lr=0.001))
    

    You may be using categorical_crossentropy instead of binary_crossentropy for binary classification with 1 neuron at the output, but this is only an assumption considering I do not have the code and architecture to look at; in fact I am 99% that the issue is from there.