Batch Normalization

Google 於 2015 年提出了 Batch Normalization 的方法.

有一種方法是:在輸入數據時,通常都會先將 feature 做 normalize 後再進行訓練,可以加速模型收斂,而 Batch Normalization 就是指在每一層輸入都做一次 normalize


方法

在訓練神經網路時,我們通常把資料切成很多 mini-batch(小批次),BN 會針對每個 mini-batch 的每個特徵,計算其平均值與標準差,然後進行正規化:

x^=xμBσB2+ϵ

將分散的數據統一
原本可是因為不同 mini-batch 的原始數據分佈可能非常不同,會導致網路訓練不穩定 → 經過正規化後,該 mini-batch 的輸入數據會被轉換成接近 平均值為 0、標準差為 1 的分佈 → BN 將每個 batch 的數據調整到類似的標準常態分佈 → 讓後續的層更容易學習

Note

  • μB:mini-batch 的平均值
  • σB2:mini-batch 的變異數
  • ϵ:避免除以 0 的小常數


應用場景

Batch Normalization is used
when the "Error Surface (錯誤曲面) is too rugged(崎嶇)",
even if the error surface is convex, training is not easy.

Error Surface 情境.png
Fig: Left: a convex function. Right: a non-convex function.
It is much easier to find the bottom of the surface in the convex function than the non-convex surface. (Source: Reza Zadeh)


除了 Batch Normalization 之外,還有其他進階的優化方法,例如:

不過當我們用Optimizer(優化器)(例如 SGD、Adam)訓練模型時,理想情況下希望損失函數的 Error Surface (錯誤曲面) 是 Smooth(平滑) 的。

也因此 Batch Normalization 的作用 changes the landscape (使各個方向平順化) of the error surface to make it smoother in all directions.

If the Error Surface (錯誤曲面) is flat,
it is less likely to encounter the so-called Critical Point (臨界點).


Feature Scaling (特徵縮放)
Works by rescaling the input features
→ so that they have 0 mean and 1 variance.

在深度神經網路中,若網路層數較多,通常會在其中插入 Batch Normalization 層,而這個過程本質上也是一種特徵縮放。


What are the advantage after Feature Normalization?

  1. Each "dimension has a mean of 0" and a "Variance of 1"
  2. To create a smoother error surface,
  3. one approach is to generate data that follows a normal distribution statistically(常態分佈)

⇒ 獲得上述,再做梯度下降可以收斂得更快、更好

Note

若沒有特徵縮放,誤差曲面可能非常崎嶇
導致必須使用 非常小的學習率 才能避免過衝 (overshooting),
但過小的學習率又會導致收斂速度過慢,進而降低訓練效率。

Pasted image 20230328160116.png
How To Calculate the Mean and Standard Deviation
Fig: 當誤差曲面不平滑時,Learning Rate 選擇困難:
過大 → 過衝震盪;
過小 → 收斂緩慢。
Feature Scaling 可以緩解這個問題。


假設我們有一組資料 {z1,z2,...,zn},我們希望將其轉換為標準化後的 z~i

  1. 計算平均值μ=1ni=1nzi
  2. 計算標準差σ=1ni=1n(ziμ)2
  3. 進行標準化 (Feature Normalization)z~i=ziμσ

完成第一次 Feature Normalization 後,
因為每個元素在計算時會用到整個維度的 平均值標準差
因此同一維度中的所有元素會產生彼此關聯性

Screen Shot 2023-04-20 at 3.13.50 PM.png
Fig: 特徵縮放後,同一維度內的元素不再獨立,而是彼此關聯。


總結

Batch Normalization 李宏毅教授

Powered by Forestry.md