Week 4 Assignment 1 Exercise 7

I am getting following error:
dA_prev: [[-1.15171336 0.06718465 -0.3204696 2.09812712]
[ 0.60345879 -3.72508701 5.81700741 -3.84326836]
[-0.4319552 -1.30987417 1.72354705 0.05070578]
[-0.38981415 0.60811244 -1.25938424 1.47191593]
[-2.52214926 2.67882552 -0.67947465 1.48119548]]
dW: 1.6954034753430811
db: [[-0.14713786]
[-0.11313155]
[-0.13209101]]
Error: Data type mismatch in variable 1. Got type: <class ‘numpy.float64’> but expected type <class ‘numpy.ndarray’>
Error: Wrong shape for variable 1.
Error: Wrong output for variable 1.
0 Tests passed
3 Tests failed


AssertionError Traceback (most recent call last)
in
6 print("db: " + str(t_db))
7
----> 8 linear_backward_test(linear_backward)

~/work/release/W4A1/public_tests.py in linear_backward_test(target)
308 ]
309
→ 310 multiple_test(test_cases, target)
311
312 def linear_activation_backward_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 linear_backward. Check your equations and avoid using global variables inside the function.

Any help would be appreciated .
Below is my code snippet:

{moderator edit - solution code removed}

Please check the formula for dW there. You have not implemented it correctly. You can see that your value ends up being a scalar float, but it should be an array the same shape as the corresponding W value for the given layer.

Thanks Paul for your quick response.

dW has shape of (n[l],n[l-1])
dA has shape of [n[l],m) , since we know the shape of A_prev , we know n[l] .
I am not sure how do i get n[l-1] ?

This was one implementation I was thinking :
(n[l],m) * (m,n[l]) ,
dW = np.sum(np.dot(dZ, np.transpose(A_prev[:,np.newaxis])))/m

But I am getting shape error :

ValueError Traceback (most recent call last)
in
1 t_dZ, t_linear_cache = linear_backward_test_case()
----> 2 t_dA_prev, t_dW, t_db = linear_backward(t_dZ, t_linear_cache)
3
4 print("dA_prev: " + str(t_dA_prev))
5 print("dW: " + str(t_dW))

in linear_backward(dZ, cache)
30
31 #dW = np.sum(np.dot(dZ, np.transpose(A_prev)[np.newaxis:,]))/m
—> 32 dW = np.sum(np.dot(dZ, np.transpose(A_prev[:,np.newaxis])))/m
33
34

<array_function internals> in dot(*args, **kwargs)

ValueError: shapes (3,4) and (4,1,5) not aligned: 4 (dim 1) != 1 (dim 1)

Still in the woods :frowning:

Pls. help

The whole point is that A_prev is n^{[l-1]} x m. That’s why they call it “prev”, right?

How on earth did you end up with a 3D object in that computation? Don’t use fancy syntax with np.transpose. If you want the transpose of A_prev, just use A_prev.T, right?

And it looks like you did not carry through on my very first instructions in my first reply. Your code for dW is wrong. Look at the formula again. Where is there a “sum” in that formula?

Thank You Sir , Not sure what I was thinking :frowning:
Correct implementation:
dW = np.dot(dZ, np.transpose(A_prev))/m

Sorry for my mess :frowning:

Thanks
Vinod