# [Week 2 A2 Exercise 6] Programming Assignment

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
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?

Have you declared grads before calling it ?

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}

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().

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

Thanks again!

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

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):

1 Like

thank you , I got it