Excercise 6 backward_propagation
I have added all 6 backwards propagation formulas into the ‘Your Code Starts Here’ section and there are no syntax issues or misalignment of vectors, however the test function says the values are wrong. The values are slightly off compared to the expected result but I have no idea why?
I won’t post my code but this is what I get from the results:
Here are my results from that test cell with a couple of added prints to show the intermediate values of dZ2 and dZ1, which are used to compute the other quantities.
The other key thing to know here is that there are several different test cases, not all of which are directly visible in the notebook. You may have to read the file public_tests.py
to see the other test.
dZ2 = [[-0.4997693 0.49985831 -0.49976037]]
dZ1 = [[ 0.00528712 -0.00528824 0.00528716]
[ 0.00453054 -0.00454038 0.00453968]
[-0.00275488 0.00275645 -0.00275366]
[-0.01145044 0.01145559 -0.0114478 ]]
dW1 = [[ 0.00301023 -0.00747267]
[ 0.00257968 -0.00641288]
[-0.00156892 0.003893 ]
[-0.00652037 0.01618243]]
db1 = [[ 0.00176201]
[ 0.00150995]
[-0.00091736]
[-0.00381422]]
dW2 = [[ 0.00078841 0.01765429 -0.00084166 -0.01022527]]
db2 = [[-0.16655712]]
dZ2 = [[-0.6619883 -2.19926803 -0.13665468 -1.1809203 -0.60392063 -1.23005814
0.5505375 ]]
dZ1 = [[-0.29566091 -1.28277352 0.02249305 0.9074007 1.33800015 0.68564737
-0.35055957]
[-0.4140565 -1.79789169 -0.02663664 -0.89206642 1.5652835 -0.93523728
0.14524558]
[-0.58374539 -0.85869844 -0.12095004 -1.05524101 -0.54274041 -0.95273462
0.49244949]
[-0.18637787 -0.34557891 -0.0334634 -0.33225085 0.0475564 0.15365525
0.15183635]
[-0.50342817 -1.15246998 -0.09926505 -1.03902884 -0.47135171 -1.08670567
0.29998346]
[ 0.25606933 1.32742479 -0.05148612 0.74584305 0.29507368 -0.18450677
-0.40340853]
[-0.37453006 -0.24923951 -0.15907623 -1.47796364 0.66996151 -1.38803016
0.19589764]
[-0.08870237 -0.98945009 0.05061421 -0.60482225 0.49895105 0.16251438
0.23517207]
[ 0.19721439 0.26166043 -0.02535616 -1.01012876 -0.44143725 -0.19363978
0.27066004]]
All tests passed!
But note that your values different even for the visible test case, so there is clearly something wrong in your code. It might be worth printing dZ2 and dZ1 and comparing those values to mine as well.
The other key point is that you have to use A1 to compute the derivative of tanh
, not tanh(Z1).
Hi Paulin, thanks for the reply, but since my values differ from the first formula onwards, I wanted to see if we have the same inputs from the dictionary to eliminate the possibility that my starting values are wrong.
Could you send me the values you have for A1, A2, m and Y? Here are mine.
A2= [[0.5002307 0.49985831 0.50023963]]
Y= [[ True False True]]
m= 3
A1= [[-0.00616578 0.0020626 0.00349619]
[-0.05225116 0.02725659 -0.02646251]
[-0.02009721 0.0036869 0.02883756]
[ 0.02152675 -0.01385234 0.02599885]]
The values for dZ1 and dZ2 are different. dZ1 must be different if dZ2 is already wrong:
Mine:
dZ2= [[-0.74976925 0.24985833 -0.74976031]]
Yours:
dZ2 = [[-0.4997693 0.49985831 -0.49976037]]
Mine:
dZ1= [[ 0.0079319 -0.00264337 0.00793201]
Yours:
dZ1 = [[ 0.00528712 -0.00528824 0.00528716]
For computing 𝑔[1]′(𝑍[1]) I’m using (1 - np.power(A1, 2))
as suggested by the exercise. So I don’t think the mistake is there. The mistake happens in the very beginning.
It seems a bit of a stretch to think that the input values are wrong, but here are the answers for the first test case:
A1 [[-0.00616578 0.0020626 0.00349619]
[-0.05225116 0.02725659 -0.02646251]
[-0.02009721 0.0036869 0.02883756]
[ 0.02152675 -0.01385234 0.02599885]]
A2 [[0.5002307 0.49985831 0.50023963]]
Y [[ True False True]]
m 3
dZ2 = [[-0.4997693 0.49985831 -0.49976037]]
dZ1 = [[ 0.00528712 -0.00528824 0.00528716]
[ 0.00453054 -0.00454038 0.00453968]
[-0.00275488 0.00275645 -0.00275366]
[-0.01145044 0.01145559 -0.0114478 ]]
dW1 = [[ 0.00301023 -0.00747267]
[ 0.00257968 -0.00641288]
[-0.00156892 0.003893 ]
[-0.00652037 0.01618243]]
db1 = [[ 0.00176201]
[ 0.00150995]
[-0.00091736]
[-0.00381422]]
dW2 = [[ 0.00078841 0.01765429 -0.00084166 -0.01022527]]
db2 = [[-0.16655712]]
You can see that the A1, A2 and Y values agree with yours.
But why is your dZ2 value wrong? The formula for that is very simple:
dZ^{[2]} = A^{[2]} - Y
If your A2 and Y values are correct, there is not really much room to get that wrong.
Solved it. I indeed manged to get the formula for dZ2 wrong. All works fine now. Thanks for your help.