# Week 2 exercise 5 (propagate)

Hi, what am I doing wrong? How to fix it? I’ve been stuck here for a very long time…

[Moderator edit: solution code removed]

## And here is the error message

ValueError Traceback (most recent call last)
in
3 X = np.array([[1., -2., -1.], [3., 0.5, -3.2]])
4 Y = np.array([[1, 1, 0]])
----> 5 grads, cost = propagate(w, b, X, Y)
6
7 assert type(grads[“dw”]) == np.ndarray

in propagate(w, b, X, Y)
39 # db = …
40 # YOUR CODE STARTS HERE
—> 41 dw = X * ((A - Y).T) / m
42 db = np.sum((A - Y)) / m
43 # YOUR CODE ENDS HERE

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

The issue here seems to be with the shape/dimension of one of the variables. I’d suggest that you print the shape of m, X, A, and Y. That way you can see where the shape issues originated.

Hi, @Doron_Modan.

Study your traceback (i.e. error message). You are throwing a ValueError exception saying that your expression for dw is violating the rules of matrix algebra. As you can see in the test case, X is set up as a 2 \times 3 dimensional matrix; Y is 1 \times 3, but A-Y is transposed so the second factor is 1 \times 3. You are using the asterisk operator which performs element-by-element matrix multiplication. That operation requires that the two factor matrices have exactly the same dimensions, so no good.

You want ordinary matrix multiplication here, which requires that the inner dimensions agree. In this case, 3 = 3, so good. The product will be a 2 \times 1 matrix (determined by the outer dimensions of the factor matrices. The np.dot() function performs matrix multiplication, not *. [BTW, you can also use the @ operator for matrix multiplication, but you will not see that one applied in the DLS.]

At least a couple of things may be at the root of your problem. Either you do not have a grasp on basic matrix operations, or you have Python syntax issues. Try to remediate either, or both.

You may have other problems, but let’s start with that which is revealed by the traceback.

Note well: You are not allowed to post your solution code in Discourse or any other public forum. pursuant to the Coursera honor code (that you signed off on.) In other words, it is cheating. Suppose that you were in a lecture hall and you dropped off your solutions to a friend on the way out. Even if they were wrong, it’s bad behavior and will run afoul of the proctors.

1 Like

Thank you so much, that solved it (the matrix multiplication) instead of element-wise multiplication!

I have no intention of breaking the honor code. Sorry for that, you are 100% right. For the future, what is the proper way to let the mentor know of my solution (in order to help them find my mistake easily), without revealing it to the other sudents? The paralel to the lecture hall situation would be approaching the teacher. Is there a way to contact a mentor privately?

Also, thak you for the very clear explanation, it is true that I am learning so much new material in a short time.

The accepted practice is to post the (complete) traceback. Typically some of your code will show up in that, but that’s OK.

Although, you can direct message a mentor (click on their photo or icon), it is better to post in the public Discourse forum so that all may benefit from the discussion. It also creates a useful archive entry to enhance the value of Discourses’ search capabilities.

Cheers,
Ken B

1 Like

Understood, thank you!