W4_A1_Matrix incongruency causing value error

Hi mentors, please help! I have been back and forth through the code and tried multiple iterations, but on Exercise 5 model forward I get a discrepancy in my matrix shapes:
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)
44 print(f’b{L-1} shape: {parameters[“b” + str(L-1)].shape}‘)
45
—> 46 AL, cache = linear_activation_forward(A, parameters[‘W’ + str(L-1)], parameters[‘b’ + str(L-1)], activation=‘sigmoid’)
47 print(f’After sigmoid activation - AL shape: {AL.shape}’)
48 caches.append(cache)

in linear_activation_forward(A_prev, W, b, activation)
24 print(f’A_prev shape: {A_prev.shape}‘)
25 print(f’W shape: {W.shape}’)
—> 26 Z, linear_cache = np.dot(W, A_prev) + b, (A_prev, W, b)
27 A, activation_cache = sigmoid(Z)
28

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

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

I printed my matrix shapes:
After ReLU activation - A1 shape: (4, 4)
A2 shape: (4, 4)
W2 shape: (3, 4)
b2 shape: (3, 1)
A_prev shape: (4, 4)
W shape: (3, 4)
After sigmoid activation - AL shape: (3, 4)
After ReLU activation - A2 shape: (3, 4)
A2 shape: (3, 4)
W2 shape: (3, 4)
b2 shape: (3, 1)
A_prev shape: (3, 4)
W shape: (3, 4)

Seems like the issue is A_prev so I’m including my code for step 4 as well.

GRADED FUNCTION: linear_activation_forward

def linear_activation_forward(A_prev, W, b, activation):
“”"
Implement the forward propagation for the LINEAR->ACTIVATION layer

Arguments:
A_prev -- activations from previous layer (or input data): (size of previous layer, number of examples)
W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)
b -- bias vector, numpy array of shape (size of the current layer, 1)
activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"

Returns:
A -- the output of the activation function, also called the post-activation value 
cache -- a python tuple containing "linear_cache" and "activation_cache";
         stored for computing the backward pass efficiently
"""

if activation == "sigmoid":
    #(≈ 2 lines of code)
    # Z, linear_cache = ...
    # A, activation_cache = ...
    # YOUR CODE STARTS HERE
    
    # moderator edit: code removed

    # YOUR CODE ENDS HERE

elif activation == "relu":
    #(≈ 2 lines of code)
    # Z, linear_cache = ...
    # A, activation_cache = ...
    # YOUR CODE STARTS HERE
    
    Z, linear_cache = np.dot(W, A_prev) + b, (A_prev, W, b)
    A, activation_cache = relu(Z)
    
    # YOUR CODE ENDS HERE
cache = (linear_cache, activation_cache)

return A, cache

GRADED FUNCTION: L_model_forward

def L_model_forward(X, parameters):
“”"
Implement forward propagation for the [LINEAR->RELU]*(L-1)->LINEAR->SIGMOID computation

Arguments:
X -- data, numpy array of shape (input size, number of examples)
parameters -- output of initialize_parameters_deep()

Returns:
AL -- activation value from the output (last) layer
caches -- list of caches containing:
            every cache of linear_activation_forward() (there are L of them, indexed from 0 to L-1)
"""

caches = []
A = X
L = len(parameters) // 2                  # number of layers in the neural network

# Implement [LINEAR -> RELU]*(L-1). Add "cache" to the "caches" list.
# The for loop starts at 1 because layer 0 is the input
for l in range(1, L):
    A_prev = A 
    #(≈ 2 lines of code)
    # A, cache = ...
    # caches ...
    # YOUR CODE STARTS HERE
   
    # moderator edit: code removed

    # YOUR CODE ENDS HERE

# Implement LINEAR -> SIGMOID. Add "cache" to the "caches" list.
#(≈ 2 lines of code)
# AL, cache = ...
# caches ...
# YOUR CODE STARTS HERE
   
# moderator edit: code removed
    
# YOUR CODE ENDS HERE
      
return AL, caches

is it how I handled L in the AL= section? I tried multiple variations. Please help this is my first AI course and I am apparently a child.
cheers, Brian

Please do not post your code on the forum. That’s not allowed by the Code of Conduct.

If a mentor needs to see your code, we’ll contact you with instructions.

I have edited your post to remove the code.

I’ll follow up with some tips shortly.

In linear_activation_forward(), you should call linear_forward(), since that’s why the function exists. There’s no reason to debug the same code twice. You just pass it the A_prev, W and b parameters.

Also, the code you added at the end of that line , (A_prev, W, b) does nothing useful, because you don’t have a function name there.

image

The same comment applies to the code for the relu activation.

In your code for L_model_forward(), why are you using L-1 here?
image

Here’s a thread which shows the dimensional analysis for that test case. It looks like you are not getting to the output layer, which should give a 1 x 4 output.

The hidden layers are handled in the for loop and there are two of them in this test case. Then the output layer is handled after you fall out of the for loop. Also note that the indentation looked a little sketchy in your code. That is critical in python: it defines what is in the body of a loop.

Also try the following experiment and watch what happens:

for ii in range(1,4):
    print(f"ii = {ii}")

print(f"After loop ii = {ii}")

thank you, utilizing the linear_forward() was the issue. I should have thought about pulling it forward, but I got to focused on the cell I was on. Sorry about the code posting on the forum

No problem.

thank you Paul