Course 1 Week 3 program mini assignment

On the cost function, I am getting incorrect output on the first test and the other tests are not passing.

I tried using np.multiply as indicated and then added the second term in the cost equation. I then used np.sum and multiplied the result by -1/m

I will review the lectures again. Is there a specific video I can look at?

Thank you,

Neil A.

Hi @NeilA, maybe you could post the error message you are getting.

Alberto,

I have all the functions working prior to the cost function.
I am using the equation provided. Test 1 passes with incorrect output.
I am using np.multiply to get the logprobs and then multiply that by -1/m * np.sum(logprobs)
The other tests fail with the following error message:

cost = -2.6929281743533924
Error: Wrong output
 1  Tests passed
 1  Tests failed

Costs are always positive by definition, so something is clearly wrong. Maybe your logprobs value has already been multiplied by -1? Also note that the logprobs computation they show you in the notebook is not the whole story: it’s just part of one term of the total cost formula, right? Compare your code to the math formula for the cost.

Paul,

Thank you for the response.

Yes - I added the second term in the cost formula, the: + ((1-Y) * log(1-A2)

I also tried using np.dot.

One question on A2 which was a the sigmoid of Z2. The prior function which calculated A2 passed. I am assuming this means the values are correct?

I will try your suggestion of comparing the math formula

I will try to load the original program and add the code back in.

Thx you,

Neil

Please note that the compute_cost function here is completely independent of forward propagation: it just takes A2 as an input. So the correctness of your cost calculation is independent of the correctness of the rest of your code. The test case just supplies a value of A2, which they generated by some other means.

Also note that some of the functions here have multiple test cases and the first test case frequently just checks the data type of the output, not the value. So the fact that the first test passed just says that at least your output value is a numpy floating point scalar value or whatever the test case is checking for. You can examine the test cases by clicking “File → Open” and then reading the file public_tests.py.

Hi Paul,

Thank you for clarifying this point
I see that now. I will check out the test cases.

Neil

Hi Paul,

Thank you for your help!

The issue was with the parentheses in second term. I was using 1-log(A2) instead of log(1-A2).

All working now

Neil

1 Like

It’s great news that you found the solution! Thanks for letting us know.