# Week 2 programming exercise - cost calculation

I need help finding what I am doing wrong in calculating cost for the propagate function in week 2 programming exercise. I have confirmed that the values for my A matrix are correct (from other threads on this topic). I am using np.dot and have also validated the calculation in Excel and gettin the same values (i.e. -2.14 for the first test with m=3 and -0.966 for the second test case with m=4).

Can’t figure out my error and need help. see the output from my notebook below:

## m=3 Y=[[1 1 0]] A=[[0.99979657 0.62245933 0.00273196]] dw = [[ 0.25071532] [-0.06604096]] db = -0.1250040450043965 cost = -2.1428184225087668 m=4 Y=[[1 1 0 0]] A=[[0.99849882 0.99979657 0.15446527 0.99966465]]

AssertionError Traceback (most recent call last)
in
14 print ("cost = " + str(cost))
15
—> 16 propagate_test(propagate)

~/work/release/W2A2/public_tests.py in propagate_test(target)
—> 53 assert np.allclose(cost, expected_cost), f"Wrong values for cost. {cost} != {expected_cost}"
54 print(‘\033[92mAll tests passed!’)
55

AssertionError: Wrong values for cost. -0.9666039194818195 != 2.0424567983978403

Notice that your cost values are negative. How can that happen? The cost is positive by definition. Look carefully at the mathematical formula for the cost and then compare that to your code. Note that you have two 1 x m vectors Y and log(A) and you need to compute the sum of the products of those elements. There are two ways to do that:

1. Use “elementwise” multiply (* or np.multiply) followed by np.sum to add up the products.
2. Use dot product (np.dot) to do both the multiply and the add in one operation. But you need to transpose the second operand in order for the dot product to be possible.

Then do that again for (1 - Y) and log(1 - A).

Then you add those two terms, both of which are negative because the log of a number between 0 and 1 is negative.

Then you multiply that sum by -\displaystyle \frac {1}{m} to get the final answer. Note the minus sign there which is what results in the positive value.

Thank you! Was making a silly mistake in the cost formula which I have fixed and it now matches the expected output.