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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Pytorch中的Batch Normalization操作

發布時間:2025/3/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pytorch中的Batch Normalization操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?之前一直和小伙伴探討batch normalization層的實現機理,作用在這里不談,知乎上有一篇paper在講這個,鏈接

這里只探究其具體運算過程,我們假設在網絡中間經過某些卷積操作之后的輸出的feature map的尺寸為4×3×2×2

4為batch的大小,3為channel的數目,2×2為feature map的長寬

整個BN層的運算過程如下圖

?

上圖中,batch size一共是4, 對于每一個batch的feature map的size是3×2×2

對于所有batch中的同一個channel的元素進行求均值與方差,比如上圖,對于所有的batch,都拿出來最后一個channel,一共有4×4=16個元素,

然后求區這16個元素的均值與方差(上圖只求了mean,沒有求方差。。。),

求取完了均值與方差之后,對于這16個元素中的每個元素進行減去求取得到的均值與方差,然后乘以gamma加上beta,公式如下

所以對于一個batch normalization層而言,求取的均值與方差是對于所有batch中的同一個channel進行求取,batch normalization中的batch體現在這個地方

batch normalization層能夠學習到的參數,對于一個特定的channel而言實際上是兩個參數,gamma與beta,對于total的channel而言實際上是channel數目的兩倍。

?

用pytorch驗證上述想法是否準確,用上述方法求取均值,以及用batch normalization層輸出的均值,看看是否一樣

上代碼

1 # -*-coding:utf-8-*- 2 from torch import nn 3 import torch 4 5 m = nn.BatchNorm2d(3) # bn設置的參數實際上是channel的參數 6 input = torch.randn(4, 3, 2, 2) 7 output = m(input) 8 # print(output) 9 a = (input[0, 0, :, :]+input[1, 0, :, :]+input[2, 0, :, :]+input[3, 0, :, :]).sum()/16 10 b = (input[0, 1, :, :]+input[1, 1, :, :]+input[2, 1, :, :]+input[3, 1, :, :]).sum()/16 11 c = (input[0, 2, :, :]+input[1, 2, :, :]+input[2, 2, :, :]+input[3, 2, :, :]).sum()/16 12 print('The mean value of the first channel is %f' % a.data) 13 print('The mean value of the first channel is %f' % b.data) 14 print('The mean value of the first channel is %f' % c.data) 15 print('The output mean value of the BN layer is %f, %f, %f' % (m.running_mean.data[0],m.running_mean.data[0],m.running_mean.data[0])) 16 print(m)

m = nn.BatchNorm2d(3)

聲明新的batch normalization層,用

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

模擬feature map的尺寸

輸出值

?

咦,怎么不一樣,貌似差了一個小數點,可能與BN層的momentum變量有關系,在生命batch normalization層的時候將momentum設置為1試一試

m.momentum=1

輸出結果

沒毛病

至于方差以及輸出值,大抵也是這樣進行計算的吧,留個坑

轉載于:https://www.cnblogs.com/yongjieShi/p/9332655.html

總結

以上是生活随笔為你收集整理的Pytorch中的Batch Normalization操作的全部內容,希望文章能夠幫你解決所遇到的問題。

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