Weight Initialization

Deriving proper initialization schemes from variance propagation analysis: Xavier/Glorot for sigmoid/tanh, He/Kaiming for ReLU, orthogonal initialization, and the relationship between initialization and gradient flow stability.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. The Variance Propagation Problem
  5. Xavier/Glorot Initialization
  6. He/Kaiming Initialization for ReLU
  7. Variance in the Backward Pass
  8. Orthogonal Initialization
  9. Initialization for Residual Networks
  10. Common Pitfalls
  11. Summary
  12. Exercises

Learning Objectives

  1. Derive the variance of layer outputs as a function of weights and inputs.
  2. Derive the Xavier condition for maintaining variance across layers.
  3. Derive the He correction for ReLU networks.
  4. Analyze backward-pass variance propagation.
  5. Explain orthogonal initialization and its spectral properties.

Notation

  • nln_l — number of neurons in layer ll (fan-in to layer l+1l+1)
  • W(l)Rnl+1×nl\mathbf{W}^{(l)} \in \mathbb{R}^{n_{l+1} \times n_l} — weight matrix at layer ll
  • h(l)Rnl\mathbf{h}^{(l)} \in \mathbb{R}^{n_l} — activations at layer ll
  • Var(hi(l))\text{Var}(h_i^{(l)}) — variance of activations
  • Var(wij(l))\text{Var}(w_{ij}^{(l)}) — variance of weight entries

Core Intuition

If weights are initialized too small, signals shrink exponentially through layers (vanishing activations). If too large, signals explode. Proper initialization maintains the variance of activations (forward pass) and gradients (backward pass) across all layers, ensuring stable training from the first iteration.

Weight Initialization

Vanishing
L1σ²=0.215L2σ²=0.483L3σ²=0.499L4σ²=0.000L5σ²=0.000
Input σ
1.00
Insight: Xavier/He init keeps activation variance stable across layers. Zero init kills all gradients; large random weights cause exploding activations by layer 5.

The Variance Propagation Problem

Consider layer ll with linear pre-activation:

zi(l)=j=1nl1wij(l)hj(l1).(1)z_i^{(l)} = \sum_{j=1}^{n_{l-1}} w_{ij}^{(l)} h_j^{(l-1)}. \tag{1}

Assuming wijw_{ij} and hjh_j are independent, zero-mean, and iid within each layer:

Var(zi(l))=nl1Var(wij(l))Var(hj(l1)).(2)\text{Var}(z_i^{(l)}) = n_{l-1} \cdot \text{Var}(w_{ij}^{(l)}) \cdot \text{Var}(h_j^{(l-1)}). \tag{2}

For the signal to neither explode nor vanish, we need:

Var(zi(l))=Var(hj(l1))\text{Var}(z_i^{(l)}) = \text{Var}(h_j^{(l-1)}) nl1Var(wij(l))=1.(3)\Rightarrow n_{l-1} \cdot \text{Var}(w_{ij}^{(l)}) = 1. \tag{3}

Xavier/Glorot Initialization

For linear or tanh activations (where h=ϕ(z)zh = \phi(z) \approx z near zero):

Forward pass condition: Var(w)=1/nin\text{Var}(w) = 1/n_{\text{in}}.

Backward pass condition (maintaining gradient variance): Var(w)=1/nout\text{Var}(w) = 1/n_{\text{out}}.

Xavier compromise (averaging both constraints):

Var(wij)=2nin+nout.(4)\boxed{\text{Var}(w_{ij}) = \frac{2}{n_{\text{in}} + n_{\text{out}}}.} \tag{4}

In practice:

  • Uniform: wU[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]
  • Gaussian: wN(0,2nin+nout)w \sim \mathcal{N}\left(0, \frac{2}{n_{\text{in}} + n_{\text{out}}}\right)

He/Kaiming Initialization for ReLU

ReLU sets half the activations to zero: E[ReLU(z)2]=12Var(z)\mathbb{E}[\text{ReLU}(z)^2] = \frac{1}{2}\text{Var}(z) (for zero-mean Gaussian zz).

Derivation. For zN(0,σ2)z \sim \mathcal{N}(0, \sigma^2):

E[ReLU(z)2]=E[z21[z>0]]=12E[z2]=σ22.(5)\mathbb{E}[\text{ReLU}(z)^2] = \mathbb{E}[z^2 \cdot \mathbf{1}[z > 0]] = \frac{1}{2}\mathbb{E}[z^2] = \frac{\sigma^2}{2}. \tag{5}

The variance halves at each ReLU. To compensate:

Var(zi(l))=nl1Var(w)12Var(z(l1)).\text{Var}(z_i^{(l)}) = n_{l-1} \cdot \text{Var}(w) \cdot \frac{1}{2}\text{Var}(z^{(l-1)}).

Setting Var(z(l))=Var(z(l1))\text{Var}(z^{(l)}) = \text{Var}(z^{(l-1)}):

Var(wij)=2nin.(6)\boxed{\text{Var}(w_{ij}) = \frac{2}{n_{\text{in}}}.} \tag{6}

This is He initialization (He et al., 2015). The factor of 2 compensates for ReLU zeroing half the distribution.


Variance in the Backward Pass

During backpropagation, gradients propagate as:

Lhj(l1)=i=1nlwij(l)Lzi(l).(7)\frac{\partial L}{\partial h_j^{(l-1)}} = \sum_{i=1}^{n_l} w_{ij}^{(l)} \frac{\partial L}{\partial z_i^{(l)}}. \tag{7}

By the same variance analysis:

Var(Lh(l1))=nlVar(w)Var(Lz(l)).(8)\text{Var}\left(\frac{\partial L}{\partial h^{(l-1)}}\right) = n_l \cdot \text{Var}(w) \cdot \text{Var}\left(\frac{\partial L}{\partial z^{(l)}}\right). \tag{8}

For gradient stability: nlVar(w)=1n_l \cdot \text{Var}(w) = 1, giving Var(w)=1/nout\text{Var}(w) = 1/n_{\text{out}}.

Combined: Xavier averages the forward (1/nin1/n_{\text{in}}) and backward (1/nout1/n_{\text{out}}) constraints.


Orthogonal Initialization

Idea: Initialize W\mathbf{W} as a (scaled) orthogonal matrix.

Construction: Generate a random Gaussian matrix, compute its QR decomposition G=QR\mathbf{G} = \mathbf{Q}\mathbf{R}, and set W=gainQ\mathbf{W} = \text{gain} \cdot \mathbf{Q}.

Property: All singular values of W\mathbf{W} equal the gain. This means:

  • Forward pass: Wh=gainh\lVert\mathbf{W}\mathbf{h}\rVert = \text{gain} \cdot \lVert\mathbf{h}\rVert (exact norm preservation).
  • Backward pass: WTg=gaing\lVert\mathbf{W}^T\mathbf{g}\rVert = \text{gain} \cdot \lVert\mathbf{g}\rVert (exact gradient norm preservation).

For gain =1= 1: perfect isometry. Prevents both explosion and vanishing for linear networks of arbitrary depth.


Initialization for Residual Networks

In a ResNet with skip connections h(l+1)=h(l)+F(h(l))\mathbf{h}^{(l+1)} = \mathbf{h}^{(l)} + F(\mathbf{h}^{(l)}), the variance accumulates:

Var(h(L))Var(h(0))+l=1LVar(F(l)).(9)\text{Var}(h^{(L)}) \approx \text{Var}(h^{(0)}) + \sum_{l=1}^L \text{Var}(F^{(l)}). \tag{9}

After LL blocks, variance grows linearly. Fixup initialization (Zhang et al., 2019) scales the residual branch by 1/L1/\sqrt{L}:

F(l)1LF(l),(10)F^{(l)} \to \frac{1}{\sqrt{L}} F^{(l)}, \tag{10}

so total variance remains 2Var(h(0))\approx 2\text{Var}(h^{(0)}) regardless of depth.


Common Pitfalls

Pitfall 1. Using Xavier initialization with ReLU networks. The factor-of-2 correction is essential; without it, activations decay as (1/2)L(1/2)^L.

Pitfall 2. Initializing all weights to zero. All neurons compute the same function → gradients are identical → symmetry never breaks. Always use random initialization.

Pitfall 3. Forgetting bias initialization. Biases are typically initialized to zero. For ReLU, a small positive bias (e.g., 0.01) can help prevent initial dead neurons, but is not standard.


Summary

  • Xavier: Var(w)=2/(nin+nout)\text{Var}(w) = 2/(n_{\text{in}} + n_{\text{out}}) — for sigmoid/tanh.
  • He: Var(w)=2/nin\text{Var}(w) = 2/n_{\text{in}} — for ReLU (compensates 50% zeroing).
  • Orthogonal: exact norm preservation via W=Q\mathbf{W} = \mathbf{Q} — strongest guarantee for linear dynamics.
  • Residual scaling: 1/L1/\sqrt{L} prevents variance growth in deep ResNets.
  • Proper initialization ensures stable gradient flow from the very first training step.

Exercises

Exercise 1. Derive equation (2) from the assumptions of independence and zero-mean.

Exercise 2. Show that E[ReLU(z)2]=σ2/2\mathbb{E}[\text{ReLU}(z)^2] = \sigma^2/2 for zN(0,σ2)z \sim \mathcal{N}(0, \sigma^2).

Exercise 3. For a linear network of depth LL with Xavier initialization, compute Var(h(L))/Var(h(0))\text{Var}(h^{(L)})/\text{Var}(h^{(0)}).

Exercise 4. Prove that an orthogonal matrix preserves norms: Qx=x\lVert\mathbf{Q}\mathbf{x}\rVert = \lVert\mathbf{x}\rVert.

Exercise 5. For a ResNet with LL blocks and residual variance σF2\sigma_F^2 per block, derive the total output variance and verify the 1/L1/\sqrt{L} scaling correction.