ValueError: `logits` and `labels` must have the same shape, received ((None, 10) vs (None, 1))


I ran into this error
ValueError: logits and labels must have the same shape, received ((None, 10) vs (None, 1)).
May someone assists.

Please move your post to the right topic. Introductions is not the right topic for this post.

I am doing Introduction to TensorFlow for Artificial Intelligence, Machine Learning, and DeepLearning.AI.

Please use the loss function that corresponds to multi-class classification.

Congrats on doing the specialization.
Click the pencil botton on the top and update the course / week information to table the post under the right topic.

After you click the pencil button, move the post to to the right topic.

This is loss function, i have used.

[code removed - moderator]

This assignment is a binary classification problem. Why use 10 units in the last Dense layer?

ValueError: logits and labels must have the same shape, received ((None, 17, 17, 10) vs (None,)).
I am also getting this error.
Actually the code was running but i was not getting the callback to work as it should so while i was trying that the assignment raised this error, pls help call generated an error since the output shapes aren’t compatible with the dataset. Use print(model.summary()) before calling to see the model architecture. Keeping in mind that the label is either 0 or 1, please use tf.keras.layers.Flatten(), at the right place within the model to fix the error.

This is your stacktrace for reference:

Epoch 1/15
ValueError                                Traceback (most recent call last)
<ipython-input-8-e46272af2bc9> in <module>
----> 1 hist = train_happy_sad_model(gen)

<ipython-input-7-5c87d9883235> in train_happy_sad_model(train_generator)
     33     # Your model should achieve the desired accuracy in less than 15 epochs.
     34     # You can hardcode up to 20 epochs in the function below but the callback should trigger before 15.
---> 35     history =,
     36                         epochs=15,
     37                         callbacks=[callbacks],

/opt/conda/lib/python3.8/site-packages/keras/utils/ in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/opt/conda/lib/python3.8/site-packages/tensorflow/python/framework/ in autograph_handler(*args, **kwargs)
   1127           except Exception as e:  # pylint:disable=broad-except
   1128             if hasattr(e, "ag_error_metadata"):
-> 1129               raise e.ag_error_metadata.to_exception(e)
   1130             else:
   1131               raise

ValueError: in user code:

    File "/opt/conda/lib/python3.8/site-packages/keras/engine/", line 878, in train_function  *
        return step_function(self, iterator)
    File "/opt/conda/lib/python3.8/site-packages/keras/engine/", line 867, in step_function  **
        outputs =, args=(data,))
    File "/opt/conda/lib/python3.8/site-packages/keras/engine/", line 860, in run_step  **
        outputs = model.train_step(data)
    File "/opt/conda/lib/python3.8/site-packages/keras/engine/", line 809, in train_step
        loss = self.compiled_loss(
    File "/opt/conda/lib/python3.8/site-packages/keras/engine/", line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/opt/conda/lib/python3.8/site-packages/keras/", line 141, in __call__
        losses = call_fn(y_true, y_pred)
    File "/opt/conda/lib/python3.8/site-packages/keras/", line 245, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/opt/conda/lib/python3.8/site-packages/keras/", line 1807, in binary_crossentropy
        backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
    File "/opt/conda/lib/python3.8/site-packages/keras/", line 5158, in binary_crossentropy
        return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)

    ValueError: `logits` and `labels` must have the same shape, received ((None, 17, 17, 1) vs (None,)).