Cource 2 week 2 exercise 6.2


I initialized the Adam parameters and gradients correctly according to the test in exercise 5. However, when trying to run the model using Adam, I get a KeyError for ‘dW3’. I am not quite sure what is causing this issue. Both the parameters and gradients are initialised as 0 (np.zeros()).

KeyError                                  Traceback (most recent call last)
<ipython-input-19-2ae185cb4f42> in <module>
      1 # train 3-layer model
      2 layers_dims = [train_X.shape[0], 5, 2, 1]
----> 3 parameters = model(train_X, train_Y, layers_dims, optimizer = "adam")
      5 # Predict

<ipython-input-16-620ef2b96663> in model(X, Y, layers_dims, optimizer, learning_rate, mini_batch_size, beta, beta1, beta2, epsilon, num_epochs, print_cost)
     36         v = initialize_velocity(parameters)
     37     elif optimizer == "adam":
---> 38         v, s = initialize_adam(parameters)
     40     # Optimization loop

<ipython-input-11-63378fbfde7f> in initialize_adam(parameters)
     36         v["dW" + str(l)] = <removed to avoid sharing answers>
     37         v["db" + str(l)] = <removed to avoid sharing answers>
---> 38         s["dW" + str(l)] = <removed to avoid sharing answers> 
     39         s["db" + str(l)] = <removed to avoid sharing answers>
     40     # YOUR CODE ENDS HERE

KeyError: 'dW3'

For context, all previous tests passed with other models initialised similarly.

Any help is much appreciated!


Hi @Aarjav,

Can you share your lab ID with me ? In the assignment, when you click the top right “Help” button, a panel will open and your lab ID will be shown at the bottom.

I shall take a look.


Hi @Mubsi

Sorry for the late reply! I didn’t see the notification in my email!

My lab ID is: igzkgosv

Thank you very much for your help!

After speaking with @Mubsi privately, we found that I had made a silly mistake in initialising the parameters for adam. I had used the incorrect shape even though the information was already provided.

1 Like