AI / ML Theory
Chapter 2 · 18 min

2.10 — Weight initialisation: Xavier/Glorot and He

Goal: Derive variance-preserving initialisations: Xavier (linear/tanh, 1/n) and He (ReLU, 2/n).

1. Why this session exists

Section 2.9 left an open question: even if the activation gg has well-behaved g′g', the matrix factors (W(ℓ+1))⊤(W^{(\ell+1)})^\top in the backprop chain can themselves rescale the gradient. To keep activations and gradients well-scaled across depth, we initialise weights with a carefully chosen variance. The two recipes are Xavier/Glorot (linear or tanh) and He (ReLU).

This is one of those moments where deep learning's empirical fix turns out to come from a one-line calculation.


2. Setup: variance propagation through a linear layer

Consider one linear layer

z=Wa+bz = W a + b

with W∈Rdout×dinW \in \mathbb{R}^{d_{\text{out}} \times d_{\text{in}}}, a∈Rdina \in \mathbb{R}^{d_{\text{in}}}, b=0b = 0 (for now). Assume entries of WW are i.i.d. with mean 00 and variance σW2\sigma_W^2, independent of aa. Then

zi=∑j=1dinWi,j aj.z_i = \sum_{j=1}^{d_{\text{in}}} W_{i, j}\, a_j.

Compute Var⁡(zi)\operatorname{Var}(z_i). With E[Wi,j]=0\mathbb{E}[W_{i, j}] = 0 and independence,

Var⁡(zi)  =  ∑jVar⁡(Wi,j aj)  =  ∑jE[Wi,j2] E[aj2]  =  din⋅σW2⋅Var⁡(a).\operatorname{Var}(z_i) \;=\; \sum_j \operatorname{Var}(W_{i, j}\, a_j) \;=\; \sum_j \mathbb{E}[W_{i, j}^2]\, \mathbb{E}[a_j^2] \;=\; d_{\text{in}} \cdot \sigma_W^2 \cdot \operatorname{Var}(a).

(With aja_j centred and i.i.d. across jj, E[aj2]=Var⁡(a)\mathbb{E}[a_j^2] = \operatorname{Var}(a).)

To preserve variance through the layer, Var⁡(z)=Var⁡(a)\operatorname{Var}(z) = \operatorname{Var}(a), we need

 σW2  =  1din. \boxed{\ \sigma_W^2 \;=\; \frac{1}{d_{\text{in}}}.\ }

This is the variance-preservation principle for forward propagation.


3. Xavier / Glorot initialisation (Glorot & Bengio, 2010)[1]

Glorot's full analysis does the same calculation in the backward pass and asks for both forward and backward variances to be preserved. Backward through the linear layer (from 2.6),

Var⁡((W⊤δ)i)=dout⋅σW2⋅Var⁡(δ).\operatorname{Var}\bigl((W^\top \delta)_i\bigr) = d_{\text{out}} \cdot \sigma_W^2 \cdot \operatorname{Var}(\delta).

To preserve Var⁡(δ)\operatorname{Var}(\delta) we need σW2=1/dout\sigma_W^2 = 1 / d_{\text{out}}. Forward wants 1/din1 / d_{\text{in}}; backward wants 1/dout1 / d_{\text{out}}. Compromise: take the harmonic mean,

 σW2  =  2din+dout(Xavier / Glorot). \boxed{\ \sigma_W^2 \;=\; \frac{2}{d_{\text{in}} + d_{\text{out}}} \quad\text{(Xavier / Glorot)}.\ }

In practice we sample Wi,j∼N(0,σW2)W_{i, j} \sim \mathcal{N}(0, \sigma_W^2) or, equivalently variance-wise, Wi,j∼U(−r,r)W_{i, j} \sim \mathcal{U}(-r, r) with r=6/(din+dout)r = \sqrt{6 / (d_{\text{in}} + d_{\text{out}})}.

This was the right choice when the dominant activation was tanh, which has tanh⁡′(0)=1\tanh'(0) = 1 — so the activation doesn't change the variance budget. With sigmoid hidden layers, Xavier was already a marked improvement on the previous "random scale, hope" approach.


4. He initialisation (He et al., 2015)[2]

For ReLU activations the calculation needs one more step. Recall (2.3, 2.9) that ReLU⁡′(z)∈{0,1}\operatorname{ReLU}'(z) \in \{0, 1\} and that for a symmetrically distributed zz, ReLU⁡(z)=0\operatorname{ReLU}(z) = 0 half the time. Plugging this into the variance propagation:

Var⁡(ReLU⁡(z))=12 Var⁡(z),\operatorname{Var}\bigl(\operatorname{ReLU}(z)\bigr) = \tfrac{1}{2}\, \operatorname{Var}(z),

assuming a centred symmetric zz. So passing through a ReLU halves the variance. To compensate, the next layer's weight matrix should double the variance budget:

 σW2  =  2din(He / Kaiming, for ReLU). \boxed{\ \sigma_W^2 \;=\; \frac{2}{d_{\text{in}}} \quad\text{(He / Kaiming, for ReLU)}.\ }

This is the He (or Kaiming) initialisation — the default for ReLU networks today (nn.init.kaiming_normal_ in PyTorch).

Why this matters concretely. Without the 2/din2 / d_{\text{in}} correction, ReLU networks see their activation variance halve at every layer; after L=20L = 20 layers, the typical activation magnitude has shrunk by 2−10≈10−32^{-10} \approx 10^{-3}, and the network is effectively dead. He init keeps the activation variance constant across depth.


5. Generalisations

Two natural extensions.

Generic activation gg with derivative-magnitude η:=E[(g′(Z))2]\eta := \sqrt{\mathbb{E}[(g'(Z))^2]} at the initialisation scale. The compensating variance is

σW2=1η2⋅din.\sigma_W^2 = \frac{1}{\eta^2 \cdot d_{\text{in}}}.

Plugging η2=1/2\eta^2 = 1/2 (ReLU half-on) gives 2/din2 / d_{\text{in}}, recovering He. Plugging η=1\eta = 1 (tanh near zero) gives 1/din1 / d_{\text{in}}, recovering Xavier-forward.

Layer-wise scale rescue. Modern alternatives like LSUV (Mishkin & Matas, 2016) iteratively rescale each layer's weights so that the actual variance of z(ℓ)z^{(\ell)} at initialisation matches a target value, measured on a real batch. More accurate than the closed-form rule, more annoying to implement.


6. Biases

Biases are usually initialised to zero. There is no variance to preserve — biases shift means, not variances. Exception: the bias of the output logit for a binary classifier on imbalanced data is sometimes initialised to logit⁡(P(y=1))\operatorname{logit}(\mathbb{P}(y = 1)) so that the network's prior prediction at step 0 matches the class frequency.


7. A common pitfall

Many older codebases (and CV pipelines from the early 2010s) use the forward-only Xavier variance Var⁡(w(ℓ))=1/dℓ−1\operatorname{Var}(w^{(\ell)}) = 1 / d_{\ell - 1} across the board — tanh or ReLU, indifferently. For tanh-activated networks this is fine. For ReLU networks it under-scales the weights by a factor of 22 in variance, and the activation variance halves at every layer (§4). The fix is one line: switch the scaling factor from 1/din1/d_{\text{in}} to 2/din2/d_{\text{in}} (or call nn.init.kaiming_normal_ in PyTorch) and trainability improves immediately. A small but real example of how the theory keeps catching up to the practice.


8. Takeaways

  • Forward-variance preservation: σW2=1/din\sigma_W^2 = 1 / d_{\text{in}}.
  • Backward-variance preservation: σW2=1/dout\sigma_W^2 = 1 / d_{\text{out}}.
  • Xavier (compromise): σW2=2/(din+dout)\sigma_W^2 = 2 / (d_{\text{in}} + d_{\text{out}}). Use with linear or tanh.
  • He (ReLU correction): σW2=2/din\sigma_W^2 = 2 / d_{\text{in}}. Use with ReLU.
  • Biases initialise to zero unless you have prior class-imbalance information.

Self-test

(a) Verify the §2 variance computation step by step, being explicit about which independence and centring assumptions you used.

(b) Derive the ReLU compensation factor of 22 in §4 by computing E[ReLU⁡(Z)2]\mathbb{E}[\operatorname{ReLU}(Z)^2] for Z∼N(0,σ2)Z \sim \mathcal{N}(0, \sigma^2).

(c) For a 10-layer ReLU MLP with widths all equal to d=256d = 256, compute the expected magnitude of ∥a(10)∥\lVert a^{(10)} \rVert relative to ∥a(0)∥\lVert a^{(0)} \rVert under (i) Xavier init, (ii) He init.

(d) Why don't we just initialise all weights to 00? (Hint: think about what happens to the symmetry between units in the same layer when every weight is identical and the gradient is also identical.)


References

  1. [1]

    Glorot, X., & Bengio, Y. (2010). "Understanding the difficulty of training deep feedforward neural networks". AISTATS. — Xavier/Glorot initialisation (2.10).

    ↩
  2. [2]

    He, K., Zhang, X., Ren, S., & Sun, J. (2015). "Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification". ICCV. arXiv:1502.01852 — He / Kaiming initialisation (2.10).

    ↩