Course 2, Week 3, compute_cost

cost = tf.reduce_mean(tf.keras.losses.categorical_crossentropy(y_true = labels,y_pred = logits,from_logits=True))

I am trying this code but it keeps showing

AssertionError: Test does not match. Did you get the mean of your cost functions?

can someone spot the error and guide me.
Thanks in advance.

Did you transpose the labels and logits first? The instructions tell you what the shapes need to be, which implies a transpose.

2 Likes

I did a transpose and still got the same error. Please what am I doing wrong?

{moderator edit - solution code removed}

cost -> tf.Tensor(0.4981846, shape=(), dtype=float32)

Then I got this error:

AssertionError: Test does not match. Did you get the reduce sum of your cost functions?

What you need there are transpose operations, not reshapes. Those are not the same thing, even if the shapes end up the same. Try it on a simple example and watch what happens. Transpose is a very specific mathematical operation.

1 Like

Hi, please, Iā€™m having a similar problem. I did tf transposes on labels and logits, then crossentropy and then reduce sum, but I get tf.Tensor(1.6142862, shape=(), dtype=float32) and the same message: AssertionError: Test does not match. Did you get the reduce sum of your cost functions?

[SOLVED] I tried with different values for the two extra parameters of crossentropy and it worked with:

from_logits=True