pytorch方法測(cè)試詳解——?dú)w一化(BatchNorm2d)
測(cè)試代碼:
import torch import torch.nn as nn m = nn.BatchNorm2d(2,affine=True) #權(quán)重w和偏重將被使用 input = torch.randn(1,2,3,4) output = m(input) print("輸入圖片:") print(input) print("歸一化權(quán)重:") print(m.weight) print("歸一化的偏重:") print(m.bias) print("歸一化的輸出:") print(output) print("輸出的尺度:") print(output.size()) # i = torch.randn(1,1,2) print("輸入的第一個(gè)維度:") print(input[0][0]) firstDimenMean = torch.Tensor.mean(input[0][0]) firstDimenVar= torch.Tensor.var(input[0][0],False) #Bessel's Correction貝塞爾校正不會(huì)被使用 print(m.eps) print("輸入的第一個(gè)維度平均值:") print(firstDimenMean) print("輸入的第一個(gè)維度方差:") print(firstDimenVar) bacthnormone = \ ((input[0][0][0][0] - firstDimenMean)/(torch.pow(firstDimenVar+m.eps,0.5) ))\ * m.weight[0] + m.bias[0] print(bacthnormone)
輸出為:
輸入圖片:
tensor([[[[-2.4308, -1.0281, -1.1322, 0.9819], [-0.4069, 0.7973, 1.6296, 1.6797], [ 0.2802, -0.8285, 2.0101, 0.1286]], [[-0.5740, 0.1970, -0.7209, -0.7231], [-0.1489, 0.4993, 0.4159, 1.4238], [ 0.0334, -0.6333, 0.1308, -0.2180]]]])
歸一化權(quán)重:
Parameter containing: tensor([ 0.5653, 0.0322])
歸一化的偏重:
Parameter containing: tensor([ 0., 0.])
歸一化的輸出:
tensor([[[[-1.1237, -0.5106, -0.5561, 0.3679], [-0.2391, 0.2873, 0.6510, 0.6729], [ 0.0612, -0.4233, 0.8173, -0.0050]], [[-0.0293, 0.0120, -0.0372, -0.0373], [-0.0066, 0.0282, 0.0237, 0.0777], [ 0.0032, -0.0325, 0.0084, -0.0103]]]])
輸出的尺度:
torch.Size([1, 2, 3, 4])
輸入的第一個(gè)維度:
tensor([[-2.4308, -1.0281, -1.1322, 0.9819], [-0.4069, 0.7973, 1.6296, 1.6797], [ 0.2802, -0.8285, 2.0101, 0.1286]]) 1e-05
輸入的第一個(gè)維度平均值:
tensor(0.1401)
輸入的第一個(gè)維度方差:
tensor(1.6730) tensor(-1.1237)
結(jié)論:
輸出的計(jì)算公式如下
注意torch中方差實(shí)現(xiàn)的方法是沒(méi)有使用Bessel's correction 貝塞爾校正的方差,所以在自己寫(xiě)的方差中不要用錯(cuò)了。(貝塞爾校正,即樣本方差和總體方差之間區(qū)別和校正。)
以上這篇pytorch方法測(cè)試詳解——?dú)w一化(BatchNorm2d)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python dict remove數(shù)組刪除(del,pop)
我們?cè)谟脭?shù)組列表做刪除的時(shí)候,可能選擇2個(gè)方法,一個(gè)是del,一個(gè)是pop方法2013-03-03利用pandas進(jìn)行數(shù)據(jù)清洗的方法
本文主要介紹了利用pandas進(jìn)行數(shù)據(jù)清洗的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Python中re正則匹配數(shù)據(jù)的實(shí)現(xiàn)
在Python中,可以使用re模塊來(lái)使用正則表達(dá)式,本文主要介紹了Python中re正則匹配數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04python飛機(jī)大戰(zhàn)pygame游戲背景設(shè)計(jì)詳解
這篇文章主要介紹了python飛機(jī)大戰(zhàn)pygame游戲背景設(shè)計(jì),結(jié)合實(shí)例形式詳細(xì)分析了Python使用pygame模塊設(shè)計(jì)游戲背景相關(guān)原理、流程與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-12-12