pytorch中F.avg_pool1d()和F.avg_pool2d()的使用操作
F.avg_pool1d()數(shù)據(jù)是三維輸入
input維度: (batch_size,channels,width)channel可以看成高度
kenerl維度:(一維:表示width的跨度)channel和輸入的channel一致可以認(rèn)為是矩陣的高度
假設(shè)kernel_size=2,則每倆列相加求平均,stride默認(rèn)和kernel_size保持一致,越界則丟棄(下面表示1,2列和3,4列相加求平均)
input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float() print(input) m = F.avg_pool1d(input,kernel_size=2) m tensor([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [0., 0., 0., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]]) tensor([[[1.0000, 1.0000], [1.0000, 1.0000], [0.0000, 0.5000], [1.0000, 1.0000], [1.0000, 1.0000]]])
假設(shè)kenerl_size=3,表示前3列相加求平均,后面的不足3列丟棄
input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float() print(input) m = F.avg_pool1d(input,kernel_size=3) m tensor([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [0., 0., 0., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]]) tensor([[[1.], [1.], [0.], [1.], [1.]]]) input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float() print(input) m = F.avg_pool1d(input,kernel_size=4) m tensor([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [0., 0., 0., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]]) tensor([[[1.0000], [1.0000], [0.2500], [1.0000], [1.0000]]])
假設(shè)stride=1每次移動(dòng)一個(gè)步伐
input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float() print(input) m = F.avg_pool1d(input,kernel_size=2,stride=1) m tensor([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [0., 0., 0., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]]) tensor([[[1.0000, 1.0000, 1.0000, 1.0000], [1.0000, 1.0000, 1.0000, 1.0000], [0.0000, 0.0000, 0.5000, 1.0000], [1.0000, 1.0000, 1.0000, 1.0000], [1.0000, 1.0000, 1.0000, 1.0000]]]) input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float() print(input) m = F.avg_pool1d(input,kernel_size=4,stride=1) m tensor([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [0., 0., 0., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]]) tensor([[[1.0000, 1.0000], [1.0000, 1.0000], [0.2500, 0.5000], [1.0000, 1.0000], [1.0000, 1.0000]]])
F.avg_pool2d()數(shù)據(jù)是四維輸入
input維度: (batch_size,channels,height,width)
kenerl維度:(二維:表示width的跨度)channel和輸入的channle一致,如果數(shù)據(jù)是三維,則channel為1.(如果只寫一個(gè)數(shù)n,kenerl=(n,n))
stride默認(rèn)和kenerl一致,這是個(gè)二維的,所以在height和width上均和kenerl一致,越界同樣丟棄。
跟cnn卷積一致
input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float() print(input.size()) print(input) m = F.avg_pool2d(input,kernel_size=(4,4)) m torch.Size([1, 5, 5]) tensor([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [0., 0., 0., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]]) tensor([[[0.8125]]]) input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float() print(input.size()) print(input) m = F.avg_pool2d(input,kernel_size=(4,4),stride=1) m torch.Size([1, 5, 5]) tensor([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [0., 0., 0., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]]) tensor([[[0.8125, 0.8750], [0.8125, 0.8750]]])
如果求列的平均kenerl=(1,5),此時(shí)默認(rèn)stride=(1,5)
input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float() print(input.size()) print(input) m = F.avg_pool2d(input,kernel_size=(1,5)) m torch.Size([1, 5, 5]) tensor([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [0., 0., 0., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]]) tensor([[[1.0000], [1.0000], [0.4000], [1.0000], [1.0000]]])
如果求行的平均kenerl=(5,1),此時(shí)默認(rèn)stride=(5,1),用卷積的概念取思考
input = torch.tensor([[1,1,1,1,1],[1,1,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]).unsqueeze(0).float() print(input.size()) print(input) m = F.avg_pool2d(input,kernel_size=(5,1)) m torch.Size([1, 5, 5]) tensor([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [0., 0., 0., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]]) tensor([[[0.8000, 0.8000, 0.8000, 1.0000, 1.0000]]])
對(duì)于四維的數(shù)據(jù),channel默認(rèn)和輸入一致
input=torch.randn(10,3,4,4) m=F.avg_pool2d(input,(4,4)) print(m.size()) torch.Size([10, 3, 1, 1])
補(bǔ)充:PyTorch中AdaptiveAvgPool函數(shù)解析
自適應(yīng)池化(AdaptiveAvgPool1d):
對(duì)輸入信號(hào),提供1維的自適應(yīng)平均池化操作 對(duì)于任何輸入大小的輸入,可以將輸出尺寸指定為H*W,但是輸入和輸出特征的數(shù)目不會(huì)變化。
torch.nn.AdaptiveAvgPool1d(output_size) #output_size:輸出尺寸
對(duì)輸入信號(hào),提供1維的自適應(yīng)平均池化操作 對(duì)于任何輸入大小的輸入,可以將輸出尺寸指定為H*W,但是輸入和輸出特征的數(shù)目不會(huì)變化。
# target output size of 5 m = nn.AdaptiveAvgPool1d(5) input = autograd.Variable(torch.randn(1, 64, 8)) output = m(input)
自適應(yīng)池化(AdaptiveAvgPool2d):
class torch.nn.AdaptiveAvgPool2d(output_size)
對(duì)輸入信號(hào),提供2維的自適應(yīng)平均池化操作 對(duì)于任何輸入大小的輸入,可以將輸出尺寸指定為H*W,但是輸入和輸出特征的數(shù)目不會(huì)變化。
參數(shù):
output_size: 輸出信號(hào)的尺寸,可以用(H,W)表示H*W的輸出,也可以使用耽擱數(shù)字H表示H*H大小的輸出
# target output size of 5x7 m = nn.AdaptiveAvgPool2d((5,7)) input = autograd.Variable(torch.randn(1, 64, 8, 9)) # target output size of 7x7 (square) m = nn.AdaptiveAvgPool2d(7) input = autograd.Variable(torch.randn(1, 64, 10, 9)) output = m(input)
自適應(yīng)池化的數(shù)學(xué)解釋:
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。