Pytorch(筆記3)--MaxPool2d&AdaptiveAvgPool2d

   在上一節中我們詳細的闡述了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

堅持一件事或許很難,但堅持下來一定很酷!^_^

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章