Course1 wk4 Exercise 9 - L_model_backward error

I got the below error message when implementing L model backward step. I follow the hint. Not sure what “keyError dA1” mean.


KeyError Traceback (most recent call last)
in
1 t_AL, t_Y_assess, t_caches = L_model_backward_test_case()
----> 2 grads = L_model_backward(t_AL, t_Y_assess, t_caches)
3
4 print("dA0 = " + str(grads[‘dA0’]))
5 print("dA1 = " + str(grads[‘dA1’]))

in L_model_backward(AL, Y, caches)
58 # YOUR CODE STARTS HERE
59 current_cache = caches[l]
—> 60 dA_prev_temp, dW_temp, db_temp =linear_activation_backward(grads[“dA”+str(l+1)], current_cache, “relu”)
61 grads[“dA” + str(l)] = dA_prev_temp
62 grads[“dW” + str(l + 1)] = dW_temp

KeyError: ‘dA1’

The grads variable there is a dictionary, which is used to store all the gradients that you are computing. So it looks like something must be wrong with your logic that is causing it to reference the gradient of dA1 from the dictionary before it has been added. So now you need to figure out why that is happening. I would suggest putting print statements in your code to track which layer you are processing and show which gradients you are computing at each point.

Looking at your output, it suggests that you are on the first iteration of the loop over the hidden layers here. So what that suggests is that perhaps your logic for the output layer did not correctly store the gradients it computed in the grads dictionary. Think carefully about the layer number at the output layer and what the gradients are that you are computing there. If that’s not enough of a clue to find the issue, you can add this print statement at the top of the hidden layer loop:

print(f"grads.keys() = {grads.keys()}")

That will show you the keys of the current contents of the dictionary. Here’s what I see when I include that print statement at that point in the logic:

grads.keys() = dict_keys(['dA1', 'dW2', 'db2'])

In this test case there are only 2 layers: the output layer (layer 2) and one hidden layer (layer 1).