# Course 4 Week 1 Lab 1 Exercise 8 pool_backward

I’m getting a non-broadcastable output operand with shape (2,1) doesn’t match the broadcast shape (2,2) error…

For the statement
dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += mask*dA[i, h,w, c]

The mask is a 2x2 array… dA should be a specific value that should be multiplied by the mask… This should then be copied to all the cells in the dA_prev array denoted by the slice… right?

not sure where to look…

thanks,

rick

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)
100 # Set dA_prev to be dA_prev + (the mask multiplied by the correct entry of dA) (≈1 line)
→ 101 dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += mask*dA[i, h,w, c]
102
103 elif mode == “average”:

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

The shapes should be as follows:

• dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] : (2, 2)
• dA[i, h, w, c]: scalar

Interesting. I would have said that the shapes (2,1) and (2,2) should be broadcastable in general, but I think what that says is that it must be the LHS there that is the wrong shape. If the RHS was (2,1) and the LHS was (2,2), then you’d be fine, right? Although that would still be wrong based on our understanding of what is really supposed to be happening here.

Anytime you have a shape mismatch, the first debugging step is “Ok, what shapes are they?” Balaji has showed you what they should be. So the second question is “Which one is wrong?” and then question 3 is “Ok, how did it get that way?”

I’ve spent a fair amount of time trying to figure this out… The LHS of the equation is given… so that should be right… The mask is right… and the other part is a scaler… So that should be OK as it iterates through the slice. This is the only place that this variable is assigned… other than the initialization prior to the loop… I’ve tried different combinations of the indexes for dA… not sure what else to do…

Well, the code for the LHS is given, but the result depends on the inputs, right? What if your `horiz_start` and `horiz_end` values are wrong?

the Horiz_start and Horiz_end calculations should be that same as the earlier exercise 3… that worked…

Sure, it should work in principle. But it seems not to be working. Have you actually tried printing the values to make sure?

But note that just because pad = 0 in one test case does not mean you can just ignore it, right? What if the next test case has padding?

yes… the first two iterations look right… horiz… 0:2 then 1:3 never gets to the iteration for vertical…

Well, ok, at what point does it fail?

Opps… needed to subtract 1 from the end points as the filter is a 2x2 not 3x3…

now I get to the following error…

AssertionError 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)
119
120 # Making sure your output shape is correct
→ 121 assert(dA_prev.shape == A_prev.shape)
122
123 return dA_prev

AssertionError:

Ok, well, how do you debug shape mismatches? You start by printing both of them. Which one is wrong? How did it get that way?