Error with Z dimension

Hi everyone!

Can someone please help me with this as I’ve gone through the forum and couldn’t find a way to solve it. I keep getting the following error when I run L_model_back FUNCTION.
I’m passing the right dA’s. For the hidden layers I’m passing dA[l+1] and dAL for the output layer

Are you sure that you didn’t modify the logic in linear_activation_backward that parses the current_cache into the linear and activation caches? That code was just given to you and doesn’t need to be modified.

If that’s not the problem, then the next step is to print the shapes of dA and the Z value in the linear_activation_backward logic and see if that gives you any clue. It’s a mess to do the debugging print in relu_backward, because that function is in a separate “utils” python file.

Note that we can tell the error is happening in one of the hidden layers, not the output layer from where the exception is thrown. Maybe also print the layer number in the loop to see that as well.

I have only added the calculation that was needed. Even the the test block for that function runs without any issues. Little bit confused :confused: I will try to check the shapes of dA and Z.

The bug is not in linear_activation_backward then: it’s in how you call it. What did you set dA_prev_temp to before you entered the “for” loop?

The point is to figure out which layer is throwing the error. My bet is that it’s the last hidden layer.

Not sure whether its okay to share a snipped of my code, but I will delete later if its not allowed.

This is for the output layer:

{moderator edit - solution code removed}

And then I passed this dA_prev_temp as parameter in the for loop

That looks fine. Then the error throws in the for loop. Which iteration of the loop and what the shapes? I added print statements to my L_model_backward and here’s what I see:

total layers = 2
AL = [[1.78862847 0.43650985]]
Y = [[1 0]]
dAL = [[-0.5590876   1.77465392]]
L_model_backward output layer l = 2
dZ.shape (1, 2)
A_prev.shape (3, 2)
W.shape (1, 3)
L_model_backward hidden layer l = 1
dA_prev_temp.shape (3, 2)
Z.shape (3, 2)
dZ.shape (3, 2)
A_prev.shape (4, 2)
W.shape (3, 4)
dA0 = [[ 0.          0.52257901]
 [ 0.         -0.3269206 ]
 [ 0.         -0.32070404]
 [ 0.         -0.74079187]]
dA1 = [[ 0.12913162 -0.44014127]
 [-0.14175655  0.48317296]
 [ 0.01663708 -0.05670698]]
dW1 = [[0.41010002 0.07807203 0.13798444 0.10502167]
 [0.         0.         0.         0.        ]
 [0.05283652 0.01005865 0.01777766 0.0135308 ]]
dW2 = [[-0.39202432 -0.13325855 -0.04601089]]
db1 = [[-0.22007063]
 [ 0.        ]
 [-0.02835349]]
db2 = [[0.15187861]]
total layers = 2
AL = [[1.78862847 0.43650985]]
Y = [[1 0]]
dAL = [[-0.5590876   1.77465392]]
L_model_backward output layer l = 2
dZ.shape (1, 2)
A_prev.shape (3, 2)
W.shape (1, 3)
L_model_backward hidden layer l = 1
dA_prev_temp.shape (3, 2)
Z.shape (3, 2)
dZ.shape (3, 2)
A_prev.shape (4, 2)
W.shape (3, 4)
total layers = 2
AL = [[1.78862847 0.43650985]]
Y = [[1 0]]
dAL = [[-0.5590876   1.77465392]]
L_model_backward output layer l = 2
dZ.shape (1, 2)
A_prev.shape (3, 2)
W.shape (1, 3)
L_model_backward hidden layer l = 1
dA_prev_temp.shape (3, 2)
Z.shape (3, 2)
dZ.shape (3, 2)
A_prev.shape (4, 2)
W.shape (3, 4)
total layers = 2
AL = [[1.78862847 0.43650985]]
Y = [[1 0]]
dAL = [[-0.5590876   1.77465392]]
L_model_backward output layer l = 2
dZ.shape (1, 2)
A_prev.shape (3, 2)
W.shape (1, 3)
L_model_backward hidden layer l = 1
dA_prev_temp.shape (3, 2)
Z.shape (3, 2)
dZ.shape (3, 2)
A_prev.shape (4, 2)
W.shape (3, 4)
 All tests passed.

I was tracking the iterator in the for loop, when I print the value of ‘l’, it starts with 0. L=2, so the iterator should start from 1 not 0, correct?

The actual layer you are working on is l + 1, right? And then you produce dA for l as the output. Remember that loop indexes are “0 based” in python.

The loop should start from L-1 as we used reversed(range(L-1)). I’m printing 'l; inside the loop and I get 0

I will delete the snapshot once you take a look
Deleted

Please do not share your code on the forum.

Sharing should only be done if the mentor asks you to send them your code via a private message.

That’s not how indexing works in python, which I mentioned above. Watch this:

for ii in range(4):
    print(f"ii = {ii}")
print(f"After loop ii = {ii}")
ii = 0
ii = 1
ii = 2
ii = 3
After loop ii = 3


for ii in reversed(range(5)):
    print(f"ii = {ii}")
print(f"After loop ii = {ii}")
ii = 4
ii = 3
ii = 2
ii = 1
ii = 0
After loop ii = 0

So you need to deal with that in your logic.

But actually I would say that the code you showed looks correct to me. So this is a bit of a puzzle. Are you sure you really ran your new code? Note that if you just type new code and then run the test again without actually clicking “Shift-Enter” on the modified cell, then it simply runs the old code.

I was running both cells the code and test :confused: I really don’t know what is wrong with the code :frowning:

Please check your DMs for a message from me. You can recognize DMs in your feed by the little “envelope” icon.

Thanks a lot Paul! I really appreciate your support! I managed to solve it by resetting the environment and writing the code again!

1 Like

That’s great news that you were able to get things working under your own power! The code did look correct from what I could see. Onward! :nerd_face: