# Week 4 Exercise 9 L_model_backward wrong shape and output

I see that the error suggests the variables shapes and outputs are wrong. I’ve been staring at my formulas for a while but couldn’t figure out what’s wrong yet…

## 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)
in
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.

I think one of my mistakes was in the second loop (corrected from l-1 to l-2). Now the error code seems to suggest that I made a mistake with dZ (but I don’t think that’s the case…)

IndexError 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)
59 # YOUR CODE STARTS HERE
60 current_cache = caches[l-2]
—> 61 dA_prev_temp, dW_temp, db_temp = linear_activation_backward(dAL,current_cache,activation=“relu”)
62 grads[“dA” + str(l)] = dA_prev_temp
63 grads[“dW” + str(l + 1)] = dW_temp

in linear_activation_backward(dA, cache, activation)
22 # dA_prev, dW, db = …
23 # YOUR CODE STARTS HERE
—> 24 dZ=relu_backward(dA,activation_cache)
25 dA_prev,dW, db = linear_backward(dZ, linear_cache)
26

~/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 1 but corresponding boolean dimension is 3

Hi, @Kimberly_Chan. It looks like you have an indexing error. In your backprop loop, you are picking up the `l-2` element from the current cache. That’s a problem.

The loop initiates with `for l in reversed(range(L-1))`. The `reversed` function makes it a bit tricky. To see what’s going on, you can create your own cell with “Insert → Cell Below” and enter

``````for l in reversed(range(L-1)):
print(l)
``````

You will see that you will want to change your index for caches. in your `current_cache = ...` statement.

1 Like

Hello, I seem to get the same IndexError, but I can’t find where I went wrong…

IndexError 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)
60
61 current_cache = caches[l]
—> 62 dA_prev_temp, dW_temp, db_temp = linear_activation_backward(dAL, current_cache, “relu”)
63 grads[“dA” + str(l)] = dA_prev_temp
64 grads[“dW” + str(l + 1)] = dW_temp

in linear_activation_backward(dA, cache, activation)
22 # dA_prev, dW, db = …
23 # YOUR CODE STARTS HERE
—> 24 dZ = relu_backward(dA, activation_cache)
25 dA_prev, dW, db = linear_backward(dZ, linear_cache)
26

~/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 1 but corresponding boolean dimension is 3

This is a different kind of “indexing” problem than the one Ken was pointing out earlier on this thread. You can have a look at the logic of the relu_backward function by clicking “File → Open” and then opening the appropriate python file (see the “import” block at the beginning of the notebook to figure out the name). What you will see is that this error means that the dA value and the cache value (which is just Z in this case) that you passed from linear_activation_backward to relu_backward don’t match. So now the question is why that happened. It could be that it’s the dA value, not the activation cache value, that is incorrect. Put some print statements to show the shapes and then compare that to your “dimensional analysis”.