Week3, assignment compute_cost

Dear Mentors,

I am getting following error during cost calculation. Categorical_crossentropy takes y-true = labels, y_pred = logits, from_logits=True. May I know what other arguments or changes do I need to do to avoid following errors.

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

Thanks for your help.

Best regards,
Sunita

If you look at the guidance, it says,

It’s important to note that the " y_pred " and " y_true " inputs of tf.keras.losses.categorical_crossentropy are expected to be of shape (number of examples, num_classes)

On the other hand, if you look at the description of compute_cost(), it says,

logits – output of forward propagation (output of the last LINEAR unit), of shape (6, num_examples)

So, you may want to “transpose” it. :slight_smile:

1 Like

Thank you so much. It worked after taking transpose. But, I have a question. When I reshaped y_pred and y_true into column vectors, it threw same error message. Why is so?

This is actually a good question. The order of data is different.
Here is an example. Let’s define (2,3) numpy array, X.

X = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}

Then, if we “transpose” X,

X.T = \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix}

But, if reshape X to be the shape of (3,2),

X.reshape((3,2)) = \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{bmatrix}

So, the value of output tensor is different. I suppose that is the reason why you got an assertion error again.

1 Like

It means that if we flatten without loosing correspondence then it will work. I will check this out. Thanks again.

Yes, it is definitely the case that reshape and transpose are not the same thing, as Nobu demonstrated. There are also right and wrong ways to use reshape if your purpose is to “flatten” samples into vectors. Here’s a big thread that demonstrates how reshape works in this respect for the same question w.r.t. flattening the images in Course 1 Week 2.