DLS course 2 week 2 excersise 6 issue with code


KeyError Traceback (most recent call last)
in
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”)
4
5 # Predict

in model(X, Y, layers_dims, optimizer, learning_rate, mini_batch_size, beta, beta1, beta2, epsilon, num_epochs, print_cost)
37 v = initialize_velocity(parameters)
38 elif optimizer == “adam”:
—> 39 v, s = initialize_adam(parameters)
40
41 # Optimization loop

in initialize_adam(parameters)
34 # s[“db” + str(l)] = …
35 # YOUR CODE STARTS HERE
—> 36 v[“dW” + str(l)] = np.zeros(grads[“dW” + str(l)].shape)
37 v[“db” + str(l)] = np.zeros(grads[“db” + str(l)].shape)
38 s[“dW” + str(l)] = np.zeros(grads[“dW” + str(l)].shape)

KeyError: ‘dW3’

got this error in the exercise 6 adam section even though you don’t need to write any of your own code. every cell before that ra perfectly and passed all tests, don’t know what to do!

But you did write the code for initialize_adam, which you are calling there, right? It looks like that code is incorrect: it is a mistake to reference the variable grads within initialize_adam, since that is not a parameter of that function, right? Maybe that works in that one test case for that function because there happens to be a global variable grads with the right elements, but it is a mistake. That’s a bug in the test case if it doesn’t catch that error.

Yes, it turns out that the tests do not catch this error for initialize_adam: you can reference the global dictionary grads and it still passes the test. The same bug exists for initialize_velocity as well. Eeeeek!

I will file this bug immediately against the tests in the notebook, but in the meantime you just need to avoid stepping on this landmine.