C1 week2 exercise 7 error message

Hello,
I got this error message (see below) when testing optimize function. I don’t understand why the costs value is an array because in the last step the cost is a scalar.

Also, I’m confused about what is the initial value of w and b for this step as there’s only deepcopy function (to be honest I’m not familiar with this function). But w and b are local variables and I don’t see how their values are initialized.


AssertionError Traceback (most recent call last)
in
7 print("Costs = " + str(costs))
8
----> 9 optimize_test(optimize)

~/work/release/W2A2/public_tests.py in optimize_test(target)
73 assert type(costs) == list, “Wrong type for costs. It must be a list”
74 assert len(costs) == 2, f"Wrong length for costs. {len(costs)} != 2"
—> 75 assert np.allclose(costs, expected_cost), f"Wrong values for costs. {costs} != {expected_cost}"
76
77 assert type(grads[‘dw’]) == np.ndarray, f"Wrong type for grads[‘dw’]. {type(grads[‘dw’])} != np.ndarray"

AssertionError: Wrong values for costs. [array(5.80154532), array(nan)] != [5.80154532, 0.31057104]

Your understanding of cost should be a scalar is correct. costs refers to the cost recorded that match this criteria on iteration:

# Record the costs
if i % 100 == 0:
    costs.append(cost)

The test code inside public_tests.py checks for 101 iterations. This is why you have 2 values in costs.
For details on how your method gets tested, look inside the test file.

And notice that your first value of the costs array matches the expected value, but the second one does not: it is NaN (Not a Number). That’s what happens when you get overflow or divide by zero or log(0). The first cost value is after 0 iterations, meaning before the gradients and updating of the parameters takes effect. So that says the problem is not your cost logic, but your “update parameters” logic. But if your propagate function passed the tests, then the gradients are correct. So it must be how you apply the gradients in the optimize logic that is wrong. Please have a careful look at your code and compare to the formulas shown in the instructions.

Thank you for reply. Your analysis is spot on. I had an error when updating parameter value.

It’s great to hear that you were able to find the solution based on that hint. Nice work! Onward! :nerd_face: