Code does not correctly calculate cost despite the code seeming right

I’m stuck on Exercise 6 of the TensorFlow Introduction assignment, where I need to calculate the total cost given the model’s predictions and the true labels.

I am using the pescribed functions, tf.reduce_sum() and tf.keras.losses.categorical_crossentropy(from_logits=True), but it still gives the wrong answer.

I even asked ChatGPT (which is known for it’s ability to solve simple coding problems), and they said my code was correct, so this is beyond me.

The error message is this:
tf.Tensor(0.17102128, shape=(), dtype=float32)
AssertionError: Test does not match. Did you get the reduce sum of your loss functions?

1 Like

I think that means you omitted the transposes. Please see this thread. This thread also has the full check list.

I recommend you not rely on ChatGPT for debugging your code.

That worked! The exercise criteria don’t really get across that you need to transpose the inputs well, so I’m not surprised I missed it. That also explains why ChatGPT couldn’t solve it.

Thanks yet again! You’re a real lifesaver.

Glad to hear that you solved it. They did mention the expected dimensions of the labels and logits in the instructions, although they did not actually use the word “transpose” anywhere. I explained the background on the other thread that I linked above.