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/func_graph.py 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/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", 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/training.py", line 919, in compute_loss
        y, y_pred, sample_weight, regularization_losses=self.losses)
    File "/usr/local/lib/python3.7/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.7/dist-packages/keras/losses.py", line 141, in __call__
        losses = call_fn(y_true, y_pred)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1790, in categorical_crossentropy
        y_true, y_pred, from_logits=from_logits, axis=axis)
    File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5083, in categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)

    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.

Scott

4 Likes

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.

3 Likes

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