C2W4- multi-classifier - Error during training

course: “DeepLearning.AI TensorFlow Developer Professional Certificate” course.

I am facing error while training the model even after all the above code outputs are displayed as expected output.

  1. parse_data_from_input - this function working as expected
  2. train_val_generators - this function working as expected
  3. create_model - created two convolution layers as described and updated final layer as 26

1 frames
/usr/local/lib/python3.8/dist-packages/keras/engine/training.py in 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 "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1040, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1030, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 890, in train_step
    loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 948, in compute_loss
    return self.compiled_loss(
File "/usr/local/lib/python3.8/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
    loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 139, in __call__
    losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 243, in call  **
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 1787, in categorical_crossentropy
    return backend.categorical_crossentropy(
File "/usr/local/lib/python3.8/dist-packages/keras/backend.py", line 5119, in categorical_crossentropy

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

I think the final layer should be one output instead of 26!

Actually it is a multi classifier with 26 letters of english alphabet. so, should be 26 not 1.

I see that if i give 1 , this error goes away. But it should be 26 only

Im not familiar with the assignment but if its asking for 26 outputs and if you are using binary crossentropy then you have a problem. For multiclass outputs you need categorical crossentropy.

I was using categorical crossentropy only but using sparse categorical crossentropy fixed the problem.

Thanks @gent.spah for looking into this. Appreciated!


had same issue, thanks for the solve. Turns out ‘categorical_crossentropy’ is for one-hot encoding (e.g. [1,0,0] for rock, [0,1,0] for paper, as was in the lesson), but ‘sparce_categorical_crossentropy’ is for ordinal encoding (numerical value for each category e.g. (0 for a , 1 for b, 2 for c) as in assignment)


Thank you for this info. Ran into the same Value error.
I think that the lectures should cover this essential piece of concept
and explain the differences between various loss functions!