Wrong values for dW1

Z1 is 4x3 and G’ is 4x3. Now if we do dot product we will get either 4x4 or 3x3 matrix
But W[2]T . dZ[2] are resulting 4x3 So, element wise multiplication not possible. Kindly tell where I am wrong.
dZ[2], dW[2], db[2] are correct.
dW[1] is wrong.

Below is code snippet:

{moderator edit - solution code removed}

The output is:

db2 dim (1, 1)
A1 (4, 3)
W2 Shape (1, 4)
W1 Shape (4, 2)
dZ2 Shape (1, 3)
Z1 (4, 3)
X (2, 3)
m is 3
So far dZ1 (4, 3)
2nd part dim (3, 3)
g’ shape (4, 3)

Request you to kindly help.

Thanks & Regards
Suhas Gupta

It looks like you are talking about the back propagation function in Week 3 Planar Data Assignment, right? It helps if you can specify which problem you are working on.

Why are you transposing the output of g^{[1]'}(Z1)? That is not specified in any of the formulas, right?

Z1 is 4 x 3 so g(Z1) will also be 4 x 3, right? The activations are always “elementwise”.

W2 is 1 x 4 and dZ2 is 1 x 3, so

W2^T \cdot dZ2 will be 4 x 3.

So the elementwise multiply works fine.

Hi Paulinpaloalto,

Thank you for reply to request. Yes it is back propagation function Week 3 Planar Data.

{moderator edit - solution code removed}

grads = {"dW1": dW1,
         "db1": db1,
         "dW2": dW2,
         "db2": db2}   

Now the ouput is

dW1 = [[-1.86811843e-05 -5.66831428e-06]
[-8.18945458e-05 1.41219110e-04]
[ 4.18935631e-05 4.40648489e-05]
[-4.87097048e-05 2.70780332e-04]]
db1 = [[-8.34072748e-06]
[-1.60295673e-04]
[-4.63167357e-06]
[-2.34307508e-04]]
dW2 = [[ 0.00078841 0.01765429 -0.00084166 -0.01022527]]
db2 = [[-0.16655712]]

If it is ok to post code like this, request you to kindly just guide I’ll debug. While I’ll watch lecture again.

Thanks & Regards
Suhas Gupta

You are misinterpeting the dZ1 formula. You don’t need to multiply by Z1, right? You are invoking the function g'(Z1). It just turns out that for tanh that happens to be:

g'(Z1) = 1 - tanh^2(Z1)

Which can also be expressed as (1 - A1^2), because:

A1 = tanh(Z1)

Hi @suhasgupta1, also just look at the way you are writing your codes for the bold ones:

{moderator edit - solution code removed}

Also, the element wise multiplication is missing when you are calling the codes for dZ1 and you can’t add cache here.

Thanks.

@Rashmi: I think the elementwise multiplies are actually there but Suhas did not use the </> tool to format the code “as is”. If you don’t do that then the * gets interpreted as markdown and ends up italicizing things. You can see that the np.power call is italicized there.

1 Like

Thank you for the clarification, @paulinpaloalto sir. You are right !

Hi @paulinpaloalto , Yes, Thankyou. Interpretation of formula was wrong. I was able to proceed.

Thanks & Regards
Suhas Gupta

Hi Rashmi,

Thank you for replying. I am able to proceed.

Thanks & Regards
Suhas Gupta

Great to know that buddy!

Rise and shine!