Help with the Week 3 Assignment

On 4.3, Excercise 4: Forward Propgation, I am getting that following error:

in forward_propagation(X, parameters)
32 # A2 = …
—> 34 Z1=W1 * X+b1
35 A1=np.tanh(Z1)
36 Z2=W2 * A1+b2

ValueError: operands could not be broadcast together with shapes (4,2) (2,3)

I am not too sure what is happening. I have made sure my code looks like Z1=W1 * X+b1, but the W1 and t_X used in the testing doesnt match for braodcasting.

Your mistake is that you need to be doing a dot product and not “elementwise” multiply in the operation between W1 and X. “*” means “elementwise” multiplication. The way to express dot product in numpy is or the “@” operator.

This was covered in the lectures, but the other thing to realize is the notation used in math formulas in the notebooks. If two matrices or vectors are written side by side with no “operator” shown between them, then it means that the operation is “matrix multiply”. That is the full “dot product” style matrix multiply, which is a completely different operation than “elementwise” matrix multiply. When they want to express “elementwise” matrix multiply, they will always use the “*” operator to show that.

1 Like

I appreciate that! I kind of figured that was it. I thought I tried that first, but I must have had an error and then tried something else. Thank you for the clarification on the notation.

However, I do have anyone question.

On the Week 4 1st assignment, excercise 5:

I am getting the following error:

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

in L_model_forward(X, parameters)
27 # caches …
—> 29 A, cache=linear_activation_forward(A_prev, W, b, relu)
30 cashes.append(cache)

NameError: name ‘W’ is not defined

I am trying to use the linear_activation_forward function from the previous excercise, but do I need to pull the parameters W and b from the parameters that will be fed into the “L_model_forward(X, parameters)” function?

Yes, at each layer, you need to extract the appropriate elements from the parameters dictionary to be passed to the functions that implement the layer-wise forward propagation actions.

Thank you very much.