在上一节中我们详细的阐述了Conv2d的计算原理,今天我们来讲述下Pytorch中其他比较常见的操作!
在lenet5的时候,受限于计算能力和存储能力,通常采用downsample来降维
在pytorch中使用Pooling操作来实现采样,常见的pool操作包含Max_pool,Avg_pool等
Max_pool
x = t.rand(1,3,7,7)
out = nn.MaxPool2d(kernel_size=2,stride=2)
out.forward(x).shape
torch.Size([1, 3, 3, 3])
启动kernel代表的是观察角度,如下图kernel就是2*2,stride和Conv操作中一样代表每次移动的步长。
下图操作,在每次观察区域内取最大值作为采样数据进行降维操作,这样做的优点是可以使显性特征更明显,降维操作并没有更改输出和输出的channel_num
Avg_pool
对于Avg_pool来说,参数和Max_pool是完全相同的,主要区别就是在kernel中取的是平均值操作。
AdaptiveAvgPool2d&AdaptiveMaxPool2d
和之前的运算方法不同,torch.nn提供了自适应size的函数样例如下:
可以看出,输出结果的size是按照我们给丁的结果进行运算的,一个参数代表H和W相同,也可以自定义(H,W),底层是对F.adaptive_avg_pool2d的封装,一般在类中的是大写小写的是函数,在Function中。
class AdaptiveAvgPool2d(_AdaptiveAvgPoolNd):
r"""Applies a 2D adaptive average pooling over an input signal composed of several input planes.
The output is of size H x W, for any input size.
The number of output features is equal to the number of input planes.
Args:
output_size: the target output size of the image of the form H x W.
Can be a tuple (H, W) or a single H for a square image H x H.
H and W can be either a ``int``, or ``None`` which means the size will
be the same as that of the input.
Examples:
>>> # 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.AdaptiveMaxPool2d((None, 7))
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
"""
@weak_script_method
def forward(self, input):
return F.adaptive_avg_pool2d(input, self.output_size)
我们可以根据下面的内容推导出我们不知道的参数,从而实现AdaptiveAvgPool2d&AdaptiveMaxPool2与前两个函数的转换,C++源码
stride = floor ( (input_size / (output_size) )
kernel_size = input_size − (output_size−1) * stride
padding = 0
坚持一件事或许很难,但坚持下来一定很酷!^_^