Week 4 Exercise 9 error

I’m getting the following error when trying to implement the L_model_backward function. Everything runs well up to this point

2
0

KeyError Traceback (most recent call last)
in
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’]))
6 print("dW1 = " + str(grads[‘dW1’]))

KeyError: ‘dA0’

The test code obviously is written to expect that your function returns a grads dictionary that contains that key. So you need to figure out why your grads variable does not contain that key. The test cells are not modifiable, but you can do “Insert → Cell Below” and print the keys from grads like this:

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

Here’s what I see when I run that:

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

What do you see?

in my case only last layer grads value showing ;-

grads.keys() = dict_keys([‘dA2’, ‘dW2’, ‘db2’])

So how can that happen? Note that any given layer, you are computing dW and db for the current layer, but dA for the previous layer, right? So that should be a clue that there are probably two things wrong with your logic:

  1. Your logic must be wrong such that you are only doing one layer, not all layers.
  2. Your logic at the given layer is also wrong because you are not labelling the dA output correctly.
1 Like

Thank you for your support,
The second logic mistake I have solved :

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

but the first logic mistake still there .

I am getting this error , can you please help me with this.


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

in L_model_backward(AL, Y, caches)
45 current_cache = caches[L-1]
46 # dA_prev_temp, dW_temp, db_temp = linear_activation_forward(grads[“dA” + str(l+1)], current_cache, “sigmoid”)
—> 47 grads[“dA” + str(L-1)] = linear_activation_forward(dAL, current_cache, activation=“sigmoid”)
48 grads[“dW” + str(L)] = linear_activation_forward(dAL, current_cache, activation=“sigmoid”)
49 grads[“db” + str(L)] = linear_activation_forward(dAL, current_cache, activation=“sigmoid”)

TypeError: linear_activation_forward() missing 1 required positional argument: ‘b’

I am not getting what’s wrong with this code.

{moderator edit - solution code removed}

When you are doing back propagation, the input value of dA is from the next later layer, not the previous layer, right? It looks like you must have started by just “copy pasting” the forward propagation logic and then patching it up. The whole point is that you’re going backwards. You do the output layer outside the loop and then the loop walks backwards through the hidden layers.

1 Like

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)
40 # YOUR CODE STARTS HERE
41 current_cache = caches[0]
—> 42 dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads[“dA” + str(L)], current_cache, “sigmoid”)
43 grads[“dA” + str(L-1)] = dA_prev_temp
44 grads[“dW” + str(L)] = dW_temp

Found discussions on this topic but unable to figure out the solution
Unable to figure out what exactly should i do

There is logic before the line that throws which “manually” computes dAL. Did you add that value to the grads dictionary with the appropriate key? You can print the keys of a python dictionary by saying:

print(myDictionary.keys())

Note that the key value is not literally “dAL”: it is “dA2” or whatever the appropriate layer number is in this test case, right?

1 Like

Hi,
he receaved this error:

—> 61 dA_prev_temp, dW_temp, db_temp = …
62 grads[“dA” + str(l)] = dA_prev_temp
63 grads[“dW” + str(l + 1)] = dW_temp

KeyError: ‘dA2’

dict keys:
print(f"grads.keys() = {grads.keys()}")
grads.keys() = dict_keys([‘dA2’, ‘dW2’, ‘db2’, ‘dA0’, ‘dW1’, ‘db1’])

dA2 have to be dA1 but I don’t know what I’m doing wrong, please, can you help me?
Thanks

Notice that the test cases here are 2 layer nets. The way L_model_backward works is that you do the output layer first and that happens outside the loop over the hidden layers, right? So that is separate logic from the logic in the main loop and it’s the outer layer logic that should be producing dA1, right? So your logic in the hidden layer loop is correct (it produces dA0 instead of dA1), but your logic outside the loop is not correct.

For reference, here’s what I get when I print grads.keys():

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

Of course another interesting thing to check is whether the dA2 value you are inserting in the dictionary is just mislabeled and is really dA1 or whether it’s actually the value of dA2. Try printing the shape of it to see. You do actually compute dA2 as the very first step of back propagation, but you’re not supposed to put that value in the dictionary.

1 Like

Hi, I solved it, thanks!!

hello Paul,

I am having the same key error, and I print grads.key()
so in my case instead of dA0, it shows dA1 and dA2.

how do I correct the grads so that it calculates dA0

What are the shapes of the gradients you get? Maybe you just misnamed them. Notice what they tell you in the instructions and the comments in that section. At each layer l you get the dW and db values for layer l, but the dA value is for the previous layer l - 1, right?

yes I did notice dA as l-1, I had a doubt do I need to mentioned dA as dA + str(L-1) for the sigmoid activation?

will that do the correction??

I am sorry I am totally learner, I hope I am not annoying with silly doubts

KeyError Traceback (most recent call last)
in
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’]))
6 print("dW1 = " + str(grads[‘dW1’]))

KeyError: ‘dA0’

THIS IS WHAT I AM GETTING

Yes, for the output layer, you also have the output dA being for the previous layer. But the other thing to worry about is what happens in the loop over the hidden layers. There you have the same logic: you take the dA for the given layer as input and you output dW and db for the given layer, but dA for the previous layer.

Maybe your loop didn’t execute enough times. Did you get dW1 and db1 in your grads dictionary?

dW1 and db1 was there in grads dictionary. only issue is happening dA0.

I even tried the dA + str (L-1) and it still shows key error screen which I just shared.

But note that the layer number inside the loop is not L, right? It’s l (lower case ell).