AvgPool1d() in PyTorch

Buy Me a Coffee☕

*Memos:

My post explains Pooling Layer.

My post explains MaxPool1d().

My post explains MaxPool2d().

My post explains MaxPool3d().

My post explains requires_grad.

AvgPool1d() can get the 2D or 3D tensor of the one or more valu…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee

*Memos:

AvgPool1d() can get the 2D or 3D tensor of the one or more values computed by 1D average pooling from the 2D or 3D 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 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 1st argument is input(Required-Type:tensor of int or float).
  • The tensor's requires_grad which is False by default is not set to True by AvgPool1d().
import torch
from torch import nn

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

tensor1.requires_grad
# False

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

tensor2.requires_grad
# False

avgpool1d
# AvgPool1d(kernel_size=(1,), stride=(1,), padding=(0,))

avgpool1d.kernel_size
# (1,)

avgpool1d.stride
# (1,)

avgpool1d.padding
# (0,)

avgpool1d.ceil_mode
# False

avgpool1d.count_include_pad
# True

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

avgpool1d = nn.AvgPool1d(kernel_size=2)
avgpool1d(input=tensor1)
# tensor([[2.5000, 0.5000, 1.5000]])

avgpool1d = nn.AvgPool1d(kernel_size=3)
avgpool1d(input=tensor1)
# tensor([[1.6667, 1.3333]])

avgpool1d = nn.AvgPool1d(kernel_size=4)
avgpool1d(input=tensor1)
# tensor([[1.5000]])

avgpool1d = nn.AvgPool1d(kernel_size=5)
avgpool1d(input=tensor1)
# tensor([[2.2000]])

avgpool1d = nn.AvgPool1d(kernel_size=6)
avgpool1d(input=tensor1)
# tensor([[1.5000]])

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

avgpool1d = nn.AvgPool1d(kernel_size=2)
avgpool1d(input=my_tensor)
# tensor([[2.5000],
#         [3.0000]])

avgpool1d = nn.AvgPool1d(kernel_size=3)
avgpool1d(input=my_tensor)
# tensor([[1.6667],
#         [1.3333]])

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

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

avgpool1d = nn.AvgPool1d(kernel_size=2, padding=1)
avgpool1d(input=my_tensor)
# tensor([[4.0000], [-1.5000], [0.0000], [0.5000], [2.5000], [-1.0000]])

avgpool1d = nn.AvgPool1d(kernel_size=3, padding=1)
avgpool1d(input=my_tensor)
# tensor([[2.6667], [-1.0000], [0.0000], [0.3333], [1.6667], [-0.6667]])

avgpool1d = nn.AvgPool1d(kernel_size=4, padding=2)
avgpool1d(input=my_tensor)
# tensor([[2.0000], [-0.7500], [0.0000], [0.2500], [1.2500], [-0.5000]])

avgpool1d = nn.AvgPool1d(kernel_size=5, padding=2)
avgpool1d(input=my_tensor)
# tensor([[1.6000], [-0.6000], [0.0000], [0.2000], [1.0000], [-0.4000]])
etc.

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

avgpool1d = nn.AvgPool1d(kernel_size=2, padding=1)
avgpool1d(input=my_tensor)
# tensor([[[4.0000], [-1.5000], [0.0000]],
#         [[0.5000], [2.5000], [-1.0000]]])

avgpool1d = nn.AvgPool1d(kernel_size=3, padding=1)
avgpool1d(input=my_tensor)
# tensor([[[2.6667], [-1.0000], [0.0000]],
#         [[0.3333], [1.6667], [-0.6667]]])

avgpool1d = nn.AvgPool1d(kernel_size=4, padding=2)
avgpool1d(input=my_tensor)
# tensor([[[2.0000], [-0.7500], [0.0000]],
#         [[0.2500], [1.2500], [-0.5000]]])

avgpool1d = nn.AvgPool1d(kernel_size=5, padding=2)
avgpool1d(input=my_tensor)
# tensor([[[1.6000], [-0.6000], [0.0000]],
#         [[0.2000], [1.0000], [-0.4000]]])
etc.

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


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-09-15T03:59:08+00:00) AvgPool1d() in PyTorch. Retrieved from https://www.scien.cx/2024/09/15/avgpool1d-in-pytorch/

MLA
" » AvgPool1d() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Sunday September 15, 2024, https://www.scien.cx/2024/09/15/avgpool1d-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Sunday September 15, 2024 » AvgPool1d() in PyTorch., viewed ,<https://www.scien.cx/2024/09/15/avgpool1d-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » AvgPool1d() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/15/avgpool1d-in-pytorch/
CHICAGO
" » AvgPool1d() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/09/15/avgpool1d-in-pytorch/
IEEE
" » AvgPool1d() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/09/15/avgpool1d-in-pytorch/. [Accessed: ]
rf:citation
» AvgPool1d() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/09/15/avgpool1d-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.