【pytorch官方文档】:https://pytorch.org/docs/stable/generated/torch.nn.AvgPool2d.html?highlight=avgpool2d#torch.nn.AvgPool2d
torch.nn.AvgPool2d()
作用
在由多通道组成的输入特征中进行2D平均池化计算
函数
torch.nn.AvgPool2d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True, divisor_override=None)
参数
Args:
kernel_size: 滑窗(池化核)大小
stride: 滑窗的移动步长, 默认值为kernel_size
padding: 在输入信号两侧的隐式零填充数量
ceil_mode: 决定计算输出的形状时是向上取整还是向下取整, 默认为False(向下取整)
count_include_pad: 在平均池化计算中是否包含零填充, 默认为True(包含零填充)
divisor_override: 如果指定了, 它将被作为平均池化计算中的除数, 否则将使用池化区域的大小作为平均池化计算的除数
公式
代码实例
假设输入特征为S,输出特征为D
情况一
ceil_mode=False, count_include_pad=True(计算时包含零填充)
import torch
import torch.nn as nn
import numpy as np
# 生成一个形状为1*1*3*3的张量
x1 = np.array([
[1,2,3],
[4,5,6],
[7,8,9]
])
x1 = torch.from_numpy(x1).float()
x1 = x1.unsqueeze(0).unsqueeze(0)
# 实例化二维平均池化
avgpool1 = nn.AvgPool2d(kernel_size=3, stride=2, padding=1, ceil_mode=False, count_include_pad=True)
y1 = avgpool1(x1)
print(y1)
# 打印结果
'''
tensor([[[[1.3333, 1.7778],
[2.6667, 3.1111]]]])
'''
计算过程:
输出形状= floor[(3 - 3 + 2) / 2] + 1 = 2,
D[1,1] = (0+0+0+0+1+2+0+4+5) / 9 = 1.3333,
D[1,2] = (0+0+0+2+3+0+5+6+0) / 9 = 1.7778,
D[2,1] = (0+4+5+0+7+8+0+0+0) / 9 = 2.6667,
D[2,2] = (5+6+0+8+9+0+0+0+0) / 9 = 3.1111.
情况二
ceil_mode=False, count_include_pad=False(计算时不包含零填充)
avgpool2 = nn.AvgPool2d(kernel_size=3, stride=2, padding=1, ceil_mode=False, count_include_pad=False)
y2 = avgpool2(x1)
print(y2)
# 打印结果
'''
tensor([[[[3., 4.],
[6., 7.]]]])
'''
计算过程:
输出形状= floor[(3 - 3 + 2) / 2] + 1 = 2,
D[1,1] = (1+2+4+5) / 4 = 3,
D[1,2] = (2+3+5+6) / 4 = 4,
D[2,1] = (4+5+7+8) / 4 = 6,
D[2,2] = (5+6+8+9) / 4 = 7.
情况三
ceil_mode=False, count_include_pad=False, divisor_override=2(将计算平均池化时的除数指定为2)
avgpool3 = nn.AvgPool2d(kernel_size=3, stride=2, padding=1, ceil_mode=False, count_include_pad=False, divisor_override=2)
y3 = avgpool3(x1)
print(y3)
# 打印结果
'''
tensor([[[[ 6., 8.],
[12., 14.]]]])
'''
计算过程:
输出形状= floor[(3 - 3 + 2) / 2] + 1 = 2,
D[1,1] = (1+2+4+5) / 2 = 6,
D[1,2] = (2+3+5+6) / 2 = 8,
D[2,1] = (4+5+7+8) / 2 = 12,
D[2,2] = (5+6+8+9) / 2 = 14.
情况四
ceil_mode=True, count_include_pad=True, divisor_override=None(在计算输出的形状时向上取整)
x2 = np.array([
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]
])
x2 = torch.from_numpy(x2).reshape(1,1,4,4).float()
avgpool4 = nn.AvgPool2d(kernel_size=3, stride=2, padding=1, ceil_mode=True)
y4 = avgpool4(x2)
print(y4)
# 打印结果
'''
tensor([[[[ 1.5556, 3.3333, 2.0000],
[ 6.3333, 11.0000, 6.0000],
[ 4.5000, 7.5000, 4.0000]]]])
'''
计算过程:
输出形状 = ceil[(4 - 3 + 2) / 2] + 1 = 3,
D[1,1] = (0+0+0+0+1+2+0+5+6) / 9 = 1.5556,
D[1,2] = (0+0+0+2+3+4+6+7+8) / 9 = 3.3333,
D[1,3] = (0+0+4+0+8+0) / 6 = 2,
D[2,1] = (0+5+6+0+9+10+0+13+14) / 9 = 6.3333,
D[2,2] = (6+7+8+10+11+12+14+15+16) / 9 = 11,
D[2,3] = (8+0+12+0+16+0) / 6 = 6,
D[3,1] = (0+13+14+0+0+0) / 6 = 4.5,
D[3,2] = (14+15+16+0+0+0) / 6 = 7.5,文章来源:https://www.toymoban.com/news/detail-418522.html
D[3,3] = (16+0+0+0) / 4 = 4.文章来源地址https://www.toymoban.com/news/detail-418522.html
到了这里,关于Pytorch平均池化nn.AvgPool2d()使用记录的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!