← All Posts
Deep Learning · Weight Initialization in Deep LearningChapter 02 / 2

Xavier, LeCun, and He Initialization: Count the Inputs and Outputs

Derive LeCun (fan-in), fan-out, Xavier, and He variances from forward and backward sums, convert variance into uniform bounds, and count connections in dense and convolutional layers.

Fan-based initialization sets a layer’s weight variance $s_W^2=\operatorname{Var}(W_{l,ij})$ by counting how many terms each matrix multiplication sums: LeCun preserves forward scale ($q_l=q_{l-1}$), fan-out preserves backward gradient scale ($\tilde q_{l-1}=\tilde q_l$), Xavier (Glorot) balances the two, and He (Kaiming) doubles the variance when ReLU discards the negative half.

1. Forward sums ($n_{\rm in}$) vs. backward sums ($n_{\rm out}$)

Consider layer $l$ with $z_l=W_lh_{l-1}$ and $h_l=\phi(z_l)$ in the linear or small-signal $\tanh$ regime ($\phi(z)\approx z$, $\phi'(z)\approx 1$), where $W_l\in\mathbb R^{n_{\rm out}\times n_{\rm in}}$ has independent zero-mean weights $\mathbb E[W_{l,ij}]=0$ of variance $s_W^2=\operatorname{Var}(W_{l,ij})=\mathbb E[W_{l,ij}^2]$, independent of incoming activations $a_j=h_{l-1,j}$ with second moment $q_{l-1}=\mathbb E[h_{l-1,j}^2]$:

When $n_{\rm in}=n_{\rm out}$, both rules match. When $n_{\rm in}\ne n_{\rm out}$, fan-in scaling ($s_W^2=1/n_{\rm in}$) keeps the forward multiplier $c=1$ but scales backward gradient moments by $c_{\rm back}=n_{\rm out}/n_{\rm in}$, whereas fan-out scaling ($s_W^2=1/n_{\rm out}$) keeps $c_{\rm back}=1$ but scales forward moments by $c=n_{\rm in}/n_{\rm out}$.

2. Xavier and He initialization: balancing fans and ReLU

3. Converting variance to uniform bounds: how $a=\sqrt{3}\,s_W$ translates to weight init and $\sqrt{6}$

Our signal-propagation equations in Sections 1 and 2 only depend on two numbers: $\mathbb E[W_{l,ij}]=0$ and $\operatorname{Var}(W_{l,ij})=s_W^2$. They do not require the weights to be Gaussian—we can draw $W_{l,ij}$ from either a Normal distribution or a Uniform distribution, provided both have mean $0$ and variance $s_W^2$:

MethodTarget variance $s_W^2$Normal init $\mathcal N(0,s_W^2)$ ($\operatorname{std}=s_W$)Uniform init $\operatorname{Uniform}(-a,a)$ ($a=\sqrt{3}\,s_W$)
LeCun (fan-in)$1/n_{\rm in}$$s_W=\sqrt{1/n_{\rm in}}$$a=\sqrt{3}\cdot\sqrt{1/n_{\rm in}}=\sqrt{3/n_{\rm in}}$
Xavier (Glorot)$2/(n_{\rm in}+n_{\rm out})$$s_W=\sqrt{2/(n_{\rm in}+n_{\rm out})}$$a=\sqrt{3}\cdot\sqrt{2/(n_{\rm in}+n_{\rm out})}=\sqrt{6/(n_{\rm in}+n_{\rm out})}$
Fan-out$1/n_{\rm out}$$s_W=\sqrt{1/n_{\rm out}}$$a=\sqrt{3}\cdot\sqrt{1/n_{\rm out}}=\sqrt{3/n_{\rm out}}$
He / Kaiming (ReLU)$2/n_{\rm in}$$s_W=\sqrt{2/n_{\rm in}}$$a=\sqrt{3}\cdot\sqrt{2/n_{\rm in}}=\sqrt{6/n_{\rm in}}$

4. Lab: compare expected and measured amplification

Test how $n_{\rm in}$, $n_{\rm out}$, and the variance rule affect a single rectangular linear layer:

One rectangular linear layer

Change the input and output widths and choose a rule. A sampled Gaussian weight matrix processes 256 Gaussian input vectors and backpropagates 256 independent Gaussian output directions. Bars compare the theoretical second-moment ratios with measured ratios.

5. Fan counts for dense and convolutional layers

# PyTorch Linear stores weight as [out_features, in_features]
torch.nn.init.xavier_uniform_(layer.weight, gain=1.0)
if layer.bias is not None:
    torch.nn.init.zeros_(layer.bias)

# He (Kaiming) fan-in normal initialization for a ReLU layer
torch.nn.init.kaiming_normal_(layer.weight, mode="fan_in", nonlinearity="relu")

# Explicit LeCun normal (used with SELU or forward-preserving linear layers)
torch.nn.init.normal_(layer.weight, mean=0.0, std=1.0 / math.sqrt(layer.in_features))

6. Questions worth answering

Is Xavier variance the arithmetic average of $1/n_{\rm in}$ and $1/n_{\rm out}$?

No. Xavier averages the fan counts in the denominator—$s_W^2=2/(n_{\rm in}+n_{\rm out})$—which is the harmonic mean of $1/n_{\rm in}$ and $1/n_{\rm out}$ and guarantees $\tfrac12(n_{\rm in}\,s_W^2+n_{\rm out}\,s_W^2)=1$.

Where does the $\sqrt{6}$ in Xavier and He uniform come from?

Matching $\operatorname{Var}(\operatorname{Uniform}(-a,a))=a^2/3$ to $s_W^2$ gives $a=\sqrt{3}\,s_W$. Multiplying $\sqrt{3}$ by the $\sqrt{2}$ inside $s_{W,\text{Xavier}}=\sqrt{2/(n_{\rm in}+n_{\rm out})}$ or $s_{W,\text{He}}=\sqrt{2/n_{\rm in}}$ gives $\sqrt{6}$ in the numerator.

When do you switch from Xavier to He initialization?

Xavier assumes zero-centered activations with unit slope near zero ($\phi(z)\approx z$, such as linear or small-signal $\tanh$). For ReLU, which zeros out negative inputs and halves the second moment $q_l$ per layer, use He initialization ($s_W^2=2/n_{\rm in}$).

Sources

Glorot and Bengio, Understanding the Difficulty of Training Deep Feedforward Neural Networks; He et al., Delving Deep into Rectifiers; LeCun et al., Efficient BackProp; Klambauer et al., Self-Normalizing Neural Networks; PyTorch initialization documentation.