Xavier Initialization/zh
| Article | |
|---|---|
| Topic area | Deep Learning |
| Prerequisites | Backpropagation, Activation Function |
概述
Xavier 初始化(又稱 Glorot 初始化)是一種為深度神經網絡設置初始權重的方案,使得激活值與梯度的方差在訓練開始時大致在各層之間保持恆定。該方法由 Xavier Glorot 與 Yoshua Bengio 於 2010 年提出,至今仍是使用最廣泛的默認初始化器之一,尤其適用於具有對稱、零中心激活函數(例如雙曲正切)的網絡。[1]
其核心思想是一個簡單的方差平衡論證。如果每一層的權重都從一個其方差與該層 fan-in 和 fan-out 相校準的分佈中抽取,那麼前向激活的幅度與反向梯度的幅度在信號沿深度傳播時既不會收縮到零,也不會無限增大。這就避免了梯度消失與梯度爆炸的病態現象,正是它們使得在人們尚未理解謹慎初始化之前,訓練深度網絡變得極為困難。
動機
在 Glorot 與 Bengio 的分析問世之前,深度網絡通常以從一個較小的高斯分佈或均勻分佈中抽取的權重進行初始化,該分佈通常是手工選擇或通過試錯確定的。實踐者觀察到,使用sigmoid或雙曲正切激活的非常深的網絡幾乎無法訓練:由反向傳播計算得到的梯度,要麼在淺層衰減到零(梯度消失問題),要麼無限增長(梯度爆炸問題),這取決於權重的尺度與激活函數的選擇。諸如 tanh 之類的對稱飽和激活會進一步加劇這一問題,因為它們的導數在零附近一個狹窄區域之外迅速衰減。
Glorot 與 Bengio 分析了前向傳播中激活值方差的演化以及反向傳播中梯度方差的演化,所依據的假設是:激活函數在零附近近似線性,且權重獨立同分佈且均值為零。他們證明,要在層級鏈中同時保持前向與反向方差的穩定,每個權重的方差應與該層的 fan-in(輸入單元數)與 fan-out(輸出單元數)掛鈎。由此得到的規定即今天所稱的Xavier 初始化。
公式表述
考慮一個全連接層,它通過權重矩陣 $ W $ 將一個維度為 $ n_{\text{in}} $ 的輸入向量映射到一個維度為 $ n_{\text{out}} $ 的輸出向量。假設輸入相互獨立、均值為零且共享方差 $ \sigma_x^2 $,權重相互獨立、均值為零且方差為 $ \sigma_w^2 $,並假設激活函數在零附近近似線性且斜率為一。那麼每個輸出的方差為
$ {\displaystyle \operatorname{Var}(y) = n_{\text{in}} \, \sigma_w^2 \, \sigma_x^2.} $
為了使方差在前向傳播中得以保持,需要 $ \sigma_w^2 = 1 / n_{\text{in}} $。將對稱的論證應用於反向傳播,其中梯度被乘以權重矩陣的轉置,則得到 $ \sigma_w^2 = 1 / n_{\text{out}} $。由於這兩條要求通常彼此衝突,Glorot 與 Bengio 提出了如下調和折中
$ {\displaystyle \sigma_w^2 = \frac{2}{n_{\text{in}} + n_{\text{out}}}.} $
這就是定義 Xavier 初始化的方差。有兩種常見的參數化方式可以實現它:
- Xavier 正態:從高斯分佈 $ \mathcal{N}\!\left(0, \frac{2}{n_{\text{in}} + n_{\text{out}}}\right) $ 中獨立地抽取每個權重。
- Xavier 均勻:從 $ \mathcal{U}(-a, a) $ 中獨立地抽取每個權重,其中 $ a = \sqrt{6 / (n_{\text{in}} + n_{\text{out}})} $,以使該均勻分佈具有所規定的方差。
偏置通常初始化為零。對於卷積層,fan-in 與 fan-out 的計算方式為:輸入通道數或輸出通道數乘以感受野的大小(例如,卷積核的高度乘以其寬度)。
推導概要
該推導依賴三個近似:權重與輸入相互獨立且均值為零,激活函數在原點附近局部線性且導數為一,並且偏置為零。在這些假設下,前向方差的遞推關係為
$ {\displaystyle \operatorname{Var}(y^{(\ell)}) = n^{(\ell)}_{\text{in}} \, \sigma^{(\ell)\,2}_w \, \operatorname{Var}(y^{(\ell-1)}),} $
於是當 $ n^{(\ell)}_{\text{in}} \sigma^{(\ell)\,2}_w = 1 $ 成立時,方差在層之間得到精確保持。反向遞推具有相同的形式,但 $ n^{(\ell)}_{\text{in}} $ 被 $ n^{(\ell)}_{\text{out}} $ 替換,因為梯度通過轉置進行傳播。對這兩個條件取平均即得 Xavier 規則。Glorot 與 Bengio 通過實驗證實,按此方式初始化的網絡在深度方向上呈現出遠為穩定的激活與梯度直方圖,並且其訓練速度遠高於使用舊式啟發式 $ \mathcal{U}(-1/\sqrt{n_{\text{in}}}, 1/\sqrt{n_{\text{in}}}) $ 初始化的網絡。
與 He 初始化的比較
Xavier 初始化所依賴的線性激活假設對於原點附近的 tanh 與邏輯sigmoid是合理的,但對於修正線性單元便不再成立。由於 ReLU 會將負輸入置零,僅約一半的預激活值對輸出方差有貢獻,因此 Xavier 初始化傾向於低估 ReLU 網絡合適的權重尺度,導致激活值隨深度衰減。He 初始化由 Kaiming He 等人於 2015 年提出,它通過使用
$ {\displaystyle \sigma_w^2 = \frac{2}{n_{\text{in}}}} $
來代替,將方差加倍以補償整流器。[2] 在現代實踐中,Xavier 初始化是基於 tanh、sigmoid 與 softmax 的體系結構的默認選擇,而 He 初始化則是 ReLU 及其變體的默認選擇。兩者都是更一般的"縮放"初始化原則的特例,有時被統稱為方差保持型初始化。
實用考量
對於使用批歸一化或層歸一化的深度網絡而言,初始化器的選擇已不像過去那樣關鍵,因為歸一化層會在每次線性變換之後重新縮放激活值,從而部分吸收初始尺度的失校。儘管如此,一個合理的初始化器仍能加速訓練的最初若干 epoch,並可能成為網絡發散與收斂之間的分水嶺,尤其是在沒有歸一化的非常深的網絡中,或在非常深的殘差網絡的殘差分支中——在那裏較小的初始尺度有所裨益。
大多數深度學習框架同時內置了 Xavier 均勻與 Xavier 正態兩種初始化器,通常以 "glorot_uniform" 與 "glorot_normal" 命名(例如在 TensorFlow 與 Keras 中),或以 "xavier_uniform_" 與 "xavier_normal_" 命名(在 PyTorch 中)。PyTorch 線性層的默認初始化採用了一種與之密切相關的 He-均勻方案。在混合多種激活或與歸一化組合使用時,實踐者通常遵循的約定是:將 tanh 與 sigmoid 層與 Xavier 配對,將 ReLU 層與 He 配對,同時讓框架對歸一化層與嵌入層的默認初始化保持不變。
For recurrent neural networks, orthogonal initialization of the recurrent weight matrix is a stronger choice than Xavier, because it preserves the norm of the hidden state across time steps exactly rather than only on average. Xavier remains a reasonable choice for the input-to-hidden weights of a recurrent layer.
Limitations
Xavier initialization makes assumptions that are routinely violated in practice. It assumes the activation is linear near zero with unit slope, which excludes ReLU and its variants; it assumes inputs and weights are independent with zero mean, which is approximately but not exactly true at initialization; and it ignores the effect of biases, normalization layers, and skip connections. For networks with strong nonlinearities, it underestimates or overestimates the appropriate scale, and dedicated derivations such as He initialization or LSUV initialization are preferred.
The scheme also says nothing about the geometric structure of the weight matrix. Two networks with identical Xavier-distributed weights can still have very different conditioning of their Jacobians, and pathological eigenvalue structures can persist even when variances are well calibrated. For very deep networks, more careful initializations such as orthogonal matrices, dynamical isometry, or Fixup initialization for residual networks address the conditioning question directly.[3] Finally, the scheme is purely about the start of training; with sufficient training, modern optimizers can usually overcome moderately mis-scaled initial weights, so Xavier initialization is best understood as a robust default rather than as an optimal choice in any strict sense.