Week4, Ass. 1,Ex. 9, L_model_backward

I got this error

dA0 = [[ 0.57243624  0.        ]
 [-0.62840214  0.        ]
 [ 0.07375161  0.        ]]
dA1 = [[ 0.12913162 -0.44014127]
 [-0.14175655  0.48317296]
 [ 0.01663708 -0.05670698]]
dW1 = [[-0.55240952  0.17511096  0.6762397 ]]
dW2 = [[-0.39202432 -0.13325855 -0.04601089]]
db1 = [[-0.2795438]]
db2 = [[0.15187861]]
Error: Wrong shape for variable dA0.
Error: Wrong shape for variable dW1.
Error: Wrong shape for variable db1.
Error: Wrong output for variable dA0.
Error: Wrong output for variable dW1.
Error: Wrong output for variable db1.
 1  Tests passed
 2  Tests failed
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-20-3ace16762626> in <module>
      9 print("db2 = " + str(grads['db2']))
     10 
---> 11 L_model_backward_test(L_model_backward)

~/work/release/W4A1/public_tests.py in L_model_backward_test(target)
    442     ]
    443 
--> 444     multiple_test(test_cases, target)
    445 
    446 def update_parameters_test(target):

~/work/release/W4A1/test_utils.py in multiple_test(test_cases, target)
    140         print('\033[92m', success," Tests passed")
    141         print('\033[91m', len(test_cases) - success, " Tests failed")
--> 142         raise AssertionError("Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.".format(target.__name__))
    143 

AssertionError: Not all tests were passed for L_model_backward. Check your equations and avoid using global variables inside the function.

Please help.
Thanks in advance.

I 've fixed my code, and not I 'm getting this error

IndexError                                Traceback (most recent call last)
<ipython-input-38-3ace16762626> in <module>
      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']))

<ipython-input-37-886b3898339e> in L_model_backward(AL, Y, caches)
     59         # YOUR CODE STARTS HERE
     60         currrent_cache = caches[l - 1]
---> 61         dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads["dA" + str(l + 1)], current_cache, "relu")
     62         grads["dA" + str(l)] = dA_prev_temp
     63         grads["dW" + str(l + 1)] = dW_temp

<ipython-input-17-f1cf83680204> in linear_activation_backward(dA, cache, activation)
     23         # YOUR CODE STARTS HERE
     24         # This helped https://community.deeplearning.ai/t/course-1-week-4-assignment-1-exercise-8/9527?u=mohassan
---> 25         dZ = relu_backward(dA, activation_cache)
     26         dA_prev, dW, db = linear_backward(dZ, linear_cache)
     27         # YOUR CODE ENDS HERE

~/work/release/W4A1/dnn_utils.py in relu_backward(dA, cache)
     54 
     55     # When z <= 0, you should set dz to 0 as well.
---> 56     dZ[Z <= 0] = 0
     57 
     58     assert (dZ.shape == Z.shape)

IndexError: boolean index did not match indexed array along dimension 0; dimension is 3 but corresponding boolean dimension is 1

I 'm now getting this error

KeyError                                  Traceback (most recent call last)
<ipython-input-26-3ace16762626> in <module>
      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']))

<ipython-input-25-99e2d895a796> in L_model_backward(AL, Y, caches)
     59         # YOUR CODE STARTS HERE
     60         currrent_cache = caches[l]
---> 61         dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads["dA" + str(l + 1)], current_cache, "relu")
     62         grads["dA" + str(l)] = dA_prev_temp
     63         grads["dW" + str(l + 1)] = dW_temp

KeyError: 'dA1'

Please help, I 'm still stuck.

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-58-3ace16762626> in <module>
      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']))

<ipython-input-57-46714656983b> in L_model_backward(AL, Y, caches)
     59         # YOUR CODE STARTS HERE
     60         current_cache = caches[l]
---> 61         dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads["dA" + str(l + 1)], current_cache, "relu")
     62         grads["dA" + str(l)] = dA_prev_temp
     63         grads["dW" + str(l + 1)] = dW_temp

KeyError: 'dA1'

Hi @MoHassan , your code looks alright, but the key error on dA1 probably means that something goes wrong in the for loop on l going out or range. Have a look first at your definition of the for loop and the values of l you are using in it. Hope this helps.

2 Likes

Hello @sjfischer, Thanks for your reply.
I 've written my own part of the code as mentioned in the instructions, and I 'm not supposed to edit the other parts.
Sorry, but I 'm still confused.

Note that this test case is pretty limited: it only has one hidden layer. So that probably means it’s your logic before the loop that deals with the dA value for the output layer that’s wrong. Note that you explicitly compute dAL and then invoke linear_activation_backward for the output layer before the loop, which is where the dA1 value should be getting added to the grads dictionary, right?

2 Likes

The reason the code needs to be structured that way is that the activation argument to linear_activation_backward is different for the output layer. You could have made that conditional within the loop, but that’s not the way they set up the template code.

1 Like

Hello @paulinpaloalto, I totally understand what you are talking about, and I 've made the activation argument ‘relu’ in the output layer, but I can’t figure out what changes I should make to get the code work.
Thanks and apologizing for being late.

The activation function is “relu” for the hidden layers, right? For the output layer it is “sigmoid”.

The place to look for the error is before the for loop. In back propagation, everything happens in the reverse order: we do the calculations for the output layer first and then we enter the loop for the hidden layers.

3 Likes

Thanks so much for your help, there was a small typo and the problem is solved :smiley:.

2 Likes

My error was:

dA0 = [[ 0.          0.12853824]
 [ 0.         -0.08041233]
 [ 0.         -0.07888325]
 [ 0.         -0.18221184]]
dA1 = [[-0.41311681 -0.10826111]
 [ 0.45350638  0.11884557]
 [-0.0532252  -0.01394816]]
dW1 = [[0.10087189 0.0192033  0.03393989 0.02583208]
 [0.         0.         0.         0.        ]
 [0.01299615 0.00247412 0.00437275 0.00332816]]
dW2 = [[ 0.33288923 -0.16886813 -0.53686918]]
db1 = [[-0.05413055]
 [ 0.        ]
 [-0.00697408]]
db2 = [[0.25460995]]
Error: Wrong output for variable dA1.
Error: Wrong output for variable dW2.
Error: Wrong output for variable db2.
Error: Wrong output for variable dA0.
Error: Wrong output for variable dW1.
Error: Wrong output for variable db1.
 2  Tests passed
 1  Tests failed

Just to remember the expected inputs for the linear_activation_backward function:

linear_activation_backward(**dA**, cache, activation_function)

Thanks @MoHassan and @paulinpaloalto

1 Like

I have the same error and output results as @gubertoli; however, I still struggle to find the wrong part of my code. Could someone clarify the solution a little bit more?

Ohh, now I see the ‘typo’ that mentioned above :smiley: , it also worked for me