当前位置:首页 » python教程 » 正文

pytorch中F.avg_pool1d()和F.avg_pool2d()的使用操作

看: 676次  时间:2021-06-15  分类 : python教程

F.avg_pool1d()数据是三维输入

input维度: (batch_size,channels,width)channel可以看成高度

kenerl维度:(一维:表示width的跨度)channel和输入的channel一致可以认为是矩阵的高度

假设kernel_size=2,则每俩列相加求平均,stride默认和kernel_size保持一致,越界则丢弃(下面表示1,2列和3,4列相加求平均)

input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float()
print(input)
m = F.avg_pool1d(input,kernel_size=2)
m

tensor([[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [0., 0., 0., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]])
tensor([[[1.0000, 1.0000],
         [1.0000, 1.0000],
         [0.0000, 0.5000],
         [1.0000, 1.0000],
         [1.0000, 1.0000]]])

假设kenerl_size=3,表示前3列相加求平均,后面的不足3列丢弃

input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float()
print(input)
m = F.avg_pool1d(input,kernel_size=3)
m
tensor([[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [0., 0., 0., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]])
tensor([[[1.],
         [1.],
         [0.],
         [1.],
         [1.]]])

input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float()
print(input)
m = F.avg_pool1d(input,kernel_size=4)
m
tensor([[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [0., 0., 0., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]])
tensor([[[1.0000],
         [1.0000],
         [0.2500],
         [1.0000],
         [1.0000]]])

假设stride=1每次移动一个步伐

input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float()
print(input)
m = F.avg_pool1d(input,kernel_size=2,stride=1)
m
tensor([[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [0., 0., 0., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]])
tensor([[[1.0000, 1.0000, 1.0000, 1.0000],
         [1.0000, 1.0000, 1.0000, 1.0000],
         [0.0000, 0.0000, 0.5000, 1.0000],
         [1.0000, 1.0000, 1.0000, 1.0000],
         [1.0000, 1.0000, 1.0000, 1.0000]]])

input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float()
print(input)
m = F.avg_pool1d(input,kernel_size=4,stride=1)
m
tensor([[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [0., 0., 0., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]])
tensor([[[1.0000, 1.0000],
         [1.0000, 1.0000],
         [0.2500, 0.5000],
         [1.0000, 1.0000],
         [1.0000, 1.0000]]])

F.avg_pool2d()数据是四维输入

input维度: (batch_size,channels,height,width)

kenerl维度:(二维:表示width的跨度)channel和输入的channle一致,如果数据是三维,则channel为1.(如果只写一个数n,kenerl=(n,n))

stride默认和kenerl一致,这是个二维的,所以在height和width上均和kenerl一致,越界同样丢弃。

跟cnn卷积一致

input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float()
print(input.size())
print(input)
m = F.avg_pool2d(input,kernel_size=(4,4))
m
torch.Size([1, 5, 5])
tensor([[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [0., 0., 0., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]])
tensor([[[0.8125]]])

input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float()
print(input.size())
print(input)
m = F.avg_pool2d(input,kernel_size=(4,4),stride=1)
m
torch.Size([1, 5, 5])
tensor([[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [0., 0., 0., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]])
tensor([[[0.8125, 0.8750],
         [0.8125, 0.8750]]])

如果求列的平均kenerl=(1,5),此时默认stride=(1,5)

input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float()
print(input.size())
print(input)
m = F.avg_pool2d(input,kernel_size=(1,5))
m
torch.Size([1, 5, 5])
tensor([[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [0., 0., 0., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]])
tensor([[[1.0000],
         [1.0000],
         [0.4000],
         [1.0000],
         [1.0000]]])

如果求行的平均kenerl=(5,1),此时默认stride=(5,1),用卷积的概念取思考

input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float()
print(input.size())
print(input)
m = F.avg_pool2d(input,kernel_size=(5,1))
m
torch.Size([1, 5, 5])
tensor([[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [0., 0., 0., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]])
tensor([[[0.8000, 0.8000, 0.8000, 1.0000, 1.0000]]])

对于四维的数据,channel默认和输入一致

input=torch.randn(10,3,4,4)
m=F.avg_pool2d(input,(4,4))
print(m.size())
torch.Size([10, 3, 1, 1])

补充:PyTorch中AdaptiveAvgPool函数解析

自适应池化(AdaptiveAvgPool1d):

对输入信号,提供1维的自适应平均池化操作 对于任何输入大小的输入,可以将输出尺寸指定为H*W,但是输入和输出特征的数目不会变化。

torch.nn.AdaptiveAvgPool1d(output_size)
#output_size:输出尺寸

对输入信号,提供1维的自适应平均池化操作 对于任何输入大小的输入,可以将输出尺寸指定为H*W,但是输入和输出特征的数目不会变化。

# target output size of 5
m = nn.AdaptiveAvgPool1d(5)
input = autograd.Variable(torch.randn(1, 64, 8))
output = m(input)

自适应池化(AdaptiveAvgPool2d):

class torch.nn.AdaptiveAvgPool2d(output_size)

对输入信号,提供2维的自适应平均池化操作 对于任何输入大小的输入,可以将输出尺寸指定为H*W,但是输入和输出特征的数目不会变化。

参数:

output_size: 输出信号的尺寸,可以用(H,W)表示H*W的输出,也可以使用耽搁数字H表示H*H大小的输出

# target output size of 5x7
m = nn.AdaptiveAvgPool2d((5,7))
input = autograd.Variable(torch.randn(1, 64, 8, 9))
# target output size of 7x7 (square)
m = nn.AdaptiveAvgPool2d(7)
input = autograd.Variable(torch.randn(1, 64, 10, 9))
output = m(input)

自适应池化的数学解释:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持python博客。

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!