Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PyTorch's "MaxPool2D", is padding added depending on "ceil_mode"?

In MaxPool2D the padding is by default set to 0 and the ceil_mode is also set to False. Now, if I have an input of size 7x7 with kernel=2,stride=2 the output shape becomes 3x3, but when I use ceil_mode=True, it becomes 4x4, which makes sense because (if the following formula is correct), for 7x7 with output_shape would be 3.5x3.5 and depending on the ceil_mode it would be either 3x3 or 4x4.

Now, my question is, if the ceil_mode=True, does it change the default padding?

If it does, then how is it adding the padding i.e. is it adding the padding on left first or right, up first or down?

like image 426
paul-shuvo Avatar asked Jan 25 '20 05:01

paul-shuvo


People also ask

What is ceil mode?

Ceil_mode=True changes the padding. In the case of ceil mode, additional columns and rows are added at the right as well as at the down. (Not top and not left). It does not need to be one extra column. It depends on the stride value as well.

What does NN maxpool2d do?

Applies a 2D max pooling over an input signal composed of several input planes. If padding is non-zero, then the input is implicitly padded with negative infinity on both sides for padding number of points. dilation controls the spacing between the kernel points.


1 Answers

Ceil_mode=True changes the padding.

In the case of ceil mode, additional columns and rows are added at the right as well as at the down. (Not top and not left). It does not need to be one extra column. It depends on the stride value as well. I just wrote small code snippet where you can check how the populated values are pooled in either modes.

Before I found the post referenced above, I experimented the same way with your problem, it also seems as though the zero-padding is not used during the pooling operation, as in my following example the zeros would have been the maximum elements to be taken, but this does not seem to be the case.

    test_tensor = torch.FloatTensor(2,7,7).random_(-10,-5)
    print(test_tensor)
    max_pool = nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)
    print(max_pool(test_tensor))
    max_pool = nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=False)
    print(max_pool(test_tensor))

Random sample tensor:

    tensor([[[ -6.,  -9.,  -7., -10.,  -6.,  -8.,  -6.],
             [-10., -10., -10.,  -6., -10.,  -9.,  -6.],
             [-10.,  -7.,  -7.,  -8., -10., -10.,  -9.],
             [ -8., -10., -10.,  -9.,  -9., -10.,  -9.],
             [ -8.,  -6.,  -8.,  -6.,  -7.,  -7.,  -9.],
             [-10.,  -8.,  -7., -10.,  -9.,  -6.,  -8.],
             [-10.,  -6.,  -9., -10.,  -9.,  -9., -10.]],

            [[-10.,  -8.,  -6., -10.,  -9.,  -6.,  -7.],
             [ -7.,  -7., -10., -10.,  -6.,  -9.,  -7.],
             [ -6., -10.,  -7.,  -8.,  -8., -10.,  -9.],
             [ -8.,  -8.,  -6.,  -7.,  -6.,  -8.,  -6.],
             [ -9.,  -8.,  -7., -10.,  -8.,  -8.,  -7.],
             [-10., -10.,  -6.,  -9.,  -8.,  -8.,  -8.],
             [-10.,  -6.,  -9.,  -9.,  -7.,  -9., -10.]]])

ceil_mode=True


    tensor([[[ -6.,  -6.,  -6.,  -6.],
             [ -7.,  -7.,  -9.,  -9.],
             [ -6.,  -6.,  -6.,  -8.],
             [ -6.,  -9.,  -9., -10.]],

            [[ -7.,  -6.,  -6.,  -7.],
             [ -6.,  -6.,  -6.,  -6.],
             [ -8.,  -6.,  -8.,  -7.],
             [ -6.,  -9.,  -7., -10.]]])

ceil_mode=False

    tensor([[[-6., -6., -6.],
             [-7., -7., -9.],
             [-6., -6., -6.]],

            [[-7., -6., -6.],
             [-6., -6., -6.],
             [-8., -6., -8.]]])

like image 130
a-doering Avatar answered Oct 04 '22 16:10

a-doering