DLS4 -Week1- Assignment 1- Exercise 8 - Pool_backward

Hi,

Please explain me what i am doing wrong in this.
(5, 4, 2, 2)
(5, 5, 3, 2)

ValueError Traceback (most recent call last)
in
7 dA = np.random.randn(5, 4, 2, 2)
8
----> 9 dA_prev1 = pool_backward(dA, cache, mode = “max”)
10 print(“mode = max”)
11 print('mean of dA = ', np.mean(dA))

in pool_backward(dA, cache, mode)
85 a_prev_slice = a_prev[vert_start:vert_end,horiz_start:horiz_end,c]
86 mask = create_mask_from_window(a_prev_slice)
—> 87 dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += dA_prev[i,h,w,c] + (mask * dA_prev[i,h,w,c])
88 elif mode == “average”:
89 da = distribute_value(dA, (n_H,n_W))

ValueError: non-broadcastable output operand with shape (2,1) doesn’t match the broadcast shape (2,2)

Hi Ruchi,

The += operator already adds to dA_prev. So you do not want the additional dA_prev.

Hello,

I am still getting the same error. Could you advise?

104 # Set dA_prev to be dA_prev + (the mask multiplied by the correct entry of dA) (≈1 line)
→ 105 dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += mask * dA[i, h, w, c]
106
107 elif mode == “average”:

ValueError: non-broadcastable output operand with shape (2,1) doesn’t match the broadcast shape (2,2)

TIA

Hi shatadrusaha,

It looks like there is a problem with your code elsewhere. Could you send me your code through a direct message so I can have a look?

Hello reinoudbosch,

Thanks for your response. I did a silly mistake while initialising ‘dA_prev’. I had initialised it with the dimensions of “dA” rather than “A_prev”. After doing the respesctive correction, the code runs fine without any error.

Thanks,
Shatadru

3 Likes

Hi ad thanks, your answer helped me figure out I was taking the wrong slice of dA. Can anyone shed some light though as to why we do dA[i, h, w, c] instead of dA[i, vert_start:vert_end, horiz_start:horiz_end, c]?

2 Likes

I have also the same question.

Hi, does anyone know the answer to this question?

If that is the question you are referring to, the answer is that you have to keep track of which “space” you are indexing in: the input space or the output space. h and w are index values in the output space. vert_start and horiz_start are indices in the input (prev) space. In the general case, the shapes of input and output are different, right? You also have to keep track of the fact that we’re going “backwards” here in backprop: the input is actually the output! :scream_cat:

But the fundamental point is to remember that forward and backward propagation are the mirror images of each other. In forward propagation, we are mapping from a “patch” or “slice” of the input space to a single point in the output space. So when we reverse things and do back prop, we are mapping from a single point in the output space back to a “patch” in the input space.

1 Like

I would just like to add, for those working on this in year 2024, that the line 89 in this question:
89 da = distribute_value(dA, (n_H,n_W)
is not correct :). I think the assignment might have changed because I saw the same line in another question from 2021 concerning this assignment and nobody replied saying that this line was incorrected (and I think after fixing some other parts, the students fixed their bug).
Anyway, it took me quite some time to realize what “da” from the hint “# Get the value da from dA (≈1 line)” actually is, because I had not seen this variable anywhere else, not in the code, nor in the description above. So I was struggling to understand what I was supposed to get. (But I finally figured it out :)!)

Glad to hear that you figured out the solution here. Nice work!

Yes, that line 89 as shown is probably wrong, both then and now. (We can’t say for sure, because we can’t see if they separately assigned anything to dA.) The reason it’s not commented on here on this thread is that the error was thrown before they got to that line. Meaning that there were multiple problems and the approach is always “one step at a time”, right? :nerd_face:

People just might not have noticed because they were less alert than you are or were just focussed on the specific question at issue. :smile:

1 Like