Error in my dense layer, `ValueError: Shapes (None, 1) and (None, 26) are incompatible

I am having issues with my model in the Course 2 week 4 programming assignment. I have been able to complete the parse_data_from_input and train_val_generators part of the assignment but this part if giving me issues and I don’t seem to know what the solution is.

I get this weird error everytime I run the create_model function.

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 1010, in step_function  **
        outputs =, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 860, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 919, in compute_loss
        y, y_pred, sample_weight, regularization_losses=self.losses)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/usr/local/lib/python3.7/dist-packages/keras/", line 141, in __call__
        losses = call_fn(y_true, y_pred)
    File "/usr/local/lib/python3.7/dist-packages/keras/", line 245, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/", line 1790, in categorical_crossentropy
        y_true, y_pred, from_logits=from_logits, axis=axis)
    File "/usr/local/lib/python3.7/dist-packages/keras/", line 5083, in categorical_crossentropy

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

[code removed - moderator]

1 Like

I was having the exact same issue and it was driving me crazy!

Change your loss to:

loss = ‘sparse_categorical_cross_entropy’

I think the categorical_cross_entropy ( which i was trying to use ) tries to put it into a one hot encoding and messes with the dims.

in any case changing this worked for me. Hope it helps.



Thanks this solve the problem.

Please ignore this message - I had an issue related to this post but resolved it myself. Many thanks

Thanks a lot :slight_smile:

Thank you.
Just it’s ‘sparse_categorical_crossentropy’
without the underscore between cross and entropy.


loss = ‘sparse_categorical_crossentropy’ worked for me.
Thank you very much, to all contributors. I discovered that copy pasting single quotes from here causes invalid syntax, because of non-standard quotation marks. I needed to provide my own quotation marks

Switched from CategoricalCrossentropy to SparseCategoricalCrossentropy. It helped. Thanks