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.