Pool_forward test failed!

My error message is below. The way to compute a_prev, vert_start, vert_end, horiz_start and horiz_end, a_prev_slice are the same as those in conv_forward, and np.max/mean simply applies to a_prev_slice. I don’t know why this error occurs.

mode = max
A.shape = (2, 3, 3, 3)
A[1, 1] =
 [[2.10025514 2.10025514 2.10025514]
 [2.10025514 2.10025514 2.10025514]
 [2.10025514 2.10025514 2.10025514]]
mode = average
A.shape = (2, 3, 3, 3)
A[1, 1] =
 [[-0.04193968 -0.04193968 -0.04193968]
 [-0.04193968 -0.04193968 -0.04193968]
 [-0.04193968 -0.04193968 -0.04193968]]
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-31-034ff9e73c4d> in <module>
     13 print("A[1, 1] =\n", A[1, 1])
     14 
---> 15 pool_forward_test(pool_forward)

~/work/release/W1A1/public_tests.py in pool_forward_test(target)
    152                                          [0.31515939, 0.84616065, 0.07734007],
    153                                          [0.69803203, 0.84616065, 1.2245077 ],
--> 154                                          [0.69803203, 1.12141771, 1.2245077 ]])), "Wrong value for A[1, 1]"
    155 
    156     assert np.allclose(cache[0][1, 2], np.array([[ 0.16938243,  0.74055645, -0.9537006 ],

AssertionError: Wrong value for A[1, 1]

The expected output is:
Expected output

mode = max
A.shape = (2, 3, 3, 3)
A[1, 1] =
 [[1.96710175 0.84616065 1.27375593]
 [1.96710175 0.84616065 1.23616403]
 [1.62765075 1.12141771 1.2245077 ]]

mode = average
A.shape = (2, 3, 3, 3)
A[1, 1] =
 [[ 0.44497696 -0.00261695 -0.31040307]
 [ 0.50811474 -0.23493734 -0.23961183]
 [ 0.11872677  0.17255229 -0.22112197]]

One clue is to notice that all the individual content values in each of your A[1,1] values are all the same, whereas that is not true for the “Expected values”. So how could that type of error happen? The most likely thing I can think of is that you are indexing a_slice_prev incorrectly. Somehow the index values aren’t changing.

In the for loop, “for c in range(n_C)”, the a_slice_prev= […], the a_slice_prev doesn’t depend on ‘c’ and that’s why in each iteration of ‘c’, a_slice_prev’s value remains the same. Then because A[i, h, w, c] = np.max(a_slice_prev), some of the values in A must be the same. Is this true?

Yes, you’ll notice in the expected values that some of the values do agree, but definitely not all of them, right? But in your case, literally every one is the same. So how can that happen?

I don’t understand what you mean when you say a_slice_prev doesn’t depend on c: of course it does. But note that unlike conv_forward, pool_forward happens on each input channel one at a time and the output channel equals the input channel. In other words, pooling happens “per channel” on the input side.

I fixed one error and this is the new error message:

mode = max
A.shape = (2, 3, 3, 3)
A[1, 1] =
 [[1.96710175 1.96710175 1.96710175]
 [1.96710175 1.96710175 1.96710175]
 [1.62765075 1.62765075 1.62765075]]
mode = average
A.shape = (2, 3, 3, 3)
A[1, 1] =
 [[0.04398565 0.04398565 0.04398565]
 [0.01118852 0.01118852 0.01118852]
 [0.0233857  0.0233857  0.0233857 ]]
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-17-034ff9e73c4d> in <module>
     13 print("A[1, 1] =\n", A[1, 1])
     14 
---> 15 pool_forward_test(pool_forward)

~/work/release/W1A1/public_tests.py in pool_forward_test(target)
    152                                          [0.31515939, 0.84616065, 0.07734007],
    153                                          [0.69803203, 0.84616065, 1.2245077 ],
--> 154                                          [0.69803203, 1.12141771, 1.2245077 ]])), "Wrong value for A[1, 1]"
    155 
    156     assert np.allclose(cache[0][1, 2], np.array([[ 0.16938243,  0.74055645, -0.9537006 ],

AssertionError: Wrong value for A[1, 1]

It looks better, but now in each row, all the values are the same. so still note quite right. I said that ‘a_slice_prev’ doesn’t depend on ‘c’ because in each iteration, a_slice_prev only depends on a_prev, vert_start, vert_end, horiz_start, horiz_end, that is a_slice_prev = a_prev[vert_start:vert_end,horiz_start:horiz_end, :]. So to me it has nothing to do with ‘c’. ‘c’ only matters when it stores with A[i, h, w, c] = np.max(a_slice_prev).

Regarding ''pooling happens per channel on the input side", since n_C = n_C_prev, so the code is the same as conv_forward. We still need to iterate on n_C. right?

Yes, you iterate over c. The bug is that the slicing you do there is wrong. That is taking all the input channels, but the point is pooling operates one channel at a time. You just copied the code from conv_forward, but that works differently. That was what I was pointing out in my previous comment. It is also described in the lectures.

That’s not what I said: I specifically said that pool_forward works differently than conv_forward, because it operates one input channel at a time and preserves the channels.

@paulinpaloalto I added ‘c’ to the slicing and it worked! I need to review the slides to understand what’s the difference. Thank you.

In forward pooling, the input channels are preserved as output channel, but in forward_conv, the output channel in each layer is a parameter that user specifies when defining the network structure?

Yes, that sounds correct. In a forward convolution layer, the filter size matches the number of channels of the input and each filter creates one output channel. That means that the number of filters you choose to have determines the number of output channels.

In a forward pooling layer, each input channel is processed separately and creates one output channel, so the number of input and output channels is always the same in a pooling layer.