Something wrong with the week 3 assignment compute_cost

In the week 3 assignment compute_cost(A2, Y), as I understand, what I need to do is compute the cost, I tried following code:

   # mentor edit: code removed

and

   # mentor edit: code removed

both got error

AssertionError: Wrong value. Expected: 0.5447066599017815 got: 1.0493608309109266

But I don’t understand why.

1 Like

@zonehuang for cost I would look more carefully at how the cross-entropy (log loss) function is defined, as you are missing some parts. See:

Also, I am not crazy about it but the way Prof. Ng has us doing this is calculating ‘cost’ in two steps (thus logprobs). Really it can be done all on one line.

2 Likes

Hi @zonehuang

In the first snippet, the computation only considers the case where the true label is 1, neglecting the case where it’s 0. Additionally, the logarithms of predicted probabilities are not properly negated.

In the second snippet, the dot product between the true labels and the transposed logarithms of predicted probabilities is incorrect for this context. To fix these issues, ensure both cases (true label being 1 and 0) are accounted for in the computation, and properly negate the logarithms of predicted probabilities.

Please check the formula again and reconsider the way you are trying to implement this function!

3 Likes

Thank you for the reply.

I’ve figured it out, the instruction is kinda confusing, I thought I only need to calc the - \sum\limits_{i=1}^{m} y^{(i)}\log(a^{[2](i)}):

I complete the whole cost function and it works now

3 Likes

@zonehuang, in the future, please do not post your code on the forum. That’s not allowed by the Code of Conduct.

It is usually sufficient to post a screen caputure image that shows any asserts or error messages you see.