溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

pytorch中卷積和池化計(jì)算方式的示例分析

發(fā)布時(shí)間:2021-08-17 11:38:51 來(lái)源:億速云 閱讀:287 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了pytorch中卷積和池化計(jì)算方式的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

TensorFlow里面的padding只有兩個(gè)選項(xiàng)也就是valid和same

pytorch里面的padding么有這兩個(gè)選項(xiàng),它是數(shù)字0,1,2,3等等,默認(rèn)是0

所以輸出的h和w的計(jì)算方式也是稍微有一點(diǎn)點(diǎn)不同的:tf中的輸出大小是和原來(lái)的大小成倍數(shù)關(guān)系,不能任意的輸出大??;而nn輸出大小可以通過(guò)padding進(jìn)行改變

nn里面的卷積操作或者是池化操作的H和W部分都是一樣的計(jì)算公式:H和W的計(jì)算

pytorch中卷積和池化計(jì)算方式的示例分析

class torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False):
"""
Parameters: 
  kernel_size – the size of the window to take a max over
  stride – the stride of the window. 默認(rèn)值是kernel_size
  padding – implicit zero padding to be added on both side,默認(rèn)值是0
  dilation – a parameter that controls the stride of elements in the window,默認(rèn)值是1
  return_indices – if True, will return the max indices along with the outputs. Useful when Unpooling later
  ceil_mode – when True, will use ceil instead of floor to compute the output shape,向上取整和向下取整,默認(rèn)是向下取整
"""

不一樣的地方在于:第一點(diǎn),步長(zhǎng)stride默認(rèn)值,上面默認(rèn)和設(shè)定的kernel_size一樣,下面默認(rèn)是1;第二點(diǎn),輸出通道的不一樣,上面的輸出通道和輸入通道是一樣的也就是沒(méi)有改變特征圖的數(shù)目,下面改變特征圖的數(shù)目為out_channels

class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True):
    pass
"""
Parameters: 
  in_channels (int) – Number of channels in the input image
  out_channels (int) – Number of channels produced by the convolution
  kernel_size (int or tuple) – Size of the convolving kernel
  stride (int or tuple, optional) – Stride of the convolution. Default: 1,默認(rèn)是1
  padding (int or tuple, optional) – Zero-padding added to both sides of the input. Default: 0
  dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1
  groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1
  bias (bool, optional) – If True, adds a learnable bias to the output. Default: True
"""

第三點(diǎn)不一樣是卷積有一個(gè)參數(shù)groups,將特征圖分開(kāi)給不同的卷積進(jìn)行操作然后再整合到一起,xception就是利用這一個(gè)。

"""
At groups=1, all inputs are convolved to all outputs.
At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels, and producing half the output channels, and both subsequently concatenated.
At groups= in_channels, each input channel is convolved with its own set of filters (of size ?out_channelsin_channels?
).
"""

pytorch AvgPool2d函數(shù)

class torch.nn.AvgPool2d(kernel_size, stride=None, padding=0, 
             ceil_mode=False, count_include_pad=True):
  pass
"""
kernel_size: the size of the window
stride: the stride of the window. Default value is :attr:`kernel_size`
padding: implicit zero padding to be added on both sides
ceil_mode: when True, will use `ceil` instead of `floor` to compute the output shape
count_include_pad: when True, will include the zero-padding in the averaging calculation
"""

shape的計(jì)算公式,在(h,w)位置處的輸出值的計(jì)算。

pytorch中卷積和池化計(jì)算方式的示例分析

pytorch中的F.avg_pool1d()平均池化操作作用于一維,input 的維度是三維比如[2,2,7]。F.avg_pool1d()中核size是3,步長(zhǎng)是2表示每三個(gè)數(shù)取平均,每隔兩個(gè)數(shù)取一次.比如[1,3,3,4,5,6,7]安照3個(gè)數(shù)取均值,兩步取一次,那么結(jié)果就是[ 2.3333 ,4 ,6 ],也就是核是一維的,也只作用于一個(gè)維度。按照池化操作計(jì)算公式input size為[2,2,7],kernel size為3,步長(zhǎng)為2,則輸出維度計(jì)算(7-3)/2+1=3所以輸出維度是[2,2,3],這與輸出結(jié)果是一致的。

pytorch中的F.avg_pool2d(),input 是維度是4維如[2,2,4,4],表示這里批量數(shù)是2也就是兩張圖像,這里通道數(shù)量是2,圖像是size 是4*4的.核size是(2,2),步長(zhǎng)是(2,2)表示被核覆蓋的數(shù)取平均,橫向縱向的步長(zhǎng)都是2.那么核是二維的,所以取均值時(shí)也是覆蓋二維取的。輸出中第一個(gè)1.5的計(jì)算是:(1+2+1+2)/4=1.5.表示第一張圖像左上角的四個(gè)像素點(diǎn)的均值。按照池化操作計(jì)算公式input size為[2,2,4,4],kernel size為2*2,步長(zhǎng)為2,則輸出維度計(jì)算(4-2)/2+1=2所以輸出維度是[2,2,2,2],這與輸出結(jié)果是一致的。

Conv3d函數(shù)

class torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1,
           padding=0, dilation=1, groups=1, bias=True):
  pass
"""
in_channels (int): Number of channels in the input image
out_channels (int): Number of channels produced by the convolution
kernel_size (int or tuple): Size of the convolving kernel
stride (int or tuple, optional): Stride of the convolution. Default: 1
padding (int or tuple, optional): Zero-padding added to all three sides of the input. Default: 0
dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
Shape:
    - Input: :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})`
    - Output: :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})`
"""
  C_out = out_channels

pytorch中卷積和池化計(jì)算方式的示例分析

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“pytorch中卷積和池化計(jì)算方式的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI