日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

batchnorm2d参数 torch_Pytorch-nn.BatchNorm2d()

發布時間:2025/3/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 batchnorm2d参数 torch_Pytorch-nn.BatchNorm2d() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Pytorch官方文檔:

測試代碼:

轉自:https://blog.csdn.net/tmk_01/article/details/80679549

import torch

import torch.nn as nn

m = nn.BatchNorm2d(2,affine=True) #weight(gamma)和bias(beta)將被使用

input = torch.randn(1,2,3,4)

output = m(input)

print("輸入圖片:")

print(input)

print("歸一化權重(公式中的gamma):")

print(m.weight)

print("歸一化偏置(公式中的beta):")

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([[[[-1.1622, -0.9170, -0.6798, -0.0270],

[ 0.2687, -1.6046, -0.2142, -0.3561],

[ 0.2908, -0.1012, 1.3594, 1.1316]],

[[ 0.4689, 1.4049, 1.2324, -1.3721],

[-0.1498, -0.3207, 0.5072, -1.2563],

[ 1.5934, -0.8010, 0.1270, 0.5993]]]])

歸一化權重(公式中的gamma):

Parameter containing:

tensor([0.8681, 0.7207], requires_grad=True)

歸一化偏置(公式中的beta):

Parameter containing:

tensor([0., 0.], requires_grad=True)

歸一化的輸出:

tensor([[[[-1.0344, -0.7794, -0.5326, 0.1463],

[ 0.4538, -1.4945, -0.0484, -0.1960],

[ 0.4767, 0.0691, 1.5881, 1.3512]],

[[ 0.2279, 0.9400, 0.8088, -1.1729],

[-0.2429, -0.3729, 0.2570, -1.0848],

[ 1.0834, -0.7384, -0.0323, 0.3271]]]],

grad_fn=)

輸出的尺度:

torch.Size([1, 2, 3, 4])

輸入的第一個維度:

tensor([[-1.1622, -0.9170, -0.6798, -0.0270],

[ 0.2687, -1.6046, -0.2142, -0.3561],

[ 0.2908, -0.1012, 1.3594, 1.1316]])

1e-05

輸入的第一個維度平均值:

tensor(-0.1676)

輸入的第一個維度方差:

tensor(0.6967)

tensor(-1.0344, grad_fn=)

BatchNorm深度理解可參考:

https://zhuanlan.zhihu.com/p/30922689

4.Normalizing activations in a network

5.Fitting Batch Norm into a neural network

6.Why does Batch Norm work?

7. Batch Norm at test time

總結

以上是生活随笔為你收集整理的batchnorm2d参数 torch_Pytorch-nn.BatchNorm2d()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。