# \[Week 2 A2 Exercise 6\] Programming Assignment

**URL:** <https://community.deeplearning.ai/t/week-2-a2-exercise-6-programming-assignment/2363>\
**Category:** Neural Networks and Deep Learning\
**Tags:** coursera-platform\
**Created:** [April 29, 2021, 3:48pm UTC](https://community.deeplearning.ai/t/week-2-a2-exercise-6-programming-assignment/2363 "2021-04-29T15:48:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jalfvale](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@jalfvale](https://community.deeplearning.ai/u/jalfvale)\
**Post date:** [April 29, 2021, 3:48pm UTC](https://community.deeplearning.ai/t/week-2-a2-exercise-6-programming-assignment/2363/1 "2021-04-29T15:48:58Z")

</div>

Hi,

## I’m getting the following error that seems not to be in the code written by me:

UnboundLocalError Traceback (most recent call last)  
 in   
----\> 1 params, grads, costs = optimize(w, b, X, Y, num\_iterations=100, learning\_rate=0.009, print\_cost=False)  
2  
3 print ("w = " + str(params[“w”]))  
4 print ("b = " + str(params[“b”]))  
5 print ("dw = " + str(grads[“dw”]))

in optimize(w, b, X, Y, num\_iterations, learning\_rate, print\_cost)  
40  
41 # Retrieve derivatives from grads  
—\> 42 dw = grads[“dw”]  
43 db = grads[“db”]  
44

UnboundLocalError: local variable ‘grads’ referenced before assignment

```
Could you tell me if the root cause comes from my code or how to fix it, please?

```

---

<div class="post-metadata">

**Author:** ![ace.b](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@ace.b](https://community.deeplearning.ai/u/ace.b)\
**Post date:** [April 29, 2021, 3:56pm UTC](https://community.deeplearning.ai/t/week-2-a2-exercise-6-programming-assignment/2363/2 "2021-04-29T15:56:47Z")

</div>

> [@jalfvale](#):
>
> optimize

Have you declared grads before calling it ?

---

<div class="post-metadata">

**Author:** ![jalfvale](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@jalfvale](https://community.deeplearning.ai/u/jalfvale)\
**Post date:** [April 29, 2021, 4:09pm UTC](https://community.deeplearning.ai/t/week-2-a2-exercise-6-programming-assignment/2363/3 "2021-04-29T16:09:17Z")

</div>

Thank you so much for your quick answer, ace.b. The point is that grads is used not by my code but by the “external” one:

_{moderator edit - solution code removed}_

---

<div class="post-metadata">

**Author:** ![ace.b](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@ace.b](https://community.deeplearning.ai/u/ace.b)\
**Post date:** [April 29, 2021, 4:24pm UTC](https://community.deeplearning.ai/t/week-2-a2-exercise-6-programming-assignment/2363/4 "2021-04-29T16:24:24Z")

</div>

> [@jalfvale](#):
>
> ```auto
> for i in range(num_iterations):
> # (≈ 1 lines of code)
> # Cost and gradient calculation 
> # grads, cost = ...
> # YOUR CODE STARTS HERE
> 
> ```

You have to declare grads and cost as mentioned in the tips :  
1) Calculate the cost and the gradient for the current parameters. Use propagate().

---

<div class="post-metadata">

**Author:** ![jalfvale](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@jalfvale](https://community.deeplearning.ai/u/jalfvale)\
**Post date:** [April 29, 2021, 5:04pm UTC](https://community.deeplearning.ai/t/week-2-a2-exercise-6-programming-assignment/2363/5 "2021-04-29T17:04:47Z")

</div>

```
   Oops, now I see it! That's been absoultely my fault. 

   Thanks again!
```

---

<div class="post-metadata">

**Author:** ![pranav0809](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pranav0809](https://community.deeplearning.ai/u/pranav0809)\
**Post date:** [April 30, 2021, 12:14pm UTC](https://community.deeplearning.ai/t/week-2-a2-exercise-6-programming-assignment/2363/6 "2021-04-30T12:14:24Z")

</div>

my code has passed all the tests but fails for the last part

model\_test(model)` It shows the following error AssertionError: Wrong values for d['w']. [[0.28154433] [-0.11519574] [0.13142694] [0.20526551]] != [[0.00194946] [-0.0005046] [0.00083111] [0.00143207]]`

how do i fix it

---

<div class="post-metadata">

**Author:** ![jalfvale](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@jalfvale](https://community.deeplearning.ai/u/jalfvale)\
**Post date:** [May 1, 2021, 10:52pm UTC](https://community.deeplearning.ai/t/week-2-a2-exercise-6-programming-assignment/2363/7 "2021-05-01T22:52:21Z")

</div>

Try removing the parameters’ values for num\_iterations=100, learning\_rate=0.009, print\_cost=False when calling optimize, so they get values from the ones used in model:

def model(X\_train, Y\_train, X\_test, Y\_test, num\_iterations=2000, learning\_rate=0.5, print\_cost=False):

---

<div class="post-metadata">

**Author:** ![pranav0809](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pranav0809](https://community.deeplearning.ai/u/pranav0809)\
**Post date:** [May 2, 2021, 4:56pm UTC](https://community.deeplearning.ai/t/week-2-a2-exercise-6-programming-assignment/2363/8 "2021-05-02T16:56:20Z")

</div>

thank you , I got it
