Search

Search papers and lessons

He (Kaiming) Initialization

Level 4 · 10 min read

He (Kaiming) initialization scales a ReLU layer's starting weights with variance 2 divided by its input width, compensating for the half of the signal that ReLU zeroes out.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Neural network basics
  • Linear algebra
  • Losses & regularization
  • Optimizers
  • Sign in to see your progress

TL;DR

He (Kaiming) initialization sets the random starting weights of a layer with variance 2/nin2/n_{\text{in}}, where ninn_{\text{in}} is the number of inputs to the layer. The factor of 2 is there because a ReLU throws away every negative value, which on average removes half of the signal's energy. Scaling the weights up by that amount keeps the signal from shrinking as it passes through a deep ReLU network.

The same reasoning applies to the gradients flowing backwards, so the rule can also be written with the number of outputs. In practice the input-width version is the usual default.

Where you'll see it: as the default initializer for convolutional and dense layers followed by ReLU in deep vision networks, and as the ReLU counterpart of Xavier initialization.

Intuition

Half the crowd leaves

Picture a line of people passing a message down a corridor. At each step, each person hears a number and repeats it, but anyone who hears a negative number stays silent and passes on nothing. On average half of the people stay silent at every step. If nobody compensates, the message gets quieter and quieter, and after ten steps almost nothing gets through.

ReLU does exactly this to a signal. It keeps positive values and sets negative ones to zero. Half the values disappear, but the layer's weights still act as if all of them were present. The fix is to make each active value louder before the next layer, by a factor that exactly cancels the loss. He initialization chooses that factor.

Why this matters for a deep stack

Xavier initialization was designed with symmetric activations like tanh⁡\tanh in mind, which pass both signs through. A ReLU network breaks its assumptions. The result is that with Xavier weights, a ReLU network's activations shrink by a roughly constant factor per layer. That factor is small, so after a few dozen layers the signal is effectively zero. The gradients then vanish too, which is the vanishing gradient problem in its most direct form.

flowchart LR
  A["Layer input with variance v"] --> B["Weights with variance 2 over n_in"]
  B --> C["Pre-activation variance is n_in times 2 over n_in times v, which is 2v"]
  C --> D["ReLU halves the second moment"]
  D --> E["Next layer input has second moment v"]

The math

Setting up the forward pass

Consider one fully connected layer with ninn_{\text{in}} inputs. Its pre-activation for output unit jj is

yj=∑i=1ninwji xi,y_j = \sum_{i=1}^{n_{\text{in}}} w_{ji}\, x_i,

where xix_i are the inputs and wjiw_{ji} are the weights. Assume the weights are independent of each other and of the inputs, with mean zero and variance Var(w)\mathrm{Var}(w). The inputs are treated as independent of each other and of the weights. Because the weights have zero mean, the input mean does not matter, and only the input's second moment enters.

Under these assumptions the variance of the pre-activation is

Var(yj)=nin Var(w) E[x2].\mathrm{Var}(y_j) = n_{\text{in}}\, \mathrm{Var}(w)\, \mathbb{E}[x^2].

The cross terms vanish because each product wjixiw_{ji} x_i has mean zero, and the variance of a sum of independent terms is the sum of their variances. The term E[x2]\mathbb{E}[x^2] is the second moment of the input, which is the mean of its squared values. For a zero-mean signal it equals the variance, but a ReLU output is never negative, so the second moment is the quantity to track.

The ReLU step

The inputs to this layer are the outputs of the previous ReLU, x=max⁡(0,z)x = \max(0, z), where zz is the previous layer's pre-activation. If zz is symmetric around zero, then half its mass is negative and gets set to zero. Hence

E[x2]=E ⁣[max⁡(0,z)2]=12 E[z2]=12Var(z).\mathbb{E}[x^2] = \mathbb{E}\!\left[\max(0, z)^2\right] = \frac{1}{2}\,\mathbb{E}[z^2] = \frac{1}{2}\mathrm{Var}(z).

Substituting into the first equation, the variance of the layer's pre-activation becomes

Var(y)=nin Var(w)2 Var(z).\mathrm{Var}(y) = \frac{n_{\text{in}}\, \mathrm{Var}(w)}{2}\, \mathrm{Var}(z).

Choosing the weight variance

To keep the variance from one layer to the next unchanged, we need the factor in front of Var(z)\mathrm{Var}(z) to equal 1. That gives

Var(w)=2nin.\mathrm{Var}(w) = \frac{2}{n_{\text{in}}}.

This is the He rule. The factor 2 is the reciprocal of the ReLU's halving. Over LL layers the variance of the pre-activation is multiplied by ∏ℓnin(ℓ)Var(w(ℓ))2\prod_\ell \frac{n_{\text{in}}^{(\ell)} \mathrm{Var}(w^{(\ell)})}{2}, so the rule keeps this product at 1 instead of letting it drift towards 0 or infinity.

Definition: He (Kaiming) initialization

For a layer followed by ReLU, draw each weight independently from a distribution with mean 0 and variance 2/nin2/n_{\text{in}}. A normal distribution gives He normal initialization, and a uniform distribution on [−6/nin, 6/nin ][-\sqrt{6/n_{\text{in}}},\, \sqrt{6/n_{\text{in}}}\,], which has variance 2/nin2/n_{\text{in}}, gives He uniform initialization.

The backward pass

Gradients flow backwards through the transposed weight matrices. The same argument applied to the backward pass gives a variance condition in terms of the number of outputs noutn_{\text{out}},

Var(w)=2nout.\mathrm{Var}(w) = \frac{2}{n_{\text{out}}}.

Forward and backward conditions cannot both hold exactly unless nin=noutn_{\text{in}} = n_{\text{out}}. He et al. use the forward version by default, because keeping the activations' scale stable is what matters most for the forward pass. Keras uses the fan-in version for its he_normal initializer by default, and PyTorch's kaiming_normal_ uses fan-in as well, with a mode argument to choose fan-out.

Relation to Xavier

Xavier initialization sets Var(w)=2/(nin+nout)\mathrm{Var}(w) = 2/(n_{\text{in}} + n_{\text{out}}), which averages the two conditions above. It assumes an activation with unit slope at zero, such as tanh⁡\tanh near its linear region, and does not account for the halving. ReLU needs the extra factor of 2. The forward pass depends only on the input width, so the He rule uses ninn_{\text{in}} alone.

Worked examples

Example
One layer, four inputs, by hand

Let a layer have nin=4n_{\text{in}} = 4 inputs. Suppose the previous layer's pre-activation zz has variance Var(z)=4\mathrm{Var}(z) = 4, so it has standard deviation 2. Compare two weight choices.

  1. Compute the input's second moment after ReLU: E[x2]=12Var(z)=12⋅4=2\mathbb{E}[x^2] = \frac{1}{2}\mathrm{Var}(z) = \frac{1}{2} \cdot 4 = 2.
  2. With LeCun-style weights, Var(w)=1/nin=0.25\mathrm{Var}(w) = 1/n_{\text{in}} = 0.25. The pre-activation variance is Var(y)=nin⋅Var(w)⋅E[x2]=4⋅0.25⋅2=2\mathrm{Var}(y) = n_{\text{in}} \cdot \mathrm{Var}(w) \cdot \mathbb{E}[x^2] = 4 \cdot 0.25 \cdot 2 = 2. The variance halved, from 4 to 2.
  3. With He weights, Var(w)=2/nin=0.5\mathrm{Var}(w) = 2/n_{\text{in}} = 0.5. Then Var(y)=4⋅0.5⋅2=4\mathrm{Var}(y) = 4 \cdot 0.5 \cdot 2 = 4. The variance is unchanged.
  4. Interpretation: the choice of weight variance decides whether each layer halves the signal's energy or preserves it. Over ten layers, halving every time multiplies the variance by 2−10≈0.0012^{-10} \approx 0.001, which means the standard deviation shrinks by about 3232 times. The He choice keeps it at 4 throughout.
Example
A ten-layer ReLU network, expected scale

Take a 10-layer fully connected ReLU network that is wide enough for the independence assumptions to hold. The input has unit variance. Using E[x2]=12Var(z)\mathbb{E}[x^2] = \frac12 \mathrm{Var}(z) at each layer, the expected root-mean-square (RMS) of the activations after each ReLU is as follows.

Weight varianceAfter layer 1After layer 5After layer 10
1/nin1/n_{\text{in}} (LeCun)0.710.180.03
2/nin2/n_{\text{in}} (He)1.001.001.00
  1. With variance 1/nin1/n_{\text{in}}, the pre-activation variance in layer 1 is nin⋅1nin⋅1=1n_{\text{in}} \cdot \frac{1}{n_{\text{in}}} \cdot 1 = 1, and the ReLU halves the second moment to 1/21/2, so the RMS is 1/2≈0.71\sqrt{1/2} \approx 0.71. Each further layer halves it again, so after layer kk the RMS is 2−k/22^{-k/2}: 2−5/2≈0.182^{-5/2} \approx 0.18 at layer 5 and 2−5≈0.0312^{-5} \approx 0.031 at layer 10.
  2. With He weights, layer 1 has pre-activation variance nin⋅2nin⋅1=2n_{\text{in}} \cdot \frac{2}{n_{\text{in}}} \cdot 1 = 2, and the ReLU halves the second moment back to 1. Every later layer repeats this, so the RMS stays at 1. In a real finite-width network it fluctuates around 1 rather than matching it exactly.
  3. Interpretation: at ten layers, the LeCun network carries about 3 percent of the signal's scale, and the He network carries all of it. The gradients in the early layers of the LeCun network are scaled down by the same factor on the way back, so those layers learn very slowly.

Tradeoffs and failure modes

SituationProblemBetter choice
ReLU or leaky ReLU followed by a dense or convolutional layerDefault init with variance 1/nin1/n_{\text{in}} halves the signal each layerHe initialization
tanh⁡\tanh or sigmoid activationsHe's factor of 2 overestimates the signal, so the activations saturateXavier initialization
Residual networksThe residual branch adds to the input, so variance grows with depth even under He initZero-initialize the last layer of each residual branch, or scale the branch down
Transformers with layer normalizationNormalization already rescales activations, so the init matters less for the forward passStandard init for the model family, such as a truncated normal with a small standard deviation
Very narrow layersThe approximation of many independent terms breaks downCheck the activation scale empirically at the first step of training

Failure modes to watch for. If the activations shrink or grow by a constant factor per layer, the init does not match the activation function: check whether a ReLU or a symmetric activation is used. A common bug is to apply He init to a layer whose output does not go through a ReLU, such as the final classifier, where it inflates the logits. Another is mixing fan-in and fan-out conventions between frameworks, which changes the scale for layers whose input and output widths differ.

History

In 2010 Xavier Glorot and Yoshua Bengio analysed how the scale of signals and gradients changes through a deep network and proposed the averaged rule that now carries their names. The rule worked well for tanh⁡\tanh networks.

Rectified linear units became the common activation in the following years, because they were cheap and reduced the saturation problem. Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun, at Microsoft Research, noticed in 2015 that the Xavier rule did not fit ReLU networks. Their paper, Delving Deep into Rectifiers, derived the factor of 2 and introduced the PReLU activation. They reported that a 30-layer network converged with the new initialization where the Xavier-initialized version struggled to train. The rule then became the default for ReLU networks in the major deep-learning frameworks.

Check yourself

QuestionRecall: why does a ReLU layer need a larger weight variance than a symmetric activation does?Show answer

A ReLU sets every negative input to zero, so on average it removes half of the signal's second moment. The next layer receives only half the energy. Doubling the weight variance, so that Var(w)=2/nin\mathrm{Var}(w) = 2/n_{\text{in}} instead of 1/nin1/n_{\text{in}}, compensates for this halving and keeps the variance stable from layer to layer.

QuestionComputation: a ReLU layer has nin=8n_{\text{in}} = 8 inputs, and the input signal has second moment E[x2]=3\mathbb{E}[x^2] = 3. What variance should the weights have, and what is the resulting pre-activation variance?Show answer

He initialization gives Var(w)=2/8=0.25\mathrm{Var}(w) = 2/8 = 0.25. The pre-activation variance is Var(y)=nin⋅Var(w)⋅E[x2]=8⋅0.25⋅3=6\mathrm{Var}(y) = n_{\text{in}} \cdot \mathrm{Var}(w) \cdot \mathbb{E}[x^2] = 8 \cdot 0.25 \cdot 3 = 6. This is twice the value E[x2]=3\mathbb{E}[x^2] = 3 that the next ReLU sees after halving, so the scheme is consistent: the pre-activation variance is the input's second moment multiplied by 2, and the ReLU then takes half of it back.

QuestionWhy: explain why the He rule uses only the number of inputs, while Xavier uses the average of inputs and outputs.Show answer

The He derivation tracks the forward pass, where the variance of each output depends on how many inputs are summed, ninn_{\text{in}}. Xavier averages the forward condition with the backward condition, which depends on noutn_{\text{out}}, so that both directions keep roughly the same scale. He et al. judge the forward pass to matter most for ReLU networks, so they use ninn_{\text{in}} alone, although the backward version 2/nout2/n_{\text{out}} is also valid.

QuestionApplied: you are training a 30-layer ReLU network with dense layers, and the loss does not move in the first few hundred steps. Suggest a diagnosis involving initialization.Show answer

Check the scale of the activations at each layer right after initialization. If the root-mean-square falls steadily with depth, the initialization is probably too small for ReLU, and the early layers get tiny gradients. Switching the weights to He initialization, with variance 2/nin2/n_{\text{in}}, and re-measuring the scales is the usual fix. A residual connection, or a normalization layer, can also be added if the depth must stay.

Sources