Course 2, Week 3 compute_total_loss function

I have transposed both labels and logits so both have the shape (6,2) and passed them to cross entropy function and then to reduce_sum function.
But it returns the value: tf.Tensor(1.6142862, shape=(), dtype=float32)

Can you give me some insight on what my mistake is?

Hi @ricky_pii

Have you set the 3rd parameter, from_logits=True, when calling the tf.keras.losses.categorical_crossentropy()

I’m dealing with that problem, too. I’ve transposed logits and labels (tf.transpose(logits), tf.transpose(labels). And also used tf.keras.losses.categorical_crossentropy and tf.reduce_sum, from_logits=True.
I’ve read all the threads about this exercise and still don’t know what the problem is. Everytime I get this error: ValueError: Shapes (2, 4) and (2, 6) are incompatible.

One important point to note here is that order of logits and labels is also important when passing them to the categorical_crossentropy. Labels comes first, then logits.

But both labels and logits have the same shape (already given to you). Are you making some changes to it?

1 Like

Thanks for your guidance. I replaced them (tf.transpose(labels), tf.transpose(logits), from_logits=True), and still get the same error.
No, I didn’t make any changes. I also got a fresh copy of the assignment (according to instructions) to make sure the code and assignments have not changed.

Strange :confused: :confused: :thinking: :thinking:

Please send me your complete code of compute_total_loss function in a private message. Click my name and message.

So, to update others, Niloofar_Hamidi didn’t pass the previous test which lead to an error in this test too.
Remember that you have to pass all the previous tests before proceeding to the next one.

We need to transpose the logits as well as the labels, but I think so i should be mentioned somewhere in he guide too.

Thank you for your reply!

Yes I did.

Yes, I passed them in a correct order.

Yes, I did transpose both

1 Like

Hello @ricky_pii! Thank you for sending me your code.

I suggest reading Kin’s message carefully. It says, have “you set the 3rd parameter, from_logits= True”? True, I repeat, True. Its True, not False.

It’s worked.
Thank you!