一、最大值池化 MaxPool
1.1 MaxPool1d
nn.MaxPool1d 是一种一维最大池化层,常用于卷积神经网络中对输入张量的空间维度(宽度)进行下采样。该层通过在输入张量上应用一个滑动窗口(也称为池化窗口),并在每个窗口中选择最大值
class torch.nn.MaxPool1d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
对于输入信号的输入通道,提供1维最大池化(max pooling
)操作
如果输入的大小是(N,C,L),那么输出的大小是(N,C,L_out)的计算方式是:
如果padding
不是0,会在输入的每一边添加相应数目0dilation
用于控制内核点之间的距离,详细描述在这里
参数:
- kernel_size(
int
ortuple
) - max pooling的窗口大小 - stride(
int
ortuple
,optional
) - max pooling的窗口移动的步长。默认值是kernel_size
- padding(
int
ortuple
,optional
) - 输入的每一条边补充0的层数 - dilation(
int
ortuple
,optional
) – 一个控制窗口中元素步幅的参数 - return_indices - 如果等于
True
,会返回输出最大值的序号,对于上采样操作会有帮助 - ceil_mode - 如果等于
True
,计算输出信号大小的时候,会使用向上取整,代替默认的向下取整的操作
shape:
输入: (N,C_in,L_in)
输出: (N,C_out,L_out)
>>> # pool of size=3, stride=2
>>> m = nn.MaxPool1d(3, stride=2)
>>> input = autograd.Variable(torch.randn(20, 16, 50))
>>> output = m(input)
1.2 MaxPool2d
nn.MaxPool2d 是二维最大池化层,广泛用于卷积神经网络来进行空间下采样。它通过在输入张量上应用一个二维滑动窗口,并选择每个窗口中的最大值来工作
torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
对于输入信号的输入通道,提供2维最大池化(max pooling
)操作
如果输入的大小是(N,C,H,W),那么输出的大小是(N,C,H_out,W_out)和池化窗口大小(kH,kW)的关系是:
如果padding
不是0,会在输入的每一边添加相应数目0dilation
用于控制内核点之间的距离,详细描述在这里
参数kernel_size
,stride
, padding
,dilation
数据类型: 可以是一个int
类型的数据,此时卷积height和width值相同; 也可以是一个tuple
数组(包含来两个int类型的数据),第一个int
数据表示height的数值,tuple
的第二个int类型的数据表示width的数值
参数:
- kernel_size(
int
ortuple
) - max pooling的窗口大小 - stride(
int
ortuple
,optional
) - max pooling的窗口移动的步长。默认值是kernel_size
- padding(
int
ortuple
,optional
) - 输入的每一条边补充0的层数 - dilation(
int
ortuple
,optional
) – 一个控制窗口中元素步幅的参数 - return_indices - 如果等于
True
,会返回输出最大值的序号,对于上采样操作会有帮助 - ceil_mode - 如果等于
True
,计算输出信号大小的时候,会使用向上取整,代替默认的向下取整的操作
shape:
输入: (N,C,H_{in},W_in)
输出: (N,C,H_out,W_out)
>>> # pool of square window of size=3, stride=2
>>> m = nn.MaxPool2d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.MaxPool2d((3, 2), stride=(2, 1))
>>> input = autograd.Variable(torch.randn(20, 16, 50, 32))
>>> output = m(input)
1.3 MaxPool3d
nn.MaxPool3d 是三维最大池化层,用于在三维数据(如3D图像或视频帧)上进行空间下采样
torch.nn.MaxPool3d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
对于输入信号的输入通道,提供3维最大池化(max pooling)操作
如果输入的大小是(N,C,D,H,W),那么输出的大小是(N,C,D,H_out,W_out)和池化窗口大小(kD,kH,kW)的关系是:
如果padding
不是0,会在输入的每一边添加相应数目0dilation
用于控制内核点之间的距离,详细描述在这里
参数kernel_size
,stride
, padding
,dilation
数据类型: 可以是int
类型的数据,此时卷积height和width值相同; 也可以是一个tuple
数组(包含来两个int
类型的数据),第一个int
数据表示height的数值,tuple
的第二个int
类型的数据表示width的数值
参数:
- kernel_size(
int
ortuple
) - max pooling的窗口大小 - stride(
int
ortuple
,optional
) - max pooling的窗口移动的步长。默认值是kernel_size - padding(
int
ortuple
,optional
) - 输入的每一条边补充0的层数 - dilation(
int
ortuple
,optional
) – 一个控制窗口中元素步幅的参数 - return_indices - 如果等于
True
,会返回输出最大值的序号,对于上采样操作会有帮助 - ceil_mode - 如果等于
True
,计算输出信号大小的时候,会使用向上取整,代替默认的向下取整的操作
shape:
输入: (N,C,H_in,W_in)
输出: (N,C,H_out,W_out)
>>> # pool of square window of size=3, stride=2
>>>m = nn.MaxPool3d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.MaxPool3d((3, 2, 2), stride=(2, 1, 2))
>>> input = autograd.Variable(torch.randn(20, 16, 50,44, 31))
>>> output = m(input)
二、最大值上采样 MaxUnpool
2.1 MaxUnpool1d
torch.nn.MaxUnpool1d(kernel_size, stride=None, padding=0)
Maxpool1d
的逆过程,不过并不是完全的逆过程,因为在maxpool1d
的过程中,一些最大值的已经丢失。 MaxUnpool1d
输入MaxPool1d
的输出,包括最大值的索引,并计算所有maxpool1d
过程中非最大值被设置为零的部分的反向。
注意:MaxPool1d
可以将多个输入大小映射到相同的输出大小。因此,反演过程可能会变得模棱两可。 为了适应这一点,可以在调用中将输出大小(output_size
)作为额外的参数传入。 具体用法,请参阅下面的输入和示例
参数:
- kernel_size(
int
ortuple
) - max pooling的窗口大小 - stride(
int
ortuple
,optional
) - max pooling的窗口移动的步长。默认值是kernel_size
- padding(
int
ortuple
,optional
) - 输入的每一条边补充0的层数
输入:input
:需要转换的tensor
indices
:Maxpool1d的索引号 output_size
:一个指定输出大小的torch.Size
shape:input
: (N,C,H_in)output
:(N,C,H_out)
也可以使用output_size
指定输出的大小
# 创建最大池化和对应的反池化层
pool = nn.MaxPool1d(2, stride=2, return_indices=True)
unpool = nn.MaxUnpool1d(2, stride=2)
# 输入张量
input = torch.tensor([[[1., 2, 3, 4, 5, 6, 7, 8]]])
# 应用池化和反池化
output, indices = pool(input)
unpooled_output = unpool(output, indices)
# 输出: tensor([[[ 0., 2., 0., 4., 0., 6., 0., 8.]]])
# 使用 output_size 的示例
input = torch.tensor([[[1., 2, 3, 4, 5, 6, 7, 8, 9]]])
output, indices = pool(input)
unpooled_output = unpool(output, indices, output_size=input.size())
# 输出: tensor([[[ 0., 2., 0., 4., 0., 6., 0., 8., 0.]]])
2.2 MaxUnpool2d
torch.nn.MaxUnpool2d(kernel_size, stride=None, padding=0)
Maxpool2d
的逆过程,不过并不是完全的逆过程,因为在maxpool2d的过程中,一些最大值的已经丢失。 MaxUnpool2d
的输入是MaxPool2d
的输出,包括最大值的索引,并计算所有maxpool2d
过程中非最大值被设置为零的部分的反向。
注意:MaxPool2d
可以将多个输入大小映射到相同的输出大小。因此,反演过程可能会变得模棱两可。 为了适应这一点,可以在调用中将输出大小(output_size
)作为额外的参数传入。具体用法,请参阅下面示例
参数:
- kernel_size(
int
ortuple
) - max pooling的窗口大小 - stride(
int
ortuple
,optional
) - max pooling的窗口移动的步长。默认值是kernel_size
- padding(
int
ortuple
,optional
) - 输入的每一条边补充0的层数
输入:input
:需要转换的tensor
indices
:Maxpool1d的索引号output_size
:一个指定输出大小的torch.Size
大小:input
: (N,C,H_in,W_in)output
:(N,C,H_out,W_out)
也可以使用output_size
指定输出的大小
>>> pool = nn.MaxPool2d(2, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool2d(2, stride=2)
>>> input = Variable(torch.Tensor([[[[ 1, 2, 3, 4],
... [ 5, 6, 7, 8],
... [ 9, 10, 11, 12],
... [13, 14, 15, 16]]]]))
>>> output, indices = pool(input)
>>> unpool(output, indices)
Variable containing:
(0 ,0 ,.,.) =
0 0 0 0
0 6 0 8
0 0 0 0
0 14 0 16
[torch.FloatTensor of size 1x1x4x4]
>>> # specify a different output size than input size
>>> unpool(output, indices, output_size=torch.Size([1, 1, 5, 5]))
Variable containing:
(0 ,0 ,.,.) =
0 0 0 0 0
6 0 8 0 0
0 0 0 14 0
16 0 0 0 0
0 0 0 0 0
[torch.FloatTensor of size 1x1x5x5]
2.3 MaxUnpool3d
torch.nn.MaxUnpool3d(kernel_size, stride=None, padding=0)
Maxpool3d
的逆过程,不过并不是完全的逆过程,因为在maxpool3d
的过程中,一些最大值的已经丢失。 MaxUnpool3d
的输入就是MaxPool3d
的输出,包括最大值的索引,并计算所有maxpool3d
过程中非最大值被设置为零的部分的反向。
注意:MaxPool3d
可以将多个输入大小映射到相同的输出大小。因此,反演过程可能会变得模棱两可。为了适应这一点,可以在调用中将输出大小(output_size
)作为额外的参数传入。具体用法,请参阅下面的输入和示例
参数:
- kernel_size(
int
ortuple
) - Maxpooling窗口大小 - stride(
int
ortuple
,optional
) - max pooling的窗口移动的步长。默认值是kernel_size
- padding(
int
ortuple
,optional
) - 输入的每一条边补充0的层数
输入:input
:需要转换的tensor
indices
:Maxpool1d
的索引序数output_size
:一个指定输出大小的torch.Size
大小:input
: (N,C,D_in,H_in,W_in)outpu
t:(N,C,D_out,H_out,W_out)
也可以使用output_size
指定输出的大小
>>> # pool of square window of size=3, stride=2
>>> pool = nn.MaxPool3d(3, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool3d(3, stride=2)
>>> output, indices = pool(Variable(torch.randn(20, 16, 51, 33, 15)))
>>> unpooled_output = unpool(output, indices)
>>> unpooled_output.size()
torch.Size([20, 16, 51, 33, 15])
三、均值池化 AvgPool
3.1 AvgPool1d
对信号的输入通道,提供1维平均池化(average pooling ) 输入信号的大小(N,C,L),输出大小(N,C,L_out)和池化窗口大小k的关系是:
如果padding
不是0,会在输入的每一边添加相应数目0
参数:
- kernel_size(
int
ortuple
) - 池化窗口大小 - stride(
int
ortuple
,optional
) - max pooling的窗口移动的步长。默认值是kernel_size
- padding(
int
ortuple
,optional
) - 输入的每一条边补充0的层数 - dilation(
int
ortuple
,optional
) – 一个控制窗口中元素步幅的参数 - return_indices - 如果等于
True
,会返回输出最大值的序号,对于上采样操作会有帮助 - ceil_mode - 如果等于
True
,计算输出信号大小的时候,会使用向上取整,代替默认的向下取整的操作
大小:input
:(N,C,L_in)output
:(N,C,L_out)
>>> # pool with window of size=3, stride=2
>>> m = nn.AvgPool1d(3, stride=2)
>>> m(Variable(torch.Tensor([[[1,2,3,4,5,6,7]]])))
Variable containing:
(0 ,.,.) =
2 4 6
[torch.FloatTensor of size 1x1x3]
3.2 AvgPool2d
对信号的输入通道,提供2维的平均池化(average pooling )
输入信号的大小(N,C,H,W),输出大小(N,C,H_out,W_out)和池化窗口大小(kH,kW)的关系是:
如果padding
不是0,会在输入的每一边添加相应数目0
参数:
- kernel_size(
int
ortuple
) - 池化窗口大小 - stride(
int
ortuple
,optional
) - max pooling的窗口移动的步长。默认值是kernel_size
- padding(
int
ortuple
,optional
) - 输入的每一条边补充0的层数 - dilation(
int
ortuple
,optional
) – 一个控制窗口中元素步幅的参数 - ceil_mode - 如果等于
True
,计算输出信号大小的时候,会使用向上取整,代替默认的向下取整的操作 - count_include_pad - 如果等于
True
,计算平均池化时,将包括padding
填充的0
shape:input
: (N,C,H_in,W_in)output
: (N,C,H_out,W_out)
>>> # pool of square window of size=3, stride=2
>>> m = nn.AvgPool2d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.AvgPool2d((3, 2), stride=(2, 1))
>>> input = autograd.Variable(torch.randn(20, 16, 50, 32))
>>> output = m(input)
3.3 AvgPool3d
对信号的输入通道,提供3维的平均池化(average pooling
) 输入信号的大小(N,C,D,H,W),输出大小(N,C,D_out,H_out,W_out)和池化窗口大小(kD,kH,kW)的关系是:
如果padding
不是0,会在输入的每一边添加相应数目0
参数:
- kernel_size(
int
ortuple
) - 池化窗口大小 - stride(
int
ortuple
,optional
) - maxpooling
的窗口移动的步长。默认值是kernel_size
shape:
输入大小:(N,C,D_in,H_in,W_in)
输出大小:(N,C,D_out,H_out,W_out)
>>> # pool of square window of size=3, and target output size 13x12
>>> m = nn.FractionalMaxPool2d(3, output_size=(13, 12))
>>> # pool of square window and target output size being half of input image size
>>> m = nn.FractionalMaxPool2d(3, output_ratio=(0.5, 0.5))
>>> input = autograd.Variable(torch.randn(20, 16, 50, 32))
>>> output = m(input)
四、 分数最大池化 FractionalMaxPool
4,1 FractionalMaxPool2d
class torch.nn.FractionalMaxPool2d(kernel_size, output_size=None, output_ratio=None, return_indices=False, _random_samples=None)
对输入的信号,提供2维的分数最大化池化操作 分数最大化池化的细节请阅读论文 由目标输出大小确定的随机步长,在$kH*kW$区域进行最大池化操作。输出特征和输入特征的数量相同。
参数:
- kernel_size(
int
ortuple
) - 最大池化操作时的窗口大小。可以是一个数字(表示K*K
的窗口),也可以是一个元组(kh*kw
) - output_size - 输出图像的尺寸。可以使用一个
tuple
指定(oH,oW),也可以使用一个数字oH指定一个oH*oH的输出。 - output_ratio – 将输入图像的大小的百分比指定为输出图片的大小,使用一个范围在(0,1)之间的数字指定
- return_indices - 默认值
False
,如果设置为True
,会返回输出的索引,索引对nn.MaxUnpool2d
有用。
>>> # pool of square window of size=3, and target output size 13x12
>>> m = nn.FractionalMaxPool2d(3, output_size=(13, 12))
>>> # pool of square window and target output size being half of input image size
>>> m = nn.FractionalMaxPool2d(3, output_ratio=(0.5, 0.5))
>>> input = autograd.Variable(torch.randn(20, 16, 50, 32))
>>> output = m(input)
4.2 FractionalMaxPool3d
torch.nn.FractionalMaxPool3d 是 PyTorch 中用于实现三维分数最大池化(Fractional MaxPooling)的类。与二维版本类似,它允许使用随机或非整数的步长来进行池化,从而可以获得不同大小的输出特征图。这种池化操作对于提取三维数据(如体积图像或视频序列)中的特征特别有用。
主要特性:
随机步长:通过目标输出大小确定的随机步长对输入数据进行池化。
灵活的输出尺寸:可以通过指定输出尺寸或输出比例来控制输出特征图的大小。
参数:
kernel_size (int 或 tuple):池化窗口的大小。可以是单个数字 k(表示 k x k x k 的立方窗口)或一个元组 (kt, kh, kw)。
output_size (int 或 tuple):目标输出尺寸,形式为 oT x oH x oW。可以是元组 (oT, oH, oW) 或单个数字 oH(表示立方图像 oH x oH x oH)。
output_ratio (float 或 tuple):如果希望输出尺寸是输入尺寸的比例,则可以使用此选项。必须是 (0, 1) 范围内的数字或元组。
return_indices (bool):如果为 True,则返回最大值的索引和输出。这对于后续使用 nn.MaxUnpool3d() 很有用。默认为 False。
# 立方窗口大小为3,目标输出尺寸为 13x12x11
m = nn.FractionalMaxPool3d(3, output_size=(13, 12, 11))
# 输入张量
input = torch.randn(20, 16, 50, 32, 16)
# 应用分数最大池化
output = m(input)
# 立方窗口大小为3,目标输出尺寸为输入尺寸的一半
m = nn.FractionalMaxPool3d(3, output_ratio=(0.5, 0.5, 0.5))
# 输入张量
input = torch.randn(20, 16, 50, 32, 16)
# 应用分数最大池化
output = m(input)
五、幂平均池化 LPPool
对输入信号提供幂平均池化操作。 输出的计算方式:
当p为无穷大的时候时,等价于最大池化操作
当p=1时,等价于平均池化操作。
torch.nn.LPPool1d(norm_type, kernel_size, stride=None, ceil_mode=False)
torch.nn.LPPool2d(norm_type, kernel_size, stride=None, ceil_mode=False)
参数:
kernel_size: 池化窗口的大小
stride:池化窗口移动的步长。kernel_size是默认值
ceil_mode: ceil_mode=True时,将使用向下取整代替向上取整
输入输出尺寸:
>>> # power-2 pool of square window of size=3, stride=2
>>> m = nn.LPPool2d(2, 3, stride=2)
>>> # pool of non-square window of power 1.2
>>> m = nn.LPPool2d(1.2, (3, 2), stride=(2, 1))
>>> input = autograd.Variable(torch.randn(20, 16, 50, 32))
>>> output = m(input)
六、自适应最大池化AdaptiveMaxPool
对输入信号,提供自适应最大池化操作 对于任何输入大小的输入,可以将输出尺寸指定为H*W,但是输入和输出特征的数目不会变化。
torch.nn.AdaptiveMaxPool1d(output_size, return_indices=False)
对输入信号,提供1维的自适应最大池化操作 对于任何输入大小的输入,可以将输出尺寸指定为H,但是输入和输出特征的数目不会变化。
参数:
- output_size: 输出信号的尺寸
- return_indices: 如果设置为
True
,会返回输出的索引。对nn.MaxUnpool1d
有用,默认值是False
>>> # target output size of 5
>>> m = nn.AdaptiveMaxPool1d(5)
>>> input = autograd.Variable(torch.randn(1, 64, 8))
>>> output = m(input)
torch.nn.AdaptiveMaxPool2d(output_size, return_indices=False)
对输入信号,提供2维的自适应最大池化操作 对于任何输入大小的输入,可以将输出尺寸指定为H*W,但是输入和输出特征的数目不会变化。
参数:
- output_size: 输出信号的尺寸,可以用(H,W)表示
H*W
的输出,也可以使用数字H
表示H*H
大小的输出 - return_indices: 如果设置为
True
,会返回输出的索引。对nn.MaxUnpool2d
有用,默认值是False
>>> # target output size of 5x7
>>> m = nn.AdaptiveMaxPool2d((5,7))
>>> input = autograd.Variable(torch.randn(1, 64, 8, 9))
>>> # target output size of 7x7 (square)
>>> m = nn.AdaptiveMaxPool2d(7)
>>> input = autograd.Variable(torch.randn(1, 64, 10, 9))
>>> output = m(input)
torch.nn.AdaptiveMaxPool3d(output_size, return_indices=False)
八、自适应平均池化AdaptiveAvgPool
对输入信号,提供自适应平均池化操作 对于任何输入大小的输入,可以将输出尺寸指定为H*W,但是输入和输出特征的数目不会变化。
torch.nn.AdaptiveAvgPool1d(output_size)
对输入信号,提供1维的自适应平均池化操作 对于任何输入大小的输入,可以将输出尺寸指定为H*W,但是输入和输出特征的数目不会变化。
参数:
- output_size: 输出信号的尺寸
>>> # target output size of 5
>>> m = nn.AdaptiveAvgPool1d(5)
>>> input = autograd.Variable(torch.randn(1, 64, 8))
>>> output = m(input)
torch.nn.AdaptiveAvgPool2d(output_size)
参数:
- 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)
torch.nn.AdaptiveAvgPool3d(output_size)
参数:
output_size: 输出信号的尺寸