Week 3 assignment - Tensorflow Cost Function Computation Error

[Image removed]

The first picture shows my code to compute the cost function, and the second one shows the error that is displayed. The value of my cost function is nearer to the answer, but it’s not accurate. I couldn’t find the bug in the code, can someone lend me a hand.

Hi @Huvin99,

The function tf.reduce_mean already computes the mean of the tensor elements, so you don’t need to do it explicitly on your code. Also be aware of the from_logits argument needed in tf.keras.losses.categorical_crossentropy.

I’m going to remove the first image on your post to prevent the breaking of the Honor Code.


Thanks, I already figured it out, sorry about the post.

Hi All,
I am also facing the same issue, can someone help here ?

[Image with code removed]

Hi @Ankit_Kumar,

First you should put your code between the # YOUR CODE STARTS HERE and # YOUR CODE ENDS HERE comments.

Also, I recommend you to read carefully the instructions of the exercise regarding the shape of the tf.keras.losses.categorical_crossentropy arguments and the use of the tf.reduce_mean function that helps you to compute the mean over all the examples.

Hope that helps.

P.D. I removed the image on your post since it is not allowed to show your code.

1 Like

Hello I am also stuck there and getting "AssertionError: Test does not match. Did you get the mean of your cost functions? " error.

Actually Itried

  • tf.reduce_mean(tf.keras.losses.categorical_crossentropy)

  • tf.reduce_mean(tf.keras.metrics.categorical_crossentropy) because the link given in the instruction shows that function.
    But neither of them solves my problem.

Could you please help me with that ?

Thanks a lot

Hi, @feritte.

Here are a few more hints.

Let me know if you need more help.

Good luck :slight_smile:

1 Like

Looks like this function compute_cost is prompts to errors. I have already reshaped the parameters logits, labels to (number of examples, num_classes) and passed in the correct order to tf.keras.metrics.categorical_crossentropy() and wrapping the previous function with tf.math.reduce_mean(cost) but I still have error. My output is tf.Tensor(0.8071431, shape=(), dtype=float32) and the expected value is tf.Tensor(0.810287, shape=(), dtype=float32)

None of the following versions works:

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

// Output
tf.Tensor(0.4051435, shape=(), dtype=float32)

cost = tf.math.reduce_mean(tf.keras.metrics.categorical_crossentropy(y_true = tf.transpose(labels), y_pred = tf.transpose(logits)))

// Output
tf.Tensor(0.8071431, shape=(), dtype=float32)

// **Expected ouput**

tf.Tensor(0.810287, shape=(), dtype=float32)


It’s unclear - why it’s requred, I solved by adding logits=True

1 Like

You should use the following:

  1. tf.reduce_sum instead of tf.math.reduce_mean

  2. tf.keras.losses.categorical_crossentropy instead of tf.keras.metrics.categorical_crossentropy

i tried this didn’t work too