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