pytorch方法測試詳解——歸一化(BatchNorm2d)
更新時間:2020年01月15日 15:54:34 作者:tmk_01
今天小編就為大家分享一篇pytorch方法測試詳解——歸一化(BatchNorm2d),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
測試代碼:
import torch
import torch.nn as nn
m = nn.BatchNorm2d(2,affine=True) #權重w和偏重將被使用
input = torch.randn(1,2,3,4)
output = m(input)
print("輸入圖片:")
print(input)
print("歸一化權重:")
print(m.weight)
print("歸一化的偏重:")
print(m.bias)
print("歸一化的輸出:")
print(output)
print("輸出的尺度:")
print(output.size())
# i = torch.randn(1,1,2)
print("輸入的第一個維度:")
print(input[0][0])
firstDimenMean = torch.Tensor.mean(input[0][0])
firstDimenVar= torch.Tensor.var(input[0][0],False) #Bessel's Correction貝塞爾校正不會被使用
print(m.eps)
print("輸入的第一個維度平均值:")
print(firstDimenMean)
print("輸入的第一個維度方差:")
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]]]])
歸一化權重:
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])
輸入的第一個維度:
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
輸入的第一個維度平均值:
tensor(0.1401)
輸入的第一個維度方差:
tensor(1.6730) tensor(-1.1237)
結論:
輸出的計算公式如下

注意torch中方差實現(xiàn)的方法是沒有使用Bessel's correction 貝塞爾校正的方差,所以在自己寫的方差中不要用錯了。(貝塞爾校正,即樣本方差和總體方差之間區(qū)別和校正。)
以上這篇pytorch方法測試詳解——歸一化(BatchNorm2d)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python dict remove數(shù)組刪除(del,pop)
我們在用數(shù)組列表做刪除的時候,可能選擇2個方法,一個是del,一個是pop方法2013-03-03
Python中re正則匹配數(shù)據(jù)的實現(xiàn)
在Python中,可以使用re模塊來使用正則表達式,本文主要介紹了Python中re正則匹配數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-04-04
python飛機大戰(zhàn)pygame游戲背景設計詳解
這篇文章主要介紹了python飛機大戰(zhàn)pygame游戲背景設計,結合實例形式詳細分析了Python使用pygame模塊設計游戲背景相關原理、流程與實現(xiàn)技巧,需要的朋友可以參考下2019-12-12

