M3-009M3: Deep Learning FoundationsWeight InitializationEasy
⚡Flashcard Drill➔Mastery:

Xavier and Kaiming Initialization: Mathematical Variances and Suitable Activations

📐 Mathematical Definition
Xavier: Var=2fanin+fanout;Kaiming: Var=2fanin\text{Xavier}:\ \mathrm{Var}=\frac{2}{fan_{in}+fan_{out}};\qquad \text{Kaiming}:\ \mathrm{Var}=\frac{2}{fan_{in}}
⚡ One-Sentence Core Answer
Xavier sets Var(W)=2nin+nout\text{Var}(W) = \frac{2}{n_{\text{in}} + n_{\text{out}}} for linear/Tanh activations; Kaiming sets Var(W)=2nin\text{Var}(W) = \frac{2}{n_{\text{in}}} to compensate for ReLU's half-space zeroing.

📌 Key Takeaways

  • •
    Xavier (Glorot): Var(W)=2nin+nout\text{Var}(W) = \frac{2}{n_{\text{in}} + n_{\text{out}}} (or 1nin\frac{1}{n_{\text{in}}}), assumes zero-centered linear/tanh regime
  • •
    Kaiming (He): Var(W)=2nin\text{Var}(W) = \frac{2}{n_{\text{in}}}, doubles variance to counteract ReLU halving activation power
  • •
    LeakyReLU adjustment: Kaiming initialization scales variance by 21+a2\frac{2}{1 + a^2} for negative slope aa

📐 Mathematical Derivations

Mathematical Derivations:
① Xavier (Glorot & Bengio, 2010):
Let y=∑i=1ninwixiy = \sum_{i=1}^{n_{\text{in}}} w_i x_i. Assuming independent zero-mean weights and inputs: Var(y)=ninVar(W)Var(X)\text{Var}(y) = n_{\text{in}} \text{Var}(W) \text{Var}(X). To preserve forward activation variance Var(y)=Var(X)\text{Var}(y) = \text{Var}(X), we require Var(W)=1/nin\text{Var}(W) = 1/n_{\text{in}}.
To simultaneously preserve backward gradient variance: Var(W)=1/nout\text{Var}(W) = 1/n_{\text{out}}. Taking the harmonic mean yields: Var(W)=2nin+nout\text{Var}(W) = \frac{2}{n_{\text{in}} + n_{\text{out}}}, with uniform bounds W∼U(−6nin+nout,6nin+nout)W \sim U\left(-\sqrt{\frac{6}{n_{\text{in}} + n_{\text{out}}}}, \sqrt{\frac{6}{n_{\text{in}} + n_{\text{out}}}}\right).
② Kaiming / He (He et al., 2015):
For ReLU y=max⁡(0,z)y = \max(0, z), assuming symmetric distribution around 0, exactly half the inputs are set to zero: E[y2]=12E[z2]=12ninVar(W)E[x2]\mathbb{E}[y^2] = \frac{1}{2} \mathbb{E}[z^2] = \frac{1}{2} n_{\text{in}} \text{Var}(W) \mathbb{E}[x^2].
To maintain E[y2]=E[x2]\mathbb{E}[y^2] = \mathbb{E}[x^2], we must set: Var(W)=2nin\text{Var}(W) = \frac{2}{n_{\text{in}}}. Normal distribution: W∼N(0,2nin)W \sim \mathcal{N}\left(0, \sqrt{\frac{2}{n_{\text{in}}}}\right).

🏭 Production Trade-offs

Selection matrix: Use Xavier for symmetric activations with unit gradient at origin (Tanh, Sigmoid, Softsign, linear projections). Use Kaiming for rectified non-linearities (ReLU, LeakyReLU, PReLU, ELU).
⚠️ Common Interview Pitfalls
  • ✕
    Using Xavier initialization for deep ReLU networks, which leads to exponential activation decay and severe underflow in deep layers
  • ✕
    Using default standard normal initialization N(0,1)\mathcal{N}(0, 1), causing explosive gradients within the first 3 layers
🎯 Interviewer Follow-up Inquiries
  • ?
    Why does Xavier initialization fail in networks with 30+ layers when paired with ReLU activations?
  • ?
    How is ninn_{\text{in}} (fan_in) defined for 2D convolutional weight tensors of shape (Cout,Cin,Kh,Kw)(C_{\text{out}}, C_{\text{in}}, K_h, K_w)?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM3-008: Why All-Zero Weight Initialization Fails in Neural Networks📋Back to BankNext →M3-010: The 'Variance Preservation' Principle and Its Relation to Gradient Stability
中文速览与面试要点

写出 Xavier 与 Kaiming 初始化的方差,并说明适用激活

Xavier 适合 tanh/sigmoid,Kaiming 适合 ReLU(考虑半区激活)。

核心要点

  • 推导思路(方差保持):设层输出 y=Σᵢwᵢxᵢ(忽略激活),若 w 与 x 独立且零均值,则 Var(y)=fan_in·Var(w)·Var(x)。要让 Var(y)=Var(x)(方差保持),需 Var(w)=1/fan_in 。 Xavier(Glorot) 取折中:Var(w)=2/(fan_in+fan_out)——它同时考虑前向(fan_in 决定输出方差)与反向(fan_out 决定梯度方差),故在 线性激活或对称饱和激活(tanh/sigmoid) 下能同时保持前向与反向的方差。 Kaiming(He) 的修正是针对 ReLU :ReLU 把负半区置零,使输出方差减半(因为一半神经元失活),故需把方差 加倍 :Var(w)=2/fan_in。若对 ReLU 用 Xavier,方差会逐层衰减(每层减半),深层网络的信号会消失。 fan_in/fan_out 的定义 :对全连接层 fan_in=输入维、fan_out=输出维;对卷积层 fan_in=输入通道×卷积核面积、fan_out=输出通道×卷积核面积。
  • 实践要点:① 激活与初始化的配对 ——tanh/sigmoid → Xavier;ReLU/LeakyReLU → Kaiming(LeakyReLU 的负斜率 α 会影响方差:Var=2/((1+α²)fan_in));GELU/SiLU → 近似用 Kaiming(GELU 在 0 附近近似线性,故与 ReLU 相近)。② 均匀 vs 正态 ——两者可互相转换(均匀分布的方差 = (b−a)²/12);框架同时提供( xavier_uniform_ / kaiming_normal_ )。③ 偏置初始化 ——通常置零;若激活非零中心(如 ReLU),有人用小的正偏置(如 0.01)避免初始全死,但现代实践多用零(配合归一化层)。④ 与归一化层的交互 ——若层后接 BN/LN,初始化的要求被放松(归一化会重新标准化),故方差保持不再是严格必要;这也是'有 BN 的网络对初始化更宽容'的原因。⑤ Transformer 的特殊处理 ——标准 Kaiming 会导致深层 Transformer 的残差累积方差爆炸,故 GPT-2 等用 1/√(2L) 缩放残差分支的初始化 (见'Transformer 初始化技巧'题)。⑥ 诊断 ——若前向传播后激活方差逐层指数衰减(或爆炸),说明初始化不当;可在初始化后跑一次前向,打印各层激活的均值/方差确认。
  • 目标是让每层输出方差≈1
  • ReLU 使一半神经元失活,故需更大方差

常见陷阱

  • 对 ReLU 网络用 Xavier 初始化(方差逐层减半)
  • 卷积层的 fan_in 忽略卷积核面积