Week 1 Assignment 1 pool_forward

It seems I’m unable to find what I’m doing wrong for this function. I’ve been trying for two days + reading all the similar topics from Discourse. This is the output I always get:

Tip: When you edit a cell, you must re-run it before you can test it.

Done that. The output in the image is my best result. I always get that or worse.

Do you include “stride” when you compute the horizontal and vertical start positions?

1 Like

vert_start and horiz_start contain stride in the calculation and the formula for every *_end contains its relative *_start .

Here’s what I use:
{code removed}

1 Like

I’m using the same formulas for this four variables.

The problem must be somewhere else then.

I’ve been trying for two days straight. I’ve also checked all the previous parts, in case a
I was able to get a perfect score on those parts even though the code had a bug. I didn’t find anything

May I paste the code for pool_forward here? I’m still stuck.

You can see that some of your values are correct, but not all of them. And they work better for the “max” case than for the “average” case. Also the shapes are correct, so that eliminates some classes of errors (e.g. looping over the input space instead of the output space).

Are you sure that you checked the formulas that Tmosh showed for vert_start and horiz_start? E.g. maybe you have transposed them (used w for the vertical instead of the horizontal). You have to be careful that “h” is short for “height” not for “horizontal”. Or maybe you reversed the vertical and horizontal when you do the actual indexing of A_prev.

1 Like

The mistake I made was switching “W” with “H”… “W” is “width” and is, of course, for the horizontal dimension.
Thank you very much!

2 Likes

Hi, I’m also stuck with the same problem, 4 tests pass but 2 fail. I re-checked multiple times that I use h for vertical and w for horizontal, so those are fine. I suspect I’m making a mistake when indexing A_prev. I’m doing A_prev[everything, from vertical to vertical end, from horizontal start to horizontal end, everything].

Could I please get a hint on whether my slicing is correct?

Hi, @Bella.

If you’re specifically talking about the “pooling” case, as opposed the general convolution, note that the pooling happens “per channel”. That means you would be looping over input channels, so the 4th index there on the slicing would not be “everything”. Also are you sure you checked the height and width slicing as Tom showed in an earlier reply on this thread?

Hi @paulinpaloalto ,

Thank you for your reply. Yes, it’s the pooling case where I get stuck. I also tried to replace the last “everything” with c for channels, but the same problem remains. And my vertical/horizontal starts/ends are calculated as mentioned above (and as done in the conv_forward function which works fine).

Please let me know if I can share some of my code (I’ll delete it afterwards) or do I have to send a private message?

I have same issue i checked the notation of h and w and I am using last index =c

I tried everything but i dont know why it passes the 1st testcase and not the second.

The difference between the first test case and the second is the stride, right? That needs to apply to your computation of the vert and horiz positions in the input space. Tmosh showed you the correct formulas earlier on this thread.

Hi, I have issues in the same question (pooling, stride = 2). However, my issue is that the 2nd part of the output doesn’t match the expected output for both max-pool and average-pool:

Interestingly, the values in the 1st row of the 2nd set of the values do not match while the rest are all fine. I have checked and ensured that the same formula as Tmosh provided is used and that the indexing for the A_prev is correct:

{code removed}

and

{code removed}

Indexing:
{code removed}

Thanks!

1 Like

Notice that those are not the only values that don’t match in your results: it is only the first row of the first set and the last row of the last set that matches. The others are wrong. Maybe you have the height and width dimensions reversed in the way you index a_prev_slice. Remember h is not horizontal, it’s height, right?

Thank you for the quick reply! I see. I misunderstood the output.

Despite this, I think my a_prev_slice has correct indexing and the h (vertical) and w (horizontal) parameters are defined. I think that if the indexing for a_prev_slice were wrong, both the stride values would have generated an incorrect result or thrown an IndexError?

The problem in my case is for stride != 1. I added a print statement in the channel for loop to try to debug and this was the output:

A[0,0,0,0] vert: 0:3, horiz: 0:3
A[0,0,0,1] vert: 0:3, horiz: 0:3
A[0,0,0,2] vert: 0:3, horiz: 0:3
A[0,0,1,0] vert: 0:3, horiz: 2:5
A[0,0,1,1] vert: 0:3, horiz: 2:5
A[0,0,1,2] vert: 0:3, horiz: 2:5
A[0,1,0,0] vert: 2:5, horiz: 0:3
A[0,1,0,1] vert: 2:5, horiz: 0:3
A[0,1,0,2] vert: 2:5, horiz: 0:3
A[0,1,1,0] vert: 2:5, horiz: 2:5
A[0,1,1,1] vert: 2:5, horiz: 2:5
A[0,1,1,2] vert: 2:5, horiz: 2:5
A[1,0,0,0] vert: 0:3, horiz: 0:3
A[1,0,0,1] vert: 0:3, horiz: 0:3
A[1,0,0,2] vert: 0:3, horiz: 0:3
A[1,0,1,0] vert: 0:3, horiz: 2:5
A[1,0,1,1] vert: 0:3, horiz: 2:5
A[1,0,1,2] vert: 0:3, horiz: 2:5
A[1,1,0,0] vert: 2:5, horiz: 0:3
A[1,1,0,1] vert: 2:5, horiz: 0:3
A[1,1,0,2] vert: 2:5, horiz: 0:3
A[1,1,1,0] vert: 2:5, horiz: 2:5
A[1,1,1,1] vert: 2:5, horiz: 2:5
A[1,1,1,2] vert: 2:5, horiz: 2:5

However, I still don’t know what is going on :frowning:. Your help is greatly appreciated.

1 Like