DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

MaxPool2d in PyTorch

Buy Me a Coffee

*Memos:

MaxPool2d() can get the 3D or 4D tensor of the one or more elements computed by 2D max pooling from the 3D or 4D tensor of one or more elements as shown below:

*Memos:

  • The 1st argument for initialization is kernel_size(Required-Type:int or tuple or list of int). *It must be 1 <= x.
  • The 2nd argument for initialization is stride(Optional-Default:None-Type:int or tuple or list of int): *Memos:
    • It must be 1 <= x.
    • If it's None, kernel_size is set.
  • The 3rd argument for initialization is padding(Optional-Default:0-Type:int or tuple or list of int). *It must be 0 <= x.
  • The 4th argument for initialization is dilation(Optional-Default:1-Type:int or tuple or list of int). *It must be 1 <= x.
  • The 5th argument for initialization is return_indices(Optional-Default:False-Type:bool).
  • The 6th argument for initialization is ceil_mode(Optional-Default:False-Type:bool).
  • The 1st argument is input(Required-Type:tensor of float): *Memos:
    • It must be the 3D or 4D tensor of one or more elements.
    • The tensor's requires_grad which is False by default is not set to True by MaxPool2d().


import torch
from torch import nn

tensor1 = torch.tensor([[[8., -3., 0., 1., 5., -2.]]])

tensor1.requires_grad
# False

maxpool2d = nn.MaxPool2d(kernel_size=1)
tensor2 = maxpool2d(input=tensor1)
tensor2
# tensor([[[8., -3., 0., 1., 5., -2.]]])

tensor2.requires_grad
# False

maxpool2d
# MaxPool2d(kernel_size=1, stride=1, padding=0, dilation=1, ceil_mode=False)

maxpool2d.kernel_size
# 1

maxpool2d.stride
# 1

maxpool2d.padding
# 0

maxpool2d.dilation
# 1

maxpool2d.return_indices
# False

maxpool2d.ceil_mode
# False

maxpool2d = nn.MaxPool2d(kernel_size=1, stride=None, padding=0, 
                         dilation=1, return_indices=False, ceil_mode=False)
maxpool2d(input=tensor1)
# tensor([[[8., -3., 0., 1., 5., -2.]]])

maxpool2d = nn.MaxPool2d(kernel_size=2, padding=1, return_indices=True)
maxpool2d(input=tensor1)
# (tensor([[[8., 0., 5., -2.]]]), tensor([[[0, 2, 4, 5]]]))

maxpool2d = nn.MaxPool2d(kernel_size=3, padding=1, return_indices=True)
maxpool2d(input=tensor1)
# (tensor([[[8., 5.]]]), tensor([[[0, 4]]]))

maxpool2d = nn.MaxPool2d(kernel_size=4, padding=2, return_indices=True)
maxpool2d(input=tensor1)
# (tensor([[[8., 5.]]]), tensor([[[0, 4]]]))
etc.

maxpool2d = nn.MaxPool2d(kernel_size=7, padding=3, return_indices=True)
maxpool2d(input=tensor1)
# (tensor([[[8.]]]), tensor([[[0]]]))

maxpool2d = nn.MaxPool2d(kernel_size=8, padding=4, return_indices=True)
maxpool2d(input=tensor1)
# (tensor([[[8.]]]), tensor([[[0]]]))
etc.

my_tensor = torch.tensor([[[8., -3., 0.],
                           [1., 5., -2.]]])
maxpool2d = nn.MaxPool2d(kernel_size=1, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[8., -3., 0.],
#           [1., 5., -2.]]]),
#  tensor([[[0, 1, 2],
#           [3, 4, 5]]]))

maxpool2d = nn.MaxPool2d(kernel_size=2, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[8.]]]),
#  tensor([[[0]]]))

my_tensor = torch.tensor([[[8.], [-3.], [0.], [1.], [5.], [-2.]]])

maxpool2d = nn.MaxPool2d(kernel_size=1, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[8.], [-3.], [0.], [1.], [5.], [-2.]]]),
#  tensor([[[0], [1], [2], [3], [4], [5]]]))

maxpool2d = nn.MaxPool2d(kernel_size=2, padding=1, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[8.], [0.], [5.], [-2.]]]),
#  tensor([[[0], [2], [4], [5]]]))

maxpool2d = nn.MaxPool2d(kernel_size=3, padding=1, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[8.], [5.]]]),
#  tensor([[[0], [4]]]))

maxpool2d = nn.MaxPool2d(kernel_size=4, padding=2, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[8.], [5.]]]),
#  tensor([[[0], [4]]]))
etc.

maxpool2d = nn.MaxPool2d(kernel_size=7, padding=3, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[8.]]]), tensor([[[0]]]))

maxpool2d = nn.MaxPool2d(kernel_size=8, padding=4, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[8.]]]), tensor([[[0]]]))
etc.

my_tensor = torch.tensor([[[[8.], [-3.], [0.]],
                           [[1.], [5.], [-2.]]]])
maxpool2d = nn.MaxPool2d(kernel_size=1, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[[8.], [-3.], [0.]],
#           [[1.], [5.], [-2.]]]]),
#  tensor([[[[0], [1], [2]],
#           [[0], [1], [2]]]]))

maxpool2d = nn.MaxPool2d(kernel_size=2, padding=1, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[[8.], [0.]],
#           [[1.], [5.]]]]),
#  tensor([[[[0], [2]],
#           [[0], [1]]]]))

maxpool2d = nn.MaxPool2d(kernel_size=3, padding=1, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[[8.]], [[5.]]]]),
#  tensor([[[[0]], [[1]]]]))

maxpool2d = nn.MaxPool2d(kernel_size=4, padding=2, return_indices=True)
maxpool2d(input=my_tensor)
# (tensor([[[[8.]], [[5.]]]]),
#  tensor([[[[0]], [[1]]]]))
etc.


Enter fullscreen mode Exit fullscreen mode

Top comments (0)