简体   繁体   中英

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)

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.

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