# Week 1 assignment 1 ungraded portion Pool_backward

i am getting wrong value for this pool_backward, the value of max pool mask is not adding to the da_prev. please let me know where the mistake is…

{moderator edit - solution code removed}

Thanks

Your code for slicing `A_prev` is different than mine because you “pre-slice” by i and then slice the result with 3 dimensions. The way I did this was to slice it once with all 4 dimensions at once. Either that or perhaps there is a bug in your `create_mask_from_window` function.

I added some print statements to show the shapes I get:

``````a_slice_prev.shape = (2, 2)
``````

Update: my theory above is wrong. I tried doing the slicing your way and get the same shapes. So I think we’re down to the theory that your `create_mask_from_window` is wrong or else there’s something wrong in your code that is off the right side of the image you are showing.

I would probably check out the dA your are getting, both in ‘max’ and ‘average’.

As per the instructions, you should get the ‘correct entry of dA’.

May be try getting the dA from the for-loops indices, i, h, w, c, instead of your current i, vert_start:vert_end, horiz_start:hiroz_end, c.

Thank you for the response,
Here is the code for mask mask = x/np.max(x) == 1 from create_mask_from_window, and i think it is correct. the problem could be with the slicing.

Interesting. I agree that mask computation looks ok from a “pure math” standpoint. I did it a bit differently: you can just compare x directly with np.max(x) for equality, right? Throwing a division into it may give you rounding problems in floating point. If we were doing this in the abstract beauty of \mathbb{R}, the two would be the same, but I’m not so sure in the world of floating point.

Thank you for the response,

i printed dA_prev in c loop to see if the addition happening correctly. this is what i got. is it correct? the dA_prev is getting added to the mask then for each iteration dA_prev value should be added to the previous value that way all the 4 positions of dA_prev will be filled up. but it is not happening

But the point is that dA_prev are the gradients. In “max” pooling mode, the whole point of the mask is that the gradient only affects the input values that produce the maximum output at each filter position, right? So some of the gradients (those corresponding to the non-max outputs) will be zero. Of course when you look at the results looping over c, those are all at different positions, right? To get real addition where different elements might be non-zero, you need to look at what happens in the i loop but looking at the same h, w and c position.

The operation per se is working, but the da that you are using is incorrect. You are picking “more” dA into the “da” than you should. Selecting just the single element from dA at each iteration is what you need. Your current selection of dA is causing the error at the Max and will cause it also at the Average. You are only seeing the error at the Max because the assertion halts the flow there, but the error is in both “max” and “average”.

Thank you so much, it is working

Excellent! I’m glad it is working.

[Optional] Can I ask you why is it working now and not before? What was going on?

It didnot work because of my mistake of changing indices, i, h, w, c in dA as well as dA_prev. As you said i should have changed only for dA.