Week 4 - L_layer_model

Hey everyone,

In my code, it is giving a error which so far I am not able to fix


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)
43
44 AL, cache = linear_activation_forward(A, parameters[“W”+ str(L)],
—> 45 parameters[“b”+ str(L)],activation =“sigmoid”)
46 caches.append(cache)
47

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

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 (1,3) and (4,4) not aligned: 3 (dim 1) != 4 (dim 0)

Any idea what could be the problem ?
thank you

The way to debug this type of problem is to work out the “dimensional analysis” for the case at hand. That means you start with the dimensions of all the input objects and the weight matrices and then you work through the layers in forward propagation and see what the shapes of the Z and A values will be at each level. Here is an earlier thread which shows that for the particular test case here.

Please have a look at that thread and it will make clear that you are at the output layer and the input should be A2, which should be of shape 3 x 4. That would fit with W3 being 1 x 3, right? So how did it happen that your A2 ends up being 4 x 4 instead? One clue is that the shape of A1 is legitimately 4 x 4. Hmmmm.

@paulinpaloalto
I am getting the same error message" ValueError: shapes (1,3) and (4,4) not aligned: 3 (dim 1) != 4 (dim 0)

I read the “earlier thread,” but I am having trouble figuring out where in the code can I access the indexing you mention for A. I know how to manage the indexing for w and b. I did try using A[L-1] but I still got an error:
“ValueError: shapes (1,3) and (4,) not aligned: 3 (dim 1) != 4 (dim 0)”

Look at how the code in the loop manages the variables A and A_prev. Now what happens when you fall out of the loop? What does A_prev contain? What variable did you reference for A in the output layer?

1 Like

For the output layer AL, I am using A since I thought should have the A(L-1) when the loop stops.

In the loop I am using A_prev.

When we fall out of the loop A_prev does not get updated, A should have the last activation. Am I interpreting it wrong?

It looks like A_prev is 5 by 4 (this is X or A0) and A is 4 by 4 (This is A1) from the test cases. So how do I get A2? It seems like the loop is not going far enough.

Message edited after I fixed my code:

I double checked my code… I had the AL, cache line indented too much. I reduced the indent and the code works.

Thanks!

Glad to hear you found the solution! Thanks for confirming.

Your hint and the “earlier thread” helped with the trouble shooting. So thanks.

Most programming languages use “end” or equivalent to indicate the end of a for loop. Python uses indentation.

Gotta pay attention or pay with wasted time!

1 Like

Indeed! The indentation thing in python is pretty annoying. Guido claims there was a justification to do it that way, but his reasons sound like self-serving rationalizations to me. My theory is that he just didn’t want to do the work of building a real parser. But it is what it is and we just have to play by the rules or (as you say) pay the price!

1 Like

Even after the previous cells ran smoothly with all test cases being passed getting dimension error for test case in exercise 5.Please help

Here’s a previous thread which gives the “dimensional analysis” for this test case. That’s always the way to start, so that you understand what should be happening at each layer. Once you’ve seen that, then what we see is:

Because you’ve specified “sigmoid” as the activation, that (we hope) means you’ve fallen out of the loop over the hidden dimensions and are processing the output layer. But at the output layer you should have these dimensions:

W3 is 1 x 3 and A2 is 3 x 4

But somehow you’ve got the wrong shape for W: you’re using the shape of W2. So how did that happen? Clue: what is the value of l (lower case ell) when you fall out of the loop?

1 Like

Okay got it.Thank you