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

Hi, I am trying to experiment with image recognition AI by modifying existing models, however I came across this error when running my code (I am using Kaggle):

ValueError Traceback (most recent call last)
Cell In[25], line 1
----> 1 history =
2 train_generator,
3 steps_per_epoch=train_groups,
4 epochs=20,
5 validation_data=(val_generator, valid_labels_one_hot),
6 validation_steps=valid_groups,
7 verbose=1,
8 callbacks=[
9 keras.callbacks.EarlyStopping(monitor=‘val_accuracy’, patience=5, restore_best_weights=True),
10 keras.callbacks.ReduceLROnPlateau(monitor=‘val_loss’, factor=0.7, patience=2, verbose=1),
11 keras.callbacks.ModelCheckpoint(filepath=“intial_model.keras”, save_best_only=True, monitor=“val_loss”)
12 ])

File /opt/conda/lib/python3.10/site-packages/keras/utils/, in filter_traceback..error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.traceback)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
—> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb

File /tmp/, in outer_factory..inner_factory..tf__train_function(iterator)
13 try:
14 do_return = True
—> 15 retval
= ag
_.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False

ValueError: in user code:

File "/opt/conda/lib/python3.10/site-packages/keras/engine/", line 1284, in train_function  *
    return step_function(self, iterator)
File "/opt/conda/lib/python3.10/site-packages/keras/engine/", line 1268, in step_function  **
    outputs =, args=(data,))
File "/opt/conda/lib/python3.10/site-packages/keras/engine/", line 1249, in run_step  **
    outputs = model.train_step(data)
File "/opt/conda/lib/python3.10/site-packages/keras/engine/", line 1051, in train_step
    loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/opt/conda/lib/python3.10/site-packages/keras/engine/", line 1109, in compute_loss
    return self.compiled_loss(
File "/opt/conda/lib/python3.10/site-packages/keras/engine/", line 265, in __call__
    loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/opt/conda/lib/python3.10/site-packages/keras/", line 142, in __call__
    losses = call_fn(y_true, y_pred)
File "/opt/conda/lib/python3.10/site-packages/keras/", line 268, in call  **
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/opt/conda/lib/python3.10/site-packages/keras/", line 1984, in categorical_crossentropy
    return backend.categorical_crossentropy(
File "/opt/conda/lib/python3.10/site-packages/keras/", line 5559, in categorical_crossentropy

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

could you guys help me with a potential fix?


Hi @Tony,I suspect that the error is due to your labels and no, of, neurons in the last layers does not match.

For example, If i want to train a model on the data that has 10 classes and if i have defined only 9 neurons in the last layer while training i will get ValueError: Shapes (32, 10) and (32, 9) are incompatible.

Could you please try by defining the last layers neurons equal to no, of classes. Thank You.