DEV Community

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

Posted on • Updated on

AvgPool3d in PyTorch

Buy Me a Coffee

*Memos:

AvgPool3d() can get the 4D or 5D tensor of the one or more elements computed by 3D average pooling from the 4D or 5D 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 ceil_mode(Optional-Default:False-Type:bool).
  • The 5th argument for initialization is count_include_pad(Optional-Default:True-Type:bool).
  • The 6th argument for initialization is divisor_override(Optional-Default:None-Type:int).
  • The 1st argument is input(Required-Type:tensor of int or float): *Memos:
    • It must be the 4D or 5D tensor of one or more elements.
    • The tensor's requires_grad which is False by default is not set to True by AvgPool3d().


import torch
from torch import nn

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

tensor1.requires_grad
# False

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

tensor2.requires_grad
# False

avgpool3d
# AvgPool3d(kernel_size=1, stride=1, padding=0)

avgpool3d.kernel_size
# 1

avgpool3d.stride
# 1

avgpool3d.padding
# 0

avgpool3d.ceil_mode
# False

avgpool3d.count_include_pad
# True

avgpool3d.divisor_override
# None

avgpool3d = nn.AvgPool3d(kernel_size=1, stride=None, padding=0, 
                         ceil_mode=False, count_include_pad=True, 
                         divisor_override=None)
avgpool3d(input=tensor1)
# tensor([[[[8., -3., 0., 1., 5., -2.]]]])

avgpool3d = nn.AvgPool3d(kernel_size=1, divisor_override=2)
avgpool3d(input=tensor1)
# tensor([[[[4.0000, -1.5000, 0.0000, 0.5000, 2.5000, -1.0000]]]])

my_tensor = torch.tensor([[[[8., -3., 0.],
                            [1., 5., -2.]]]])
avgpool3d = nn.AvgPool3d(kernel_size=1)
avgpool3d(input=my_tensor)
# tensor([[[[8., -3., 0.],
#           [1., 5., -2.]]]])

avgpool3d = nn.AvgPool3d(kernel_size=1, divisor_override=2)
avgpool3d(input=my_tensor)
# tensor([[[[4.0000, -1.5000, 0.0000],
#           [0.5000, 2.5000, -1.0000]]]])

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

avgpool3d = nn.AvgPool3d(kernel_size=1)
avgpool3d(input=my_tensor)
# tensor([[[[8.], [-3.], [0.], [1.], [5.], [-2.]]]])

avgpool3d = nn.AvgPool3d(kernel_size=1, divisor_override=2)
avgpool3d(input=my_tensor)
# tensor([[[[4.0000], [-1.5000], [0.0000], [0.5000], [2.5000], [-1.0000]]]])

my_tensor = torch.tensor([[[[[8.], [-3.], [0.]],
                            [[1.], [5.], [-2.]]]]])
avgpool3d = nn.AvgPool3d(kernel_size=1)
avgpool3d(input=my_tensor)
# tensor([[[[[8.], [-3.], [0.]],
#           [[1.], [5.], [-2.]]]]])

avgpool3d = nn.AvgPool3d(kernel_size=1, divisor_override=2)
avgpool3d(input=my_tensor)
# tensor([[[[[4.0000], [-1.5000], [0.0000]],
#           [[0.5000], [2.5000], [-1.0000]]]]])

my_tensor = torch.tensor([[[[[8], [-3], [0]],
                            [[1], [5], [-2]]]]])
avgpool3d = nn.AvgPool3d(kernel_size=1)
avgpool3d(input=my_tensor)
# tensor([[[[[8], [-3], [0]],
#           [[1], [5], [-2]]]]])


Enter fullscreen mode Exit fullscreen mode

Top comments (0)