M3-009M3: Deep Learning FoundationsWeight InitializationEasy
⚡Flashcard Drill➔Mastery:
Xavier and Kaiming Initialization: Mathematical Variances and Suitable Activations
📐 Mathematical Definition
⚡ One-Sentence Core Answer
Xavier sets for linear/Tanh activations; Kaiming sets to compensate for ReLU's half-space zeroing.
📌 Key Takeaways
- •Xavier (Glorot): (or ), assumes zero-centered linear/tanh regime
- •Kaiming (He): , doubles variance to counteract ReLU halving activation power
- •LeakyReLU adjustment: Kaiming initialization scales variance by for negative slope
📐 Mathematical Derivations
Mathematical Derivations:
① Xavier (Glorot & Bengio, 2010):
Let . Assuming independent zero-mean weights and inputs: . To preserve forward activation variance , we require .
To simultaneously preserve backward gradient variance: . Taking the harmonic mean yields: , with uniform bounds .
② Kaiming / He (He et al., 2015):
For ReLU , assuming symmetric distribution around 0, exactly half the inputs are set to zero: .
To maintain , we must set: . Normal distribution: .
① Xavier (Glorot & Bengio, 2010):
Let . Assuming independent zero-mean weights and inputs: . To preserve forward activation variance , we require .
To simultaneously preserve backward gradient variance: . Taking the harmonic mean yields: , with uniform bounds .
② Kaiming / He (He et al., 2015):
For ReLU , assuming symmetric distribution around 0, exactly half the inputs are set to zero: .
To maintain , we must set: . Normal distribution: .
🏭 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 , 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 (fan_in) defined for 2D convolutional weight tensors of shape ?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
写出 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 忽略卷积核面积