# W2\_A2\_Cost calculation passes Ex-5, but fails Ex-8

**URL:** <https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084>\
**Category:** Neural Networks and Deep Learning\
**Tags:** coursera-platform\
**Created:** [July 21, 2023, 2:44am UTC](https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084 "2023-07-21T02:44:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caleb\_C\_Ruszkowski](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/caleb_c_ruszkowski/32/58206_2.png) [@Caleb\_C\_Ruszkowski](https://community.deeplearning.ai/u/Caleb_C_Ruszkowski)\
**Post date:** [July 21, 2023, 2:44am UTC](https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084/1 "2023-07-21T02:44:33Z")

</div>

The exercise 8 test expects a value of 0.69314718, but my code returns 0.63719763, which means my code is bugged somewhere. However, this same code passes the exercise 5 test, meaning the bug is probably somewhere other than the cost calculation itself. My guess is somewhere between calling the optimize() function and reaching the propagate() function a few calls later. I’m completely stumped as to what it could be. I’m still figuring out numpy and the like.

This is in the Week 2 programming assignment for the cat classifier.

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [July 21, 2023, 2:49am UTC](https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084/2 "2023-07-21T02:49:18Z")

</div>

> [@Caleb\_C\_Ruszkowski](#):
>
> which either means my code is bugged

Yes, your code is bugged.

---

<div class="post-metadata">

**Author:** ![saifkhanengr](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/saifkhanengr/32/152858_2.png) [@saifkhanengr](https://community.deeplearning.ai/u/saifkhanengr)\
**Post date:** [July 21, 2023, 4:37am UTC](https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084/3 "2023-07-21T04:37:46Z")

</div>

Check your implementation of `optimize` in the `model` function.

---

<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:** [July 21, 2023, 4:48am UTC](https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084/4 "2023-07-21T04:48:14Z")

</div>

Exactly. The point is that your lower level routines like `propagate`, `optimize` and `predict` are probably correct, since they passed their unit tests. So that means the bug is in your `model` logic and how you use those lower level functions. You can get bad results from a good subroutine if you call it incorrectly or don’t handle the return values correctly.

What is the actual error output that you see when the test case fails?

---

<div class="post-metadata">

**Author:** ![Caleb\_C\_Ruszkowski](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/caleb_c_ruszkowski/32/58206_2.png) [@Caleb\_C\_Ruszkowski](https://community.deeplearning.ai/u/Caleb_C_Ruszkowski)\
**Post date:** [July 21, 2023, 12:18pm UTC](https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084/5 "2023-07-21T12:18:45Z")

</div>

AssertionError Traceback (most recent call last)  
 in   
1 from public\_tests import \*  
2  
----\> 3 model\_test(model)

~/work/release/W2A2/public\_tests.py in model\_test(target)  
127 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"  
128 assert len(d[‘costs’]) == 1, f"Wrong length for d[‘costs’]. {len(d[‘costs’])} != 1"  
 → 129 assert np.allclose(d[‘costs’], expected\_output[‘costs’]), f"Wrong values for d[‘costs’]. {d[‘costs’]} != {expected\_output[‘costs’]}"  
130  
131 assert type(d[‘w’]) == np.ndarray, f"Wrong type for d[‘w’]. {type(d[‘w’])} != np.ndarray"

AssertionError: Wrong values for d[‘costs’]. [array(0.63719763)] != [array(0.69314718)]

---

<div class="post-metadata">

**Author:** ![saifkhanengr](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/saifkhanengr/32/152858_2.png) [@saifkhanengr](https://community.deeplearning.ai/u/saifkhanengr)\
**Post date:** [July 21, 2023, 12:29pm UTC](https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084/6 "2023-07-21T12:29:13Z")

</div>

Maybe checking [this](https://community.deeplearning.ai/t/w2-ex-8-wrong-values-for-d-costs/267781) or [this](https://community.deeplearning.ai/t/w2-a2-ex-8-value-error-for-w/20579/103) helps. Let us know if it doesn’t.

---

<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:** [July 21, 2023, 2:26pm UTC](https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084/7 "2023-07-21T14:26:18Z")

</div>

The other thing to check is that you did not hard-code the learning rate on the call to `optimize` or not pass it (which is a different form of hard-coding). I’m guessing Saif’s suggestion is more likely, since the number of iterations looks correct (there’s an earlier test for that). The usual pattern of errors with hard-coding is that both the number of iterations and learning rate are hard-coded. But still worth a look …

---

<div class="post-metadata">

**Author:** ![Caleb\_C\_Ruszkowski](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/caleb_c_ruszkowski/32/58206_2.png) [@Caleb\_C\_Ruszkowski](https://community.deeplearning.ai/u/Caleb_C_Ruszkowski)\
**Post date:** [July 23, 2023, 6:36pm UTC](https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084/8 "2023-07-23T18:36:11Z")

</div>

Sorry for inactivity, but I double-checked the cost/costs variable and fixed the function calls, but it is still giving me the same error.

Can I maybe post pseudocode or something? I can tell you all my function calls are accurate and my code still passes every exercise except exercise 8.

Also for the record, I still passed the programming assignment with an 83, but I really want to get to the bottom of this

---

<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:** [July 23, 2023, 7:29pm UTC](https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084/9 "2023-07-23T19:29:09Z")

</div>

That means there is something wrong in your `model` logic. Your lower level functions may well be correct, but you have to call them correctly. It’s probably time to just look at your code, but we can’t do that here on the public thread. Please check your DMs for a message from me about how to proceed.

---

<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:** [July 23, 2023, 9:08pm UTC](https://community.deeplearning.ai/t/w2-a2-cost-calculation-passes-ex-5-but-fails-ex-8/392084/10 "2023-07-23T21:08:34Z")

</div>

Just to close the loop on the public thread here, the problem was that in the `model` function, the logic for initializing w and b before the call to `optimize` was “hand-coded” instead of just calling the `initialize_with_zeros` function. That would still have worked, if the code had been written in the same way, but the new “hand written” version used the Gaussian distribution to randomly initialize w. That is probably still a valid solution, but you end up with a different answer because you are starting Gradient Descent in a different place. Who knows, your answer might even be better, but the test cases here are not checking for “as good or better”: they are just looking for the values they expect.
