Search

Search papers and lessons

Xavier (Glorot) Initialization

Level 4 · 9 min read

Xavier (Glorot) initialization draws a layer's starting weights with variance 2 divided by its input width plus output width, so signals keep their size as they pass through the network.

On this page

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

TL;DR

Xavier (Glorot) initialization chooses the random starting weights of a layer so that the signal coming out of it has about the same spread as the signal going in. Its size depends on how many inputs and outputs the layer has. Without this care, a deep network's signals shrink toward zero or blow up before training has even begun.

The same care applies to gradients flowing backwards, which is why the rule balances two requirements instead of meeting one.

Where you'll see it: as the default weight initializer for dense layers in Keras, and as the baseline that He initialization modifies for ReLU networks.

Intuition

A chain of amplifiers

Picture a chain of ten amplifiers. Each one multiplies the signal's size by some fixed factor. If the factor is 0.5, the output is about a thousandth of the input after ten stages. If the factor is 2, the output is about a thousand times bigger. Only a factor very close to 1 keeps the signal usable across the whole chain.

A neural network is that chain. Each layer multiplies the signal by a matrix of weights. At the start of training the weights are random, so the question is how big the random numbers should be. Too small, and the signal fades with depth. Too large, and it explodes. Xavier initialization picks the size so that the amplification per layer is close to 1. The same argument applies to the gradients that Backpropagation sends backwards, since they pass through the same chain in reverse.

This matters because a faded signal means the early layers receive almost no learning signal, which is the vanishing gradient problem appearing before training starts.

−3−2−10123−1−0.500.51pre-activation zoutput
  • tanh(x)
  • y = x
Near zero, tanh is almost the straight line y = x. Xavier initialization keeps most pre-activations in this near-linear band, so each layer roughly passes the signal through with the same size.

Why two numbers, not one

A layer has a fan-in (the number of inputs, ninn_{\text{in}}) and a fan-out (the number of outputs, noutn_{\text{out}}). Forward signals are summed over the fan-in. Backward gradients are summed over the fan-out. A rule that looks only at one direction would be wrong for the other, and the Glorot choice is a compromise that keeps both roughly balanced when the layer is not square.

The math

Setup

Consider one linear layer with no bias:

yi=∑j=1ninWij xj,i=1,…,nout.y_i = \sum_{j=1}^{n_{\text{in}}} W_{ij}\, x_j, \qquad i = 1, \dots, n_{\text{out}}.

Here xjx_j is the jj-th input, yiy_i is the ii-th output, and WijW_{ij} is the weight connecting them. The weights are drawn independently with mean zero and the same variance Var(W)\mathrm{Var}(W). The inputs are assumed to have mean zero and to be independent of the weights. Those assumptions are only approximately true at initialization (inputs to later layers are not exactly zero-mean), but the derivation below depends on them.

Forward: variance grows by ninn_{\text{in}}

Each term WijxjW_{ij} x_j has mean zero, and the terms are independent, so their variances add. For one term, Var(Wijxj)=Var(W) Var(x)\mathrm{Var}(W_{ij} x_j) = \mathrm{Var}(W)\,\mathrm{Var}(x) because both factors have mean zero. Summing ninn_{\text{in}} of them gives

Var(yi)=nin Var(W) Var(x).\mathrm{Var}(y_i) = n_{\text{in}} \, \mathrm{Var}(W) \, \mathrm{Var}(x).

The factor nin Var(W)n_{\text{in}}\,\mathrm{Var}(W) is the variance gain of the layer. To keep the forward signal the same size, it should equal 1:

nin Var(W)=1.n_{\text{in}} \, \mathrm{Var}(W) = 1.

Backward: variance grows by noutn_{\text{out}}

Let L\mathcal{L} be the loss. Each input xjx_j feeds into all noutn_{\text{out}} outputs, so by the chain rule

∂L∂xj=∑i=1noutWij ∂L∂yi.\frac{\partial \mathcal{L}}{\partial x_j} = \sum_{i=1}^{n_{\text{out}}} W_{ij}\, \frac{\partial \mathcal{L}}{\partial y_i}.

The same independence argument gives Var(∂L/∂xj)=noutVar(W)Var(∂L/∂y)\mathrm{Var}\left(\partial \mathcal{L}/\partial x_j\right) = n_{\text{out}} \mathrm{Var}(W) \mathrm{Var}\left(\partial \mathcal{L}/\partial y\right). Keeping the gradient size constant needs

nout Var(W)=1.n_{\text{out}} \, \mathrm{Var}(W) = 1.

The compromise

The two conditions agree only when nin=noutn_{\text{in}} = n_{\text{out}}. For other shapes, Glorot and Bengio chose the harmonic mean of the two target variances, 1/nin1/n_{\text{in}} and 1/nout1/n_{\text{out}}:

Var(W)=2nin+nout.\mathrm{Var}(W) = \frac{2}{n_{\text{in}} + n_{\text{out}}}.

This gives forward gain nin Var(W)n_{\text{in}}\,\mathrm{Var}(W) and backward gain nout Var(W)n_{\text{out}}\,\mathrm{Var}(W) that are 2nin/(nin+nout)2n_{\text{in}}/(n_{\text{in}}+n_{\text{out}}) and 2nout/(nin+nout)2n_{\text{out}}/(n_{\text{in}}+n_{\text{out}}). They average to 1, and both equal 1 for a square layer.

The weights can be drawn from either of two distributions with this variance:

  • Normal: Wij∼N ⁣(0,2nin+nout)W_{ij} \sim \mathcal{N}\!\left(0, \frac{2}{n_{\text{in}}+n_{\text{out}}}\right).
  • Uniform: Wij∼U(−a,a)W_{ij} \sim U(-a, a) with a=6nin+nouta = \sqrt{\frac{6}{n_{\text{in}}+n_{\text{out}}}}. This works because a uniform variable on (−a,a)(-a, a) has variance a2/3a^2/3, so a2/3=2/(nin+nout)a^2/3 = 2/(n_{\text{in}}+n_{\text{out}}).

What the rule assumes

The derivation treats each layer as linear. Activations such as tanh are close to linear near zero, with slope about 1, so the rule works well for them as long as pre-activations stay in that band. It does not hold for a ReLU, which sets half its inputs to zero and so halves the forward variance. That is the reason for He initialization.

Worked examples

Example
Sizing one layer

A dense layer maps 256 inputs to 128 outputs. What should the weights look like?

  1. Fan-in plus fan-out is 256+128=384256 + 128 = 384.
  2. Variance is Var(W)=2/384=1/192≈0.00521\mathrm{Var}(W) = 2/384 = 1/192 \approx 0.00521.
  3. Standard deviation is 0.00521≈0.0722\sqrt{0.00521} \approx 0.0722, so a normal initializer uses σ≈0.072\sigma \approx 0.072.
  4. For the uniform version, a=6/384=1/64=0.125a = \sqrt{6/384} = \sqrt{1/64} = 0.125. So each weight is drawn from U(−0.125,0.125)U(-0.125, 0.125).
  5. Interpretation: each output sums 256 terms of size about 0.07, so the sum has standard deviation about 256×0.072×σx=1.15 σx\sqrt{256} \times 0.072 \times \sigma_x = 1.15\,\sigma_x when the inputs have standard deviation σx\sigma_x. The output is close in size to the input.
Example
Depth, with and without the rule

Take a linear network of 3 layers, each 100 wide, and an input with standard deviation 1. Compare two initializers.

Naive: W∼N(0,0.012)W \sim \mathcal{N}(0, 0.01^2), so Var(W)=0.0001\mathrm{Var}(W) = 0.0001.

  1. Forward gain per layer is n Var(W)=100×0.0001=0.01n\,\mathrm{Var}(W) = 100 \times 0.0001 = 0.01.
  2. The standard deviation is multiplied by 0.01=0.1\sqrt{0.01} = 0.1 per layer.
  3. After 3 layers the output standard deviation is 0.13=0.0010.1^3 = 0.001.

Xavier: Var(W)=2/(100+100)=0.01\mathrm{Var}(W) = 2/(100+100) = 0.01.

  1. Forward gain per layer is 100×0.01=1100 \times 0.01 = 1.
  2. The standard deviation is multiplied by 1 per layer.
  3. After 3 layers the output standard deviation is still 1.

Six layers of the naive version already give 0.16=10−60.1^6 = 10^{-6}, while Xavier stays at 1.

Example
A classifier head with unequal fan-in and fan-out

A final layer maps 100 features to 10 classes. Compute the two gains with the compromise.

  1. Var(W)=2/(100+10)=2/110≈0.01818\mathrm{Var}(W) = 2/(100 + 10) = 2/110 \approx 0.01818.
  2. Forward gain: nin Var(W)=100×0.01818≈1.82n_{\text{in}}\,\mathrm{Var}(W) = 100 \times 0.01818 \approx 1.82. The forward signal's variance grows by about 1.8 times.
  3. Backward gain: nout Var(W)=10×0.01818≈0.18n_{\text{out}}\,\mathrm{Var}(W) = 10 \times 0.01818 \approx 0.18. The gradient's variance shrinks to about a fifth.
  4. Neither gain is exactly 1. The compromise trades a little drift in each direction for one fixed formula. Because this shrink happens only once, at the last layer, it costs a constant factor rather than compounding with depth.

Tradeoffs and failure modes

Xavier initialization is a simple, principled default, and it has clear limits.

SituationProblemBetter choice
ReLU or leaky ReLU networkReLU zeroes half its inputs, so forward variance halves each layer, and the signal fades with depth.[[he-initialization
Sigmoid networkSigmoid outputs are centred at 0.5, not 0, so the zero-mean assumption fails. Its slope is at most 0.25, so gradients shrink anyway.Use a scheme designed for the nonlinearity, and rely on [[batch-normalization
Very deep network with normalizationNormalization layers rescale activations after each block, so the exact starting scale matters less.Xavier is still a good default. The choice is less critical than it was.
Large weights during trainingTraining can push pre-activations into the flat parts of tanh, where its slope is near 0.Keep an eye on activation statistics, and consider [[layer-normalization
Convolutional layersFan-in is not a plain number. It is channels times kernel height times kernel width.Use the same formula with that fan-in, and fan-out multiplied by the kernel area.

Xavier is also only a starting point. It sets the scale at time zero and says nothing about what happens during training, when the weights move far from their initial values.

History

In the mid-2000s, deep networks were hard to train from random starts. Hinton and his collaborators showed that unsupervised pretraining layer by layer could make deep networks work, and this was the workaround of the time.

In 2010, Xavier Glorot and Yoshua Bengio published a study of why deep networks with sigmoid and tanh units trained badly. They found that with the common initialization, the top hidden layers saturated quickly and the signal reaching lower layers became very small. They proposed the variance rule above, which keeps forward and backward signals balanced. The method is named after the first author's first name, Xavier, and is also called Glorot initialization. Together with better activations and later techniques, it helped make layer-wise pretraining unnecessary for many networks.

Five years later, Kaiming He and colleagues showed that ReLU networks needed a factor of 2 on the fan-in, which led to He initialization. Xavier initialization remains the standard choice for layers followed by tanh or linear activations.

Check yourself

QuestionA linear layer has 50 inputs and 50 outputs. What variance should Xavier initialization give its weights, and what is the forward gain?Show answer

Var(W)=2/(50+50)=0.02\mathrm{Var}(W) = 2/(50 + 50) = 0.02. Since the fan-in and fan-out are equal, the forward and backward gains are both 50×0.02=150 \times 0.02 = 1, so neither signal grows nor shrinks through the layer.

QuestionWhy is the variance of the weights divided by the sum of fan-in and fan-out, rather than by fan-in alone?Show answer

Forward signals are summed over the fan-in, which asks for Var(W)=1/nin\mathrm{Var}(W) = 1/n_{\text{in}}. Gradients are summed over the fan-out, which asks for Var(W)=1/nout\mathrm{Var}(W) = 1/n_{\text{out}}. The two agree only for square layers. Glorot and Bengio used 2/(nin+nout)2/(n_{\text{in}}+n_{\text{out}}) as a compromise so that neither direction is badly off for a non-square layer.

QuestionA 4-layer network with 200 units per layer uses weights drawn from N(0,0.052)\mathcal{N}(0, 0.05^2) and linear activations. Roughly how much does the signal's standard deviation change from input to output?Show answer

Each layer has gain 200×0.0025=0.5200 \times 0.0025 = 0.5 in variance, so the standard deviation is multiplied by 0.5≈0.707\sqrt{0.5} \approx 0.707 per layer. Over 4 layers the factor is 0.7074=0.250.707^4 = 0.25. The output standard deviation is about a quarter of the input's. Xavier would give Var(W)=2/400=0.005\mathrm{Var}(W) = 2/400 = 0.005 and a gain of 1 per layer.

QuestionYou switch a deep network from tanh to ReLU and keep Xavier initialization. Training stalls in the early layers. What is the likely cause, and what would you change?Show answer

A ReLU sets every negative pre-activation to zero, which removes about half the signal's energy. Under the zero-mean assumption, the forward variance is halved at each layer, so the signal and gradients fade with depth. The fix is He initialization, Var(W)=2/nin\mathrm{Var}(W) = 2/n_{\text{in}}, which doubles the forward gain to compensate for the zeroed half.

Sources