小白学Pytorch系列–Torch.nn API (3)
方法 | 注释 |
nn.MaxPool1d | 对由多个输入平面组成的输入信号应用1D最大池化。 |
nn.MaxPool2d | 对由多个输入平面组成的输入信号应用二维最大池化。 |
nn.MaxPool3d | 在由多个输入平面组成的输入信号上应用3D最大池化。 |
nn.MaxUnpool1d | 计算MaxPool1d的偏逆。 |
nn.MaxUnpool2d | 计算MaxPool2d的偏逆。 |
nn.MaxUnpool3d | 计算MaxPool3d的偏逆。 |
nn.AvgPool1d | 对由多个输入平面组成的输入信号应用1D平均池化。 |
nn.AvgPool2d | 在由多个输入平面组成的输入信号上应用2D平均池。 |
nn.AvgPool3d | 在由多个输入平面组成的输入信号上应用3D平均池。 |
nn.FractionalMaxPool2d | 在由多个输入平面组成的输入信号上应用2D分数极大池。 |
nn.FractionalMaxPool3d | 在由多个输入平面组成的输入信号上应用3D平均池。 |
nn.LPPool1d | 在由多个输入平面组成的输入信号上应用1D功率平均池。 |
nn.LPPool2d | 在由多个输入平面组成的输入信号上应用2D功率平均池。 |
nn.AdaptiveMaxPool1d | 对由多个输入平面组成的输入信号应用1D自适应最大池化。 |
nn.AdaptiveMaxPool2d | 在由多个输入平面组成的输入信号上应用二维自适应最大池化。 |
nn.AdaptiveMaxPool3d | 在由多个输入平面组成的输入信号上应用3D自适应最大池化。 |
nn.AdaptiveAvgPool1d | 在由多个输入平面组成的输入信号上应用1D自适应平均池。 |
nn.AdaptiveAvgPool2d | 在由多个输入平面组成的输入信号上应用2D自适应平均池。 |
nn.AdaptiveAvgPool3d | 在由多个输入平面组成的输入信号上应用3D自适应平均池。 |
nn.MaxPool1d
原文链接:
对于1维池化,池化维度可看作一行长度为的躺平的一维数组。
首先,把input的第二维度画出来,就是一个躺平的一维数组,元素值从1到10,由下图所示。
具体池化操作总共计算了4次:从左到右,构建一个长度为3的窗口,首先cover的三个元素是1、2、3,得到一个最大值为3。
然后向右移动窗口,移动2步,也就是stride=2(从起始位置1移动到3),cover三个元素变成了3、4、5,得到一个最大值为5。
再次移动窗口得到最大值7。
再然后得到最大值9。
此时窗口已经不能再往后移动(再移动的话,从9开始cover的元素不够3个,就超边界了),因此pool结束,返回4个结果值[3, 5, 7, 9],与程序输出的output相符合。
从这里看出,元素10就因为是保底模式(floor)而没有被窗口cover到。如果设置ceil_mode模式为True,得到结果是[3, 5, 7, 9, 10],
>>> # pool of size=3, stride=2
>>> m = nn.MaxPool1d(3, stride=2)
>>> input = torch.randn(20, 16, 50)
>>> output = m(input)
nn.MaxPool2d
>>> # 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 = torch.randn(20, 16, 50, 32)
>>> output = m(input)
nn.MaxPool3d
在由多个输入平面组成的输入信号上应用3D最大池化。
类似MaxPool1d
>>> # 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 = torch.randn(20, 16, 50, 44, 31)
>>> output = m(input)
nn.MaxUnpool1d
计算MaxPool1d的偏逆。
原理参考: https://vimsky.com/zh-tw/examples/usage/python-torch.nn.MaxUnpool1d-pt.html
>>> 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)
>>> unpool(output, indices)
tensor([[[ 0., 2., 0., 4., 0., 6., 0., 8.]]])
>>> # Example showcasing the use of output_size
>>> input = torch.tensor([[[1., 2, 3, 4, 5, 6, 7, 8, 9]]])
>>> output, indices = pool(input)
>>> unpool(output, indices, output_size=input.size())
tensor([[[ 0., 2., 0., 4., 0., 6., 0., 8., 0.]]])
>>> unpool(output, indices)
tensor([[[ 0., 2., 0., 4., 0., 6., 0., 8.]]])
nn.MaxUnpool2d
计算MaxPool2d的偏逆。
>>> pool = nn.MaxPool2d(2, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool2d(2, stride=2)
>>> input = 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)
tensor([[[[ 0., 0., 0., 0.],
[ 0., 6., 0., 8.],
[ 0., 0., 0., 0.],
[ 0., 14., 0., 16.]]]])
>>> # Now using output_size to resolve an ambiguous size for the inverse
>>> input = torch.torch.tensor([[[[ 1., 2., 3., 4., 5.],
[ 6., 7., 8., 9., 10.],
[11., 12., 13., 14., 15.],
[16., 17., 18., 19., 20.]]]])
>>> output, indices = pool(input)
>>> # This call will not work without specifying output_size
>>> unpool(output, indices, output_size=input.size())
tensor([[[[ 0., 0., 0., 0., 0.],
[ 0., 7., 0., 9., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 17., 0., 19., 0.]]]])
nn.MaxUnpool3d
计算MaxPool3d的偏逆
>>> # 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(torch.randn(20, 16, 51, 33, 15))
>>> unpooled_output = unpool(output, indices)
>>> unpooled_output.size()
torch.Size([20, 16, 51, 33, 15])
nn.AvgPool1d
对由多个输入平面组成的输入信号应用1D平均池化。
>>> # pool with window of size=3, stride=2
>>> m = nn.AvgPool1d(3, stride=2)
>>> m(torch.tensor([[[1., 2, 3, 4, 5, 6, 7]]]))
tensor([[[2., 4., 6.]]])
nn.AvgPool2d
>>> # 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 = torch.randn(20, 16, 50, 32)
>>> output = m(input)
nn.AvgPool3d
>>> # pool of square window of size=3, stride=2
>>> m = nn.AvgPool3d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.AvgPool3d((3, 2, 2), stride=(2, 1, 2))
>>> input = torch.randn(20, 16, 50, 44, 31)
>>> output = m(input)
nn.FractionalMaxPool2d
参考:
关于分数阶最大池化,它似乎并没有太多的实际用处。函数中仍需要kernel_size,如果指定output_size,它就可以输出指定大小的张量,类似于自适应最大池化;如果指定output_ratio,它就会根据输出大小与输入大小的比例output_ratio来决定输出大小,所以output_ratio为0~1。并且函数不能同时接受output_ratio与output_size,毕竟这两个参数都决定输出大小,同时使用势必会引起冲突。
>>> # 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 = torch.randn(20, 16, 50, 32)
>>> output = m(input)
nn.FractionalMaxPool3d
在由多个输入平面组成的输入信号上应用3D分数极大池化。
>>> # pool of cubic window of size=3, and target output size 13x12x11
>>> m = nn.FractionalMaxPool3d(3, output_size=(13, 12, 11))
>>> # pool of cubic window and target output size being half of input size
>>> m = nn.FractionalMaxPool3d(3, output_ratio=(0.5, 0.5, 0.5))
>>> input = torch.randn(20, 16, 50, 32, 16)
>>> output = m(input)
nn.LPPool1d
参考:
p即为norm_type,当p=\infty时,相当于最大池化;当p=1时,相当于Sum池化 kernel_size为滑动窗口大小,可以为一个整数,也可以为一个元组 stride为步幅,默认等于kernel_size
# 代码输入:
import torch
import torch.nn as nn
x = torch.range(1,120).reshape(2, 3, 4, 5)
print("原始数据:\n",x.shape, '\n', x)
pool = nn.LPPool2d(norm_type=2, kernel_size=2)
print("池化后数据:\n", pool(x))
print(pool(x).shape)
nn.LPPool2d
在由多个输入平面组成的输入信号上应用2D功率平均池。
>>> # 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 = torch.randn(20, 16, 50, 32)
>>> output = m(input)
nn.AdaptiveMaxPool1d
自适应池化的特点是:无论输入数据的size是多少,最终输出的size始终是函数指定的size。而背后的原理是:该函数可以根据输入size与我们指定的输出size来自动调整池化窗口、步长等参数。所以该函数没有kernel_size,stride,padding等参数,只需要指定输出size大小即可。如果需要让某一维度为原来大小,可以直接指定该部分参数为None。
>>> # target output size of 5
>>> m = nn.AdaptiveMaxPool1d(5)
>>> input = torch.randn(1, 64, 8)
>>> output = m(input)
nn.AdaptiveMaxPool2d
在由多个输入平面组成的输入信号上应用二维自适应最大池化。
>>> # target output size of 5x7
>>> m = nn.AdaptiveMaxPool2d((5, 7))
>>> input = torch.randn(1, 64, 8, 9)
>>> output = m(input)
>>> # target output size of 7x7 (square)
>>> m = nn.AdaptiveMaxPool2d(7)
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
>>> # target output size of 10x7
>>> m = nn.AdaptiveMaxPool2d((None, 7))
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
注意这里和 FractionalMaxPool2d比较一下, FractionalMaxPool2d是指定卷积核大小,但是这个AdaptiveMaxPool2d,不设置卷积核大小,随缘,可学习,所以注意区分。
nn.AdaptiveMaxPool3d
>>> # target output size of 5x7x9
>>> m = nn.AdaptiveMaxPool3d((5, 7, 9))
>>> input = torch.randn(1, 64, 8, 9, 10)
>>> output = m(input)
>>> # target output size of 7x7x7 (cube)
>>> m = nn.AdaptiveMaxPool3d(7)
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)
>>> # target output size of 7x9x8
>>> m = nn.AdaptiveMaxPool3d((7, None, None))
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)
nn.AdaptiveAvgPool1d
在由多个输入平面组成的输入信号上应用1D自适应平均池。
>>> # target output size of 5
>>> m = nn.AdaptiveAvgPool1d(5)
>>> input = torch.randn(1, 64, 8)
>>> output = m(input)
nn.AdaptiveAvgPool2d
在由多个输入平面组成的输入信号上应用二维自适应平均池。
>>> # target output size of 5x7
>>> m = nn.AdaptiveAvgPool2d((5, 7))
>>> input = torch.randn(1, 64, 8, 9)
>>> output = m(input)
>>> # target output size of 7x7 (square)
>>> m = nn.AdaptiveAvgPool2d(7)
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
>>> # target output size of 10x7
>>> m = nn.AdaptiveAvgPool2d((None, 7))
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
nn.AdaptiveAvgPool3d
在由多个输入平面组成的输入信号上应用三维自适应平均池。
>>> # target output size of 5x7x9
>>> m = nn.AdaptiveAvgPool3d((5, 7, 9))
>>> input = torch.randn(1, 64, 8, 9, 10)
>>> output = m(input)
>>> # target output size of 7x7x7 (cube)
>>> m = nn.AdaptiveAvgPool3d(7)
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)
>>> # target output size of 7x9x8
>>> m = nn.AdaptiveAvgPool3d((7, None, None))
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)