# W2\_A2\_Ex-6\_Assertion Error\_Wrong values for costs

**URL:** <https://community.deeplearning.ai/t/w2-a2-ex-6-assertion-error-wrong-values-for-costs/322520>\
**Category:** Neural Networks and Deep Learning\
**Tags:** coursera-platform\
**Created:** [May 2, 2023, 6:29pm UTC](https://community.deeplearning.ai/t/w2-a2-ex-6-assertion-error-wrong-values-for-costs/322520 "2023-05-02T18:29:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hamster](https://avatars.discourse-cdn.com/v4/letter/h/a8b319/32.png) [@Hamster](https://community.deeplearning.ai/u/Hamster)\
**Post date:** [May 2, 2023, 6:29pm UTC](https://community.deeplearning.ai/t/w2-a2-ex-6-assertion-error-wrong-values-for-costs/322520/1 "2023-05-02T18:29:32Z")

</div>

In the “optimization” Exercise the test does not pass.  
It shows an error with “costs” - one element NaN.  
The thing is, I’m not doing anything with “cost” here - and the part where I had to input a cost function just worked fine. Any ideas?

* * *

AssertionError Traceback (most recent call last)  
 in   
7 print(“Costs = " + str(costs))  
8  
----\> 9 optimize\_test(optimize)  
[…]  
—\> 75 assert np.allclose(costs, expected\_cost), f"Wrong values for costs. {costs} != {expected\_cost}”  
[…]  
AssertionError: Wrong values for costs. [array(5.80154532), array(nan)] != [5.80154532, 0.31057104]

My cost function in exercise 5:  
cost = -1 / m \* np.sum(Y \* np.log(A) + (1 - Y) \* np.log(1 - A))

no issues there … test passed

Any ideas? Thanks!

Hamster

---

<div class="post-metadata">

**Author:** ![Kic](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/kic/32/76339_2.png) [@Kic](https://community.deeplearning.ai/u/Kic)\
**Post date:** [May 2, 2023, 7:17pm UTC](https://community.deeplearning.ai/t/w2-a2-ex-6-assertion-error-wrong-values-for-costs/322520/2 "2023-05-02T19:17:19Z")

</div>

Hi @Hamster ,

Inside the Optimize() function, there is a call to propagate() which returns **cost** as one of the return parameters, and within the propagate(), the cost is calculated. Looking at the AssertionError, the calculation for cost is fine. It is likely there is a problem when appending the **cost returned** from propagate() to the **array costs**  
Use a print statement to print out what is stored in the array **costs** , that should help to locate where the problem might be.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [May 2, 2023, 8:29pm UTC](https://community.deeplearning.ai/t/w2-a2-ex-6-assertion-error-wrong-values-for-costs/322520/3 "2023-05-02T20:29:01Z")

</div>

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

Notice that the first value in the `costs` array (the one for iteration 0) is correct, but the second one for iteration 100 is wrong (the cost is `NaN` in that case). So one thing to look carefully at would be your logic for updating the parameters.

---

<div class="post-metadata">

**Author:** ![Hamster](https://avatars.discourse-cdn.com/v4/letter/h/a8b319/32.png) [@Hamster](https://community.deeplearning.ai/u/Hamster)\
**Post date:** [May 3, 2023, 7:36pm UTC](https://community.deeplearning.ai/t/w2-a2-ex-6-assertion-error-wrong-values-for-costs/322520/4 "2023-05-03T19:36:51Z")

</div>

thx, Kic & paulinpaloalto - you pointed me in the right direction.

It made a mistake in my code with the w/b updating, in a way that I added the gradient (times learning factor) instead of substracting it. That does not matter at the start (-\> therefore the first cost value for iteration 0 was correct) but then things can spin out of control → cost result after 100 interations NaN.  
Cheers, Hamster
