Week4 _Assignment1_Exercise5

Hello here , After completing the model-forward function , I’m getting this specific error (below) ,
*is there anything i’m getting wrong or missing? according to the error , the matrix multiplication rule won’t work for w times A , how to do i go about this? *
Your guidance will be much appreciated

ValueError Traceback (most recent call last)
in
1 t_X, t_parameters = L_model_forward_test_case_2hidden()
----> 2 t_AL, t_caches = L_model_forward(t_X, t_parameters)
3
4 print("AL = " + str(t_AL))
5

in L_model_forward(X, parameters)
27 # caches …
28 # YOUR CODE STARTS HERE
—> 29 A , cache = linear_activation_forward(X, parameters[‘W’+str(l)] , parameters[‘b’ + str(l)] , activation =“relu” )
30 caches.append(cache )
31

in linear_activation_forward(A_prev, W, b, activation)
31 # A, activation_cache = …
32 # YOUR CODE STARTS HERE
—> 33 Z , linear_cache = linear_forward(A_prev, W ,b)
34 A , activation_cache = relu(Z)
35

in linear_forward(A, W, b)
19 # YOUR CODE STARTS HERE
20
—> 21 Z = np.dot( W, A) + b
22
23 # YOUR CODE ENDS HERE

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

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

1 Like

I would add some printouts for W and A in the code to check their values and in particular their shape. In order to multiply W and A their shapes should be compatible (Matrix multiplication - Wikipedia)

2 Likes

It’s also worth having a look at this thread to see the “dimensional analysis” for this test case. It really helps debugging to know what you should be expecting at each layer.

If you look at that, you’ll see that 3 x 4 is the shape of W2 and 5 x 4 is the shape of X. If you are at layer 2, the “linear activation” calculation should be:

Z2 = W2 \cdot A1 + b2

Where the shape of A1 is 4 x 4. So why would you end up with X as the input at layer 2?

2 Likes

I hv now used A_prev in the Equation after pondering over your thread , Am i correct?
if yes why am i still getting this error ?

in L_model_forward(X, parameters)
39 # caches …
40 # YOUR CODE STARTS HERE
—> 41 AL ,cache = linear_activation_forward( A_prev,parameters[‘W’+str(l)].T, parameters[‘b’ + str(l)] , activation =“sigmoid” )
42 caches.append(cache)
43

in linear_activation_forward(A_prev, W, b, activation)
22 # A, activation_cache = …
23 # YOUR CODE STARTS HERE
—> 24 Z , linear_cache = linear_forward(A_prev, W ,b)
25 A , activation_cache = sigmoid(Z)
26 # YOUR CODE ENDS HERE

in linear_forward(A, W, b)
19 # YOUR CODE STARTS HERE
20
—> 21 Z = np.dot( W, A) + b
22
23 # YOUR CODE ENDS HERE

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

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

from my perspective , A_prev ( which is A2) in the equation of AL( A3) has a dimension of ( 3 , 4 ). so I don’t understand why A1 of dimension ( 4,4) is used here. I also wonder where W (4,3) is coming from because normally w3 has a dimension of ( 1,3).

Well, it’s your code that is producing that result, so you will need to examine more closely what it is doing. Note that no-one else can see your notebooks. On the question of the W value being 4 x 3, did you read that other thread I linked that gave the dimensional analysis? There is literally no object mentioned anywhere there that is 4 x 3, right? So how did that happen in your case? You must have transposed something or made some other mistake.

Update: actually you can see the transpose in your code. Why did you do that? The transpose is only in Week 2 Logistic Regression. Once we go to real Neural Networks in Week 3 and Week 4 there is no longer a transpose. It doesn’t show that in any of the formulas, right?

Yh It was a mistake I made ,I corrected it thanks for indicating .
the output I got for AL seems to be wrong in shape and value ( it has taken the shape of A2) :
AL = [[0.90589057 0.75020632 0.05200643 0.82351754]
[0.99823392 0.08462048 0.01610661 0.98885794]
[0.9999688 0.33941221 0.83703792 0.99971951]]
Error: Wrong shape for variable 0.
Error: Wrong shape for variable 0.
Error: Wrong shape for variable 1.
Error: Wrong shape for variable 2.
Error: Wrong shape for variable 1.
Error: Wrong output for variable 0.
Error: Wrong output for variable 0.
Error: Wrong output for variable 1.
Error: Wrong output for variable 2.
Error: Wrong output for variable 1.
1 Tests passed

I’m still trying to analyze the code to see what is wrong.

I got the perfect AL , thank you so much.
I’m glad.

1 Like

Congratulations on solving this! It’s the most complicated code we’ve seen so far. Onward! :nerd_face:

1 Like

Estoy exactamente en este punto
AL = [[ 2.2644603 1.09971298 0. 1.54036335]
[ 6.33722569 0. 0. 4.48582383]
[10.37508342 0. 1.63635185 8.17870169]]
Error: Wrong shape for variable 0.
Error: Wrong shape for variable 0.
Error: Wrong shape for variable 1.
Error: Wrong shape for variable 2.
Error: Wrong shape for variable 1.
Error: Wrong output for variable 0.
Error: Wrong output for variable 0.
Error: Wrong output for variable 1.
Error: Wrong output for variable 2.
Error: Wrong output for variable 1.
1 Tests passed
2 Tests failed


AssertionError Traceback (most recent call last)
in
4 print("AL = " + str(t_AL))
5
----> 6 L_model_forward_test(L_model_forward)

~/work/release/W4A1/public_tests.py in L_model_forward_test(target)
238 ]
239
→ 240 multiple_test(test_cases, target)
241 ‘’’ {
242 “name”:“datatype_check”,

~/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_forward. Check your equations and avoid using global variables inside the function.

Gracias por la ayuda

The best idea is to start by reading the thread I linked above with dimensional analysis for this test case. Note that your AL is 3 x 3. Any of the A values should have 4 columns, right? Because there are 4 samples in the input. So how did that happen?

Start by checking the dimensions of the A and W values at each layer.

Thank you very much, I could already find my mistake.

1 Like

That’s great to hear! Onward! :nerd_face:

just to save others’ time, try this Python Tryit Editor v1.0
you will understand how the for i in range(1,L) loop works
it took me more than an hour just because of this number :slight_smile: