Week 2 Exercise 5: What's the correct answer of cost and how to compute cost

I’m confused why the expected output is different from the assertion error?
I also have some problem to calculate the cost.

Hi, @Sarah3. Yes, confusing! The AssertionError is being thrown from within the propagate_test which is part of thepublic_tests.py file. (When a file is a collection of functions, it is called a “module”–just for kicks!) You can examine that and other functions using File --> Open from your notebook. If you do that, you will see the definitions for the values expected by that test (in this case 2.04245 …). The expected output reported in the notebook, is from the execution of the first few lines of that cell, which runs your function on the test data defined there.

Now, what do you think is up with your cost function?

BTW, you can create another cell in your notebook comprising the first five lines of the test cell. The first four lines define the data to run your propagate function and the fifth executes the function. You can then insert a print(cost) statement to check your output against the Expected outputcost = 0.159005 .... To do that, copy those first five lines to your clipboard, the use “Insert → Insert Cell Above”, and paste the content in there. Now you are free to edit that cell, by placing a print(cost) statement, for example, at the bottom. This might be useful for diagnosing the problem with your cost function.

1 Like

Thank you for your swift response. I’ve figured out the difference. Another questions arises when I see the result again. After running my code above, the cost is equal to -2.14. Why does the cost turn to -0.96 in the assertion error?

My codes of cost are as follows:
cost = (-1/m)*(np.dot(Y,(np.log(A)).T)+np.dot((1-Y),(1-np.log(A)).T))
I’m confused about where’s the error.

If I’m reading the parentheses correctly, I think that looks correct. Are you sure that you clicked “Shift-Enter” on the propagate cell after you made the most recent code change? Just changing the cell and then calling the function again runs the old code.

Note that you’ve quoted negative values previously and that indicates a fundamental problem: the cost formula should only produce a positive value.

I executed my code by using “Shift+Enter” correctly and I also created a new cell to examine the value of cost. I got -2.14 same as the number showed in the screenshot.
After I remove the parentheses to -1/m, I still got the same result.

I’m sorry! I wasn’t looking carefully enough. I just assumed the negative was a result of bad parentheses, but once I’d checked those I gave up. The mistake is in the second term. Please compare the code you wrote to the math formula for the cost:

J = -\displaystyle \frac {1}{m} \sum_{i = 1}^{m} \left [ y_i * log(a_i) + (1 - y_i) * log(1 - a_i) \right ]

What you wrote is equivalent to (1 - log(a_i)) in the second term, which is not what the formula says, right?

1 Like

Yes, thank you for your help. I carefully examined my code to the formula again and fixed the problem. I really appreciate your kind response.