Week 3 - Exercise 6 - compute_cost, getting incorrect mean cost

I’m transposing both logics and labels to get the input format of [num examples, num classes], and setting the from_logics flag to True in the categorical_crossentropy function. I’m getting a mean of 20.218018, and the answer should be 0.4051435 I’m wondering if my input data is incorrect somehow. I’m not really sure what to look at next here to figure out what is going on. Any help is appreciated.

logits:
tf.Tensor(
[[ 2.4048107 -0.7921977 0.9447198 1.158121 4.768706 6.1481323 ]
[ 5.0334096 -4.1523376 -0.46802214 3.9810789 2.3220146 3.909829 ]], shape=(2, 6), dtype=float32)

labels:
tf.Tensor(
[[0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0.]], shape=(2, 6), dtype=float32)

categorical_crossentropy:
tf.Tensor([23.754297 16.68174 ], shape=(2,), dtype=float32)

reduce_mean(categorical_crossentropy):
tf.Tensor(20.218018, shape=(), dtype=float32)

1 Like

That’s the value that you get if you reverse the order of the labels and logits argument to the loss function. Please compare your code to the documentation for the loss function.

2 Likes

Thanks, that was the problem, I had them reversed

Great! There are 3 popular errors on that code block:

  1. Reversing the arguments.
  2. Forgetting the transposes.
  3. Forgetting the from_logits = True argument.

Or some combination of the above. Each mistake gives a characteristic wrong answer, but the combinations can be a little harder to diagnose.

6 Likes

The most authoritative explanation!Thank u so much!