Course 2, Week 3, compute_total_loss(logits, labels):

i run this code for compute_total_loss

total_loss = tf.reduce_mean(tf.keras.losses.categorical_crossentropy(tf.transpose(labels), tf.transpose(logits), from_logits=True))

But its giving me shape error, below is the last statement of the error

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

i dont know what to do now

It looks like you must have modified the shape of either the labels or the logits to be a 3D Tensor. Try adding these statements before the statement that “throws”

    print(f"logits.shape {logits.shape}")
    print(f"labels.shape {labels.shape}")

Note that I wrote the code to do the transposes first and then put the print statements after the transposes and before the loss call and here’s what I see when I run the test cell for compute_total_loss:

logits.shape (2, 6)
labels.shape (2, 6)
tf.Tensor(0.810287, shape=(), dtype=float32)
All test passed

A couple of other things to note:

  1. You’re not following the instructions correctly for the last step: they tell you to compute the sum, not the mean. Here’s a thread explaining why it is done that way.
  2. Note that the labels input that they use in the test case is new_y_train which was created earlier in the notebook by using your one_hot_matrix function. Maybe there is a problem with that function.

Thank you very much.
The problem was in the one_hot_matrix, found it now.