欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch AvgPool2d函數(shù)使用詳解

 更新時(shí)間:2020年01月03日 10:29:01   作者:geter_CS  
今天小編就為大家分享一篇pytorch AvgPool2d函數(shù)使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,直接上代碼吧!

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
 
 
 
input = Variable(torch.Tensor([[[1, 3, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7]], [[1, 3, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7]]]))
print("input shape",input.shape)
c = F.avg_pool1d(input, kernel_size=3, stride=2)
print(c)
print("c shape:",c.shape)
 
# m = nn.AvgPool2d(3, stride=2)
m = nn.AvgPool2d((2, 2), stride=(2, 2))
input = Variable(torch.randn(20, 18, 50, 32)) # bach是20,圖片size是50*31,chanel是18(通道是18,也就是每張圖有18?jìng)€(gè)fature map)
input = np.array([[[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]],
          [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]],
         [[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]],
          [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]]]) #size2*2*4*4
print("input shape:",input.shape)
input = Variable(torch.FloatTensor(input))
output = m(input)
print(output)
print("output shape:",output.shape)#(2,2,2,2)

輸出:

input shape torch.Size([2, 2, 7])
tensor([[[ 2.3333, 4.0000, 6.0000],
     [ 2.0000, 4.0000, 6.0000]],
 
    [[ 2.3333, 4.0000, 6.0000],
     [ 2.0000, 4.0000, 6.0000]]])
c shape: torch.Size([2, 2, 3])
input shape: (2, 2, 4, 4)
tensor([[[[ 1.5000, 3.5000],
     [ 1.5000, 3.5000]],
 
     [[ 1.5000, 3.5000],
     [ 1.5000, 3.5000]]],
 
 
    [[[ 1.5000, 3.5000],
     [ 1.5000, 3.5000]],
 
     [[ 1.5000, 3.5000],
     [ 1.5000, 3.5000]]]])
output shape: torch.Size([2, 2, 2, 2])

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也就是兩張圖像,這里應(yīng)該是有通道(feature map)數(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é)果是一致的。

以上這篇pytorch AvgPool2d函數(shù)使用詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論