He initialization is a weight initialization method that sets the weight variance to 2/n so that, in networks using ReLU, the variance of each layer's output stays the same as layers are stacked (n is the number of inputs connected to one output). It was introduced in 2015 by Kaiming He et al. in the paper that proposed PReLU (ICCV 2015). After its proposer, it is also called Kaiming initialization.
The scale of the weights before training matters more the deeper the network is. If the signal shrinks a little at each layer, by the time 30 layers are stacked both the signal and the gradient are nearly 0 (vanishing gradients); if it grows a little at each layer, it overflows (exploding gradients). Even at 0.5 per layer, multiplying 29 times gives 0.529≈1.9×10−9.
Xavier initialization (Glorot and Bengio, 2010), which had been widely used until then, set the variance assuming a linear activation function (one that passes its input through unchanged). ReLU sets every negative input to 0, so this assumption fails and the variance halves at every layer. He initialization cancels this "half" by doubling the weight variance. The paper shows that a 30-layer network stalls when trained with Xavier initialization but converges with He initialization.
Theory (how variance propagates)
This section walks through the reasoning in Section 2.2 of the paper. Write the computation of layer l as follows.
yl=Wlxl+bl,xl=ReLU(yl−1)
Here xl is the input to the layer, Wl the weights, bl the bias, and yl the output before the activation function.
If the weights have mean 0 and are mutually independent, the output variance is Var[yl]=nlVar[wl]E[xl2]. nl is the number of inputs connected to one output (for a convolution with kernel size k and c input channels, nl=k2c).
When yl−1 is distributed symmetrically around 0, ReLU zeroes out half of it, so E[xl2]=21Var[yl−1].
Combining 1 and 2 gives Var[yl]=21nlVar[wl]Var[yl−1] (Eq. (8)). Over L layers, these factors multiply (Eq. (9)).
Var[yL]=Var[y1]l=2∏L21nlVar[wl]
Making the factor 1 at every layer preserves the variance (Eq. (10)).
21nlVar[wl]=1⟹Var[wl]=nl2
So the weights are drawn from a normal distribution with mean 0 and standard deviation 2/nl, and the biases are set to 0. For example, for a 3×3 convolution with 64 input channels, nl=9×64=576 and the standard deviation is 2/576≈0.059.
The figure at the top shows the result of multiplying this factor 29 times when every layer has the same width. With He initialization (factor 1), layer 30 is still at 1×; with Xavier initialization (factor 1/2), it shrinks to about 1.9×10−9×; and with variance 4/n (factor 2), shown for comparison, it swells to about 5.4×108×.
The same reasoning applies to backpropagation (gradients flowing from the output side back toward the input side). There, instead of nl, use n^l=k2dl with the number of output channels dl, and set Var[wl]=2/n^l (Eq. (14)). The paper reports that the models in its experiments converged as long as either the forward or the backward condition was satisfied.
For PReLU and Leaky ReLU, which keep a slope a for negative inputs, the condition becomes 21(1+a2)nlVar[wl]=1. With a=0 it matches the ReLU case (2/n), and with a=1 it matches the linear case (1/n).
Comparison with other initializations
The table lines up He initialization with initializations it is often compared with, from the same angles (nin and nout are the number of inputs connected to one output and the number of outputs one input connects to).
When input and output widths are equal, the Xavier variance is 1/n, exactly half that of He initialization. This difference becomes the "1/2 per layer" gap when ReLU layers are stacked. In the paper's experiments, a 22-layer model converged with both, but the error started to fall earlier with He initialization; with a 30-layer model, only Xavier initialization stalled.
Rough guidelines for choosing:
Use He initialization for ReLU and Leaky ReLU: In PyTorch, pass the function name to nonlinearity.
Use Xavier initialization for Tanh and sigmoid: It suits functions whose output is symmetric around 0 and nearly linear near 0.
Caveats when using PyTorch
The default initialization of PyTorch's nn.Linear and nn.Conv2d is not He initialization. By default, weights are drawn from a uniform distribution over ±1/nin. The resulting variance is 1/(3nin), one sixth of He initialization. To use He initialization, initialize explicitly with nn.init.kaiming_normal_ or a similar function.
Keep the following points in mind as well.
mode chooses whether to match the forward or the backward pass: mode="fan_in" (the default) uses the number of inputs nin and preserves variance in the forward pass. mode="fan_out" uses the number of outputs and preserves gradient variance in the backward pass.
A uniform distribution works too: nn.init.kaiming_uniform_ draws from a uniform distribution over ±6/n so that the variance is 2/n.
Normalization and residual connections reduce the effect of initialization: Batch normalization re-equalizes the variance of each layer's output, so training progresses more easily even if the initialization is off. Methods such as Fixup have also been proposed for training very deep residual networks without normalization.
PyTorch example
▶Show code
python
import torch
import torch.nn as nn
# initialize the weights of conv and fully connected layers with He initialization
def init_he(module: nn.Module) -> None:
if isinstance(module, (nn.Conv2d, nn.Linear)):
nn.init.kaiming_normal_(module.weight, mode="fan_in", nonlinearity="relu")
if module.bias is not None:
nn.init.zeros_(module.bias)
model = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=3, padding=1), nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=3, padding=1), nn.ReLU(),
nn.AdaptiveAvgPool2d(1), nn.Flatten(),
nn.Linear(64, 10),
)
model.apply(init_he)
# compare how the output variance changes per layer in a 30-layer ReLU network
def output_variances(init_fn, depth=30, width=512):
torch.manual_seed(0)
x = torch.randn(1024, width)
variances = []
for _ in range(depth):
layer = nn.Linear(width, width, bias=False)
init_fn(layer.weight)
y = layer(x) # output before the activation function
variances.append(y.var().item())
x = torch.relu(y)
return variances
with torch.no_grad():
he_var = output_variances(lambda w: nn.init.kaiming_normal_(w, nonlinearity="relu"))
xavier_var = output_variances(nn.init.xavier_normal_)
# with He initialization the variance stays roughly constant across layers; with Xavier initialization it roughly halves per layer
print(f"He : layer 1 {he_var[0]:.3g}, layer 30 {he_var[-1]:.3g}")
print(f"Xavier : layer 1 {xavier_var[0]:.3g}, layer 30 {xavier_var[-1]:.3g}")
FAQ
Q. Should fan_in or fan_out be used?
A.
In most cases the default fan_in (preserving variance in the forward pass) is enough. The paper reports convergence as long as either the forward or the backward condition is satisfied. Some implementations, such as torchvision's ResNet, use fan_out for convolutional layers.
Q. Can He initialization be used with GELU or SiLU?
A.
It is widely used with them. These functions have a shape close to ReLU, so He initialization rarely goes far wrong. However, the paper's derivation assumes ReLU (and PReLU), and the variance is not guaranteed to be exactly preserved for other functions.
Q. How should biases be initialized?
A.
As in the paper, setting them to 0 is common. Even with zero biases, the weights are random, so each unit's output differs and training proceeds.