Xavier initialization addresses the fundamental problem of maintaining stable signal propagation through deep neural networks during both forward and backward passes. Let me walk through the mathematical derivation and reasoning in detail.
The Core Problem
Consider a neural network layer with input x ∈ ℝⁿ and weights W ∈ ℝᵐˣⁿ. The pre-activation output is z = Wx + b. For the i-th output neuron:
z_i = Σⱼ₌₁ⁿ wᵢⱼxⱼ + bᵢ
The fundamental issue is controlling the variance of z_i across layers. If this variance grows or shrinks exponentially with depth, we get exploding or vanishing gradients.
Forward Pass Analysis
Assume the inputs xⱼ are independent random variables with zero mean and variance σₓ². Also assume the weights wᵢⱼ are independent random variables with zero mean and variance σw². The bias terms are typically initialized to zero.
For the variance of z_i:
Var(z_i) = Var(Σⱼ₌₁ⁿ wᵢⱼxⱼ)
Since the weights and inputs are independent:
Var(z_i) = Σⱼ₌₁ⁿ Var(wᵢⱼxⱼ)
For independent random variables with zero means:
Var(wᵢⱼxⱼ) = E[wᵢⱼ²]E[xⱼ²] - E[wᵢⱼ]²E[xⱼ]² = E[wᵢⱼ²]E[xⱼ²] = σw²σₓ²
Therefore:
Var(z_i) = Σⱼ₌₁ⁿ σw²σₓ² = nσw²σₓ²
For the variance to remain constant across layers (Var(z_i) = σₓ²), we need:
nσw²σₓ² = σₓ²
This gives us:
σw² = 1/n
Backward Pass Analysis
During backpropagation, gradients flow backward through the network. Consider the gradient of the loss L with respect to the pre-activation z_i, denoted as ∂L/∂z_i.
The gradient with respect to the input xⱼ is:
∂L/∂xⱼ = Σᵢ₌₁ᵐ (∂L/∂z_i)(∂z_i/∂xⱼ) = Σᵢ₌₁ᵐ (∂L/∂z_i)wᵢⱼ
Following similar independence assumptions, if the gradients ∂L/∂z_i have variance σ_g² and are independent of the weights:
Var(∂L/∂xⱼ) = Σᵢ₌₁ᵐ Var((∂L/∂z_i)wᵢⱼ) = Σᵢ₌₁ᵐ σ_g²σw² = mσw²σ_g²
For gradient variance to remain constant:
mσw²σ_g² = σ_g²
This gives us:
σw² = 1/m
The Xavier Compromise
We have two conflicting requirements:
- Forward pass stability: σw² = 1/n
- Backward pass stability: σw² = 1/m
Xavier initialization resolves this by taking the harmonic mean:
σw² = 2/(n + m)
This can be derived by minimizing the geometric mean of the variance scaling factors in both directions, or by setting:
1/σw² = (1/2)(n + m)
Practical Implementation
Xavier initialization typically uses one of two distributions:
Uniform Distribution:
W ~ U(-√(6/(n+m)), √(6/(n+m)))
This comes from the fact that for a uniform distribution U(-a, a), the variance is a²/3. Setting a²/3 = 2/(n+m) gives a = √(6/(n+m)).
Normal Distribution:
W ~ N(0, 2/(n+m))
Why This Works
The Xavier initialization works because it maintains the principle of variance preservation. By keeping the variance of activations and gradients roughly constant across layers, it prevents the exponential growth or decay that leads to training difficulties.
The factor of 2 in the numerator accounts for the fact that we're balancing two competing requirements (forward and backward pass), and the denominator (n+m) represents the average of the fan-in and fan-out, providing a reasonable compromise between the two constraints.
This initialization ensures that the network starts in a regime where gradients can flow effectively in both directions, enabling stable training of deep networks with sigmoid, tanh, or similar activation functions.