批標(biāo)準(zhǔn)化(Batch Normalization,簡稱BN)是一種用于深度神經(jīng)網(wǎng)絡(luò)的技術(shù),它的主要目的是解決深度學(xué)習(xí)模型訓(xùn)練過程中的內(nèi)部協(xié)變量偏移問題。簡單來說,當(dāng)我們在訓(xùn)練深度神經(jīng)網(wǎng)絡(luò)時(shí),每一層的輸入分布都可能會隨著前一層參數(shù)的更新而發(fā)生變化,這種變化會導(dǎo)致訓(xùn)練過程變得不穩(wěn)定。BN通過對每一層的輸入進(jìn)行標(biāo)準(zhǔn)化,使其均值為0,方差為1,從而使得網(wǎng)絡(luò)在每一層都能接收到相對穩(wěn)定的數(shù)據(jù)分布。
BatchNorm1d
對2d或3d數(shù)據(jù)進(jìn)行批標(biāo)準(zhǔn)化(Batch Normlization)操作:
class torch.nn.BatchNorm1d(num_features, eps=1e-05, momentum=0.1, affine=True):
參數(shù):
1.num_features:特征的維度 ( N , L ) ? > L ; ( N , C , L ) ? > C (N,L) -> L ;(N,C,L) -> C (N,L)?>L;(N,C,L)?>C
2.eps:在分母上添加一個(gè)定值,不能趨近于0
3.momentum:動(dòng)態(tài)均值和動(dòng)態(tài)方差所使用的動(dòng)量,這里的momentum是對均值和方差進(jìn)行的滑動(dòng)平均。即 μ 1 = ( 1 ? m o m e n t u m ) ? μ l a s t + m o m e n t u m ? μ μ_1 = (1 - momentum)* μ_{last} + momentum * μ μ1?=(1?momentum)?μlast?+momentum?μ,這里μ1為輸出值,μ_last為上一次的計(jì)算值,μ為真實(shí)計(jì)算的值
4.affine:布爾變量,是否為該層添加可學(xué)習(xí)的仿設(shè)變換,仿射變換的系數(shù)即為下式的gamma和beta
原理:
計(jì)算各個(gè)維度的均值和標(biāo)準(zhǔn)差: y = x ? mean ? [ x ] Var ? [ x ] + ? ? g a m m a + ?beta? y=\frac{x-\operatorname{mean}[x]}{\sqrt{\operatorname{Var}[x]}+\epsilon} * g a m m a+\text { beta } y=Var[x]?+?x?mean[x]??gamma+?beta?
m = nn.BatchNorm1d(5, affine=False)
m1 = nn.BatchNorm1d(5, affine=True)
input = autograd.Variable(torch.randn(5, 5))
output = m(input)
output1 = m1(input)
print(input, '\n',output,'\n',output1)
tensor([[-0.6046, -0.8939, 1.3246, 0.2621, 1.0777],
[ 0.9088, -0.6219, 0.9589, 0.7307, 0.5221],
[ 1.7435, 0.6662, -0.5827, 0.3325, -0.8179],
[-0.2250, 0.9930, 0.0504, -0.4509, 1.6605],
[-0.5742, 1.6543, 0.6083, 0.5746, -0.3208]])
tensor([[-0.9212, -1.2920, 1.2648, -0.0680, 0.7249],
[ 0.7107, -1.0117, 0.7224, 1.0842, 0.1085],
[ 1.6108, 0.3161, -1.5642, 0.1049, -1.3780],
[-0.5119, 0.6530, -0.6252, -1.8215, 1.3713],
[-0.8885, 1.3345, 0.2022, 0.7005, -0.8266]])
tensor([[-0.9212, -1.2920, 1.2648, -0.0680, 0.7249],
[ 0.7107, -1.0117, 0.7224, 1.0842, 0.1085],
[ 1.6108, 0.3161, -1.5642, 0.1049, -1.3780],
[-0.5119, 0.6530, -0.6252, -1.8215, 1.3713],
[-0.8885, 1.3345, 0.2022, 0.7005, -0.8266]],
grad_fn=<NativeBatchNormBackward>)
BatchNorm2d
對小批量(mini-batch)3d數(shù)據(jù)組成的4d輸入進(jìn)行批標(biāo)準(zhǔn)化(Batch Normalization)操作
class torch.nn.BatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True):
1.num_features: 來自期望輸入的特征數(shù),C from an expected input of size (N,C,H,W)
2.eps: 為保證數(shù)值穩(wěn)定性(分母不能趨近或取0),給分母加上的值。默認(rèn)為1e-5.
3.momentum: 動(dòng)態(tài)均值和動(dòng)態(tài)方差所使用的動(dòng)量。默認(rèn)為0.1.
4.affine: 一個(gè)布爾值,當(dāng)設(shè)為true,給該層添加可學(xué)習(xí)的仿射變換參數(shù)。
原理:
計(jì)算各個(gè)維度的均值和標(biāo)準(zhǔn)差: y = x ? mean ? [ x ] Var ? [ x ] + ? ? g a m m a + ?beta? y=\frac{x-\operatorname{mean}[x]}{\sqrt{\operatorname{Var}[x]}+\epsilon} * g a m m a+\text { beta } y=Var[x]?+?x?mean[x]??gamma+?beta?
m = nn.BatchNorm2d(2, affine=False)
m1 = nn.BatchNorm2d(2, affine=True)
input = autograd.Variable(torch.randn(1,2,5, 5))
output = m(input)
output1 = m1(input)
print(input, '\n',output,'\n',output1)
tensor([[[[-0.2606, -0.8874, 0.8364, 0.0184, 0.8040],
[ 1.0593, -0.6811, 1.3497, -0.6840, -2.0859],
[-0.5399, 1.3321, -0.6281, -0.9044, 1.7491],
[ 0.7559, 0.5607, -0.0447, -0.3868, 1.2404],
[ 1.2078, -0.9642, 0.3980, 0.2087, -1.3940]],
[[ 0.0493, 0.7372, 1.1964, 0.3862, 0.9900],
[ 0.3544, 0.1767, -1.5780, 0.1642, -2.1586],
[-0.4891, -0.7272, 1.6860, -1.6091, 0.9730],
[-2.4161, -2.2096, 0.4617, -0.2965, -0.5663],
[-0.0222, -0.7628, 0.6404, -1.4428, 0.5750]]]])
tensor([[[[-0.3522, -0.9959, 0.7743, -0.0657, 0.7410],
[ 1.0032, -0.7840, 1.3015, -0.7870, -2.2266],
[-0.6390, 1.2833, -0.7296, -1.0134, 1.7116],
[ 0.6917, 0.4912, -0.1305, -0.4818, 1.1892],
[ 1.1557, -1.0748, 0.3242, 0.1298, -1.5161]],
[[ 0.2560, 0.8743, 1.2870, 0.5588, 1.1015],
[ 0.5302, 0.3705, -1.2066, 0.3593, -1.7285],
[-0.2280, -0.4420, 1.7271, -1.2346, 1.0862],
[-1.9599, -1.7743, 0.6266, -0.0549, -0.2974],
[ 0.1917, -0.4739, 0.7873, -1.0852, 0.7285]]]])
tensor([[[[-0.3522, -0.9959, 0.7743, -0.0657, 0.7410],
[ 1.0032, -0.7840, 1.3015, -0.7870, -2.2266],
[-0.6390, 1.2833, -0.7296, -1.0134, 1.7116],
[ 0.6917, 0.4912, -0.1305, -0.4818, 1.1892],
[ 1.1557, -1.0748, 0.3242, 0.1298, -1.5161]],
[[ 0.2560, 0.8743, 1.2870, 0.5588, 1.1015],
[ 0.5302, 0.3705, -1.2066, 0.3593, -1.7285],
[-0.2280, -0.4420, 1.7271, -1.2346, 1.0862],
[-1.9599, -1.7743, 0.6266, -0.0549, -0.2974],
[ 0.1917, -0.4739, 0.7873, -1.0852, 0.7285]]]],
grad_fn=<NativeBatchNormBackward>)
使用
用的地方通常在一個(gè)全連接或者卷積層與激活函數(shù)中間,即 (全連接/卷積)—- BatchNorm —- 激活函數(shù)。但也有人說把 BatchNorm 放在激活函數(shù)后面效果更好,可以都試一下。
BN的作用:
- 加速訓(xùn)練:BN可以使得網(wǎng)絡(luò)的訓(xùn)練速度更快。因?yàn)榻?jīng)過標(biāo)準(zhǔn)化后,權(quán)重的更新方向更加明確,可以使用更大的學(xué)習(xí)率進(jìn)行訓(xùn)練。
- 正則化效果:BN具有輕微的正則化效果,可以在一定程度上防止模型過擬合。
- 允許使用各種激活函數(shù):在沒有BN之前,某些激活函數(shù)(如sigmoid和tanh)在深層網(wǎng)絡(luò)中容易導(dǎo)致梯度消失或梯度爆炸。但使用BN后,這些問題得到了緩解,因?yàn)閿?shù)據(jù)分布被標(biāo)準(zhǔn)化了。
為什么要用
BN的作用:
- 加速訓(xùn)練:BN可以使得網(wǎng)絡(luò)的訓(xùn)練速度更快。因?yàn)榻?jīng)過標(biāo)準(zhǔn)化后,權(quán)重的更新方向更加明確,可以使用更大的學(xué)習(xí)率進(jìn)行訓(xùn)練。
- 正則化效果:BN具有輕微的正則化效果,可以在一定程度上防止模型過擬合。
- 允許使用各種激活函數(shù):在沒有BN之前,某些激活函數(shù)(如sigmoid和tanh)在深層網(wǎng)絡(luò)中容易導(dǎo)致梯度消失或梯度爆炸。但使用BN后,這些問題得到了緩解,因?yàn)閿?shù)據(jù)分布被標(biāo)準(zhǔn)化了。
參考
(31條消息) BatchNorm2d原理、作用及其pytorch中BatchNorm2d函數(shù)的參數(shù)講解_LS_learner的博客-CSDN博客_batchnorm2d
(31條消息) pytorch中批量歸一化BatchNorm1d和BatchNorm2d函數(shù)_小白827的博客-CSDN博客_batchnorm1d 2d文章來源:http://www.zghlxwxcb.cn/news/detail-659137.html
BatchNorm 到底應(yīng)該怎么用? - 項(xiàng)脊軒的琵琶樹 (gitee.io)文章來源地址http://www.zghlxwxcb.cn/news/detail-659137.html
到了這里,關(guān)于深度學(xué)習(xí)Batch Normalization的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!