DLS Course 4 Programming Assignment: Convolutional Model, Step by Step Exercise 8 - pool_backward

My code:
{moderator edit - solution code removed}

The error:

Please help me with this problem!

Remember that in back prop, we are doing the reverse mapping that we do in forward prop. In a pooling layer in forward prop, you go from a square patch of the input to a single value of the output. So in backprop, we go from a single value of the output to a square patch of the input. That error message is saying that one side of your assignment is square and the other is not. Note that there should be no “ranges” like vert_start to vert_end on the output side (the RHS of that assignment statement), right? It’s a single element in the output that you map back by using the mask to expand it to a square patch.

Thanks. But I’m still a little confused. I tried this
dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += mask * dA[i, h, w, c]
and still failed.

Ok, what is the error message that you get? If it’s still the same as before, then what is the shape of the LHS there? And what is the shape of the RHS?

This is how you debug: the first rule is “believe the error message”. The first step is to figure out what it’s telling you. In your case, it’s that the shapes are wrong. Then you work from there: what should they be? Why are they wrong?

I added some print statements to my pool_backward logic and here’s what I see when I run that test case:

A_prev.shape = (5, 5, 3, 2)
dA.shape = (5, 4, 2, 2)
a_slice_prev.shape = (2, 2)
mask.shape = (2, 2)
a_slice_prev.shape = (2, 2)
mask.shape = (2, 2)
a_slice_prev.shape = (2, 2)
mask.shape = (2, 2)
a_slice_prev.shape = (2, 2)
mask.shape = (2, 2)