Xavier initialization, also called Glorot initialization, is a weight initialization scheme for neural networks that draws each layer’s starting weights from a distribution with variance 2 / (fan_in + fan_out), where fan-in and fan-out are the numbers of input and output connections of the layer. The goal is to keep the variance of activations and gradients roughly constant from layer to layer at the start of training.
The method was proposed by Xavier Glorot and Yoshua Bengio in 2010 as an answer to the difficulty of training deep networks with sigmoid and tanh activations. It remains the default initializer for many fully connected and recurrent layers in modern frameworks, although He initialization has replaced it for networks built on ReLU.
How Xavier initialization keeps signals stable
Xavier initialization works by matching the weight variance to the layer width so that a signal neither shrinks nor grows as it passes forward and backward through the network. Each output of a dense layer is a sum of fan-in weighted inputs, so its variance equals fan_in times the weight variance times the input variance, assuming independent, zero-mean inputs and weights.
To preserve variance in the forward pass, the weight variance must equal 1 / fan_in. To preserve the variance of gradients in the backward pass, it must equal 1 / fan_out. Glorot and Bengio chose the harmonic compromise 2 / (fan_in + fan_out), which satisfies both conditions exactly when the two widths are equal and approximately otherwise.
The derivation assumes an activation that is approximately linear around zero with unit slope, which holds for tanh and the softsign function near the origin. Under this assumption, the network behaves like a stack of linear maps at initialization, and the variance arguments apply layer by layer.
Uniform and normal variants
Xavier initialization comes in a uniform and a normal variant with the same target variance. Both are available in mainstream deep learning frameworks under names such as xavier_uniform_, xavier_normal_, glorot_uniform and glorot_normal.
The table gives the sampling rule and typical use of each variant.
| Variant | Sampling rule | Typical use |
|---|---|---|
| Xavier uniform | U(-a, a) with a = sqrt(6 / (fan_in + fan_out)) | Default dense layers in Keras |
| Xavier normal | N(0, sigma squared) with sigma = sqrt(2 / (fan_in + fan_out)) | When a Gaussian start is preferred |
| Truncated normal | Normal with values beyond two sigma redrawn | Avoids rare extreme weights |
| Gain-scaled Xavier | Variance multiplied by a gain squared | Adjusts for a chosen activation |
For convolutional layers, fan-in equals input channels times kernel height times kernel width, and fan-out equals output channels times the same kernel area. A 3 x 3 convolution from 64 to 128 channels therefore has fan-in 576 and fan-out 1,152, giving a weight standard deviation of about 0.034 under the normal variant.
Frameworks also expose a gain factor to correct for activations that are not unit-slope. The recommended gain for tanh is 5/3, which compensates for the slight shrinking effect of the function away from zero, while the gain for a linear or sigmoid layer is usually left at 1.
Xavier compared with other initialization schemes
Xavier initialization is one member of a family of variance-scaling initializers that differ mainly in which activation they assume. Choosing the wrong member does not always prevent training, but it slows convergence and can cause vanishing or exploding gradients in deep stacks.
The following comparison shows how the main schemes relate.
| Scheme | Weight variance | Designed for |
|---|---|---|
| Xavier (Glorot) | 2 / (fan_in + fan_out) | Tanh, sigmoid, linear layers |
| He (Kaiming) | 2 / fan_in | ReLU and its variants |
| LeCun | 1 / fan_in | SELU and self-normalizing networks |
| Orthogonal | Orthogonal matrix times a gain | Recurrent weight matrices |
| Small constant scale | Fixed, such as 0.02 squared | Large transformer language models |
The factor of two in He initialization exists because ReLU sets roughly half of its inputs to zero, halving the variance at each layer. Applied to a deep ReLU network, Xavier initialization lets activations shrink by about half per layer, which becomes severe beyond 20 or 30 layers without normalization.
Choosing Xavier initialization in practice
Xavier initialization is the right default when activations are symmetric around zero and roughly linear near the origin. In modern architectures, normalization layers and residual connections reduce, but do not remove, the influence of the initial scale.
| Layer or setting | Recommended initializer | Reason |
|---|---|---|
| Tanh or sigmoid hidden layers | Xavier with suitable gain | Matches the derivation assumptions |
| ReLU, Leaky ReLU, GELU networks | He initialization | Compensates for half-zeroed outputs |
| Attention projections in transformers | Xavier uniform or small normal | Common in reference implementations |
| Final linear output layer | Xavier or small scale | Keeps initial logits moderate |
| LSTM and GRU input weights | Xavier uniform | Gates use sigmoid and tanh |
Biases are usually initialized to zero under any of these schemes. A practical check after initialization is to pass a batch through the untrained network and confirm that activation standard deviations stay within an order of magnitude across layers.
Limitations and common mistakes
Xavier initialization guarantees only a good starting point, not stable training throughout. Its variance argument holds at step zero and relies on independence assumptions that weaken once the weights start to learn.
- Using it with deep ReLU stacks: signals decay with depth; He initialization fits better.
- Wrong fan computation: forgetting the kernel area in convolutions inflates the weight scale.
- Ignoring the gain: tanh layers without gain 5/3 start slightly under-scaled.
- Assuming it fixes everything: very deep networks still need normalization or residual connections.
- Initializing embeddings the same way: large vocabularies give an extremely small Xavier scale for embedding tables.
Another misconception is that the uniform and normal variants behave differently in a meaningful way. With the same variance, their effect on training is usually indistinguishable, and the choice is mostly a matter of convention.
In everyday language
A neural network starts with random numbers as its weights. If those numbers are too big, the signal gets louder at every layer until it blows up. If they are too small, it fades to nothing.
Xavier initialization picks the size of the random numbers carefully. It looks at how many connections go into and out of each layer. Then it chooses a scale that keeps the signal at about the same strength. It is like setting the volume on a chain of speakers so the music reaches the last room clearly.
Seen in the real world
A team training a six-layer tanh network to predict sensor faults in industrial pumps first initializes all weights from a normal distribution with a standard deviation of 1. The hidden layers have 256 units each. After the first forward pass, most tanh units are saturated at plus or minus one, gradients are close to zero and the training loss barely moves for several epochs.
The engineers switch to Xavier uniform initialization with gain 5/3, which gives a weight bound of about 0.18 for the 256 by 256 layers. Activation spread now stays similar from the first to the last layer, and the loss starts falling from the first epoch.
The model reaches its target validation accuracy in about a third of the epochs needed before. When the team later replaces tanh with ReLU, they also switch to He initialization to keep the same stable behavior.