W4_A1_Ex-10_KeyError for W1


I have implemented the code correctly( i think so), but I keep getting the following error. How can W1 be out of bounds?

following is the stack trace, if anyone can shine some light on the issue:

KeyError Traceback (most recent call last)
1 t_parameters, grads = update_parameters_test_case()
----> 2 t_parameters = update_parameters(t_parameters, grads, 0.1)
4 print ("W1 = "+ str(t_parameters[“W1”]))
5 print ("b1 = "+ str(t_parameters[“b1”]))

in update_parameters(params, grads, learning_rate)
—> 26 parameters[“W” + str(l+1)] = params[“W” + str(l+1)] - (learning_rate * grads[“W” + str(l+1)])
27 parameters[“b” + str(l+1)] = params[“b” + str(l+1)] - (learning_rate * grads[“b” + str(l+1)])

KeyError: ‘W1’

@Deepti_Jain, you are not using the correct parameters while calling the function. Check for both W and b. Here, you are updating the parameters of the model, using gradient descent: with the following formula:

𝑊[𝑙]=𝑊[𝑙]−𝛼 𝑑𝑊[𝑙]
𝑏[𝑙]=𝑏[𝑙]−𝛼 𝑑𝑏[𝑙]

Assess your codes and rectify it. Hopefully, it shall pass. Thanks!

Right! You are using several different variable names for what should be the same thing. params is not the same thing as parameters.

I get the difference between params and parameters and the dictionary- grads containing dW, db. But I still dont see what I am doing wrong.

Oh, sorry, the real problem is that the key values for the grads dictionary are different, right?

If you want to see the keys in a dictionary, you can do this:



Thanks Paul!

I fixed it. Seems like such a silly mistake I was making. :confused:

1 Like

Thank you @paulinpaloalto . It works for me too.

Hi, Waqar.

It’s great that you were able to find the solution under your own power by taking advantage of the previous questions on the forum. These forums have been in use for several years now, so there is a pretty good chance that you can find something helpful with the right search terms.

Onward! :nerd_face:


1 Like

Hi @paulinpaloalto ,
I completely agree with you.
This forum is mature with the exercises in the course.
Whenever, some bug appears with the right search keywords I am able to get the required answer. I really liked it.