Visualized ML
Topics

B · Neural networks

Weight Initialization

Pick the starting weights' size so the signal keeps its strength through every layer: too small and it fades, too large and it explodes.

Try it: B5 · Initialization

Intuition

Before training starts, every weight needs a value. Zero will not do: every neuron in a layer would compute the same thing, get the same gradient and stay identical forever, so the layer would behave like a single neuron. Random values break that symmetry. The question is how large they should be.

Each layer multiplies its input by a weight matrix. If the weights are a little too small, the signal shrinks a little at every layer; after twenty layers it has all but vanished, and so has the gradient that flows back. If they are a little too large, the signal grows at every layer until it explodes, or until a squashing activation like tanh saturates at ±1 and its slope, and with it the gradient, drops to zero. The goal of a good initialization is a signal that keeps roughly the same size through every layer.

Random inputs pass through six tanh layers. Each histogram shows one layer's activations, with their standard deviation σ above. Too small: they collapse toward 0. Xavier: they keep a healthy spread. Too large: they pile up at −1 and 1, where tanh is flat and gradients vanish.

The math

Take one neuron with \(n\) inputs, \(z = \sum_{i=1}^{n} w_i x_i\). Assume the weights and inputs are independent, with mean 0. Variances of independent products multiply and variances of sums add, so

\[ \operatorname{Var}(z) = n \,\operatorname{Var}(w)\,\operatorname{Var}(x) \]

The signal keeps its size when \(n\operatorname{Var}(w) = 1\), that is, when the weights' standard deviation is \(1/\sqrt{n}\). Any other factor compounds: after \(L\) layers the variance is multiplied by \((n\operatorname{Var}(w))^L\).

Xavier (Glorot)

For tanh, which is close to the identity near 0, keeping the forward signal steady needs \(\operatorname{Var}(w) = 1/n_{\text{in}}\), and keeping the backward gradient steady needs \(1/n_{\text{out}}\). Xavier initialization compromises:

\[ \operatorname{Var}(w) = \frac{2}{n_{\text{in}} + n_{\text{out}}} \]

He (Kaiming)

ReLU sets half its inputs to 0, which halves the variance at every layer. He initialization doubles the weights' variance to compensate:

\[ \operatorname{Var}(w) = \frac{2}{n_{\text{in}}} \]

Either can be drawn from a normal distribution or from a uniform one with the same variance (\(U(-a, a)\) has variance \(a^2/3\)). Biases usually start at 0.

Worked example. Layers of \(n = 100\) linear neurons, input variance 1. The variance after each layer is multiplied by \(100\,\sigma_w^2\).

\(\sigma_w = 0.05\): factor 0.25. After 5 layers the variance is \(0.25^5 \approx 0.001\).

\(\sigma_w = 0.1 = 1/\sqrt{100}\): factor 1. The variance stays 1 at every depth.

\(\sigma_w = 0.1414\): factor 2. After 10 layers it is \(2^{10} = 1024\). The difference between a working and a broken network is a factor of \(\sqrt 2\) in the weights.

Variants

  • Orthogonal initialization uses a random orthogonal matrix, which preserves the length of every vector exactly, not just on average. Popular for recurrent networks, which apply the same matrix many times.
  • LeCun initialization (\(\operatorname{Var}(w) = 1/n_{\text{in}}\)) is the original rule, from 1998, and the one SELU activations are designed for.
  • Residual networks often start the last layer of each residual branch at 0 (or scale it down), so every block begins as the identity and the deep network starts out behaving like a shallow one.
  • Normalization layers (batch norm, layer norm) rescale activations during training, which makes networks far less sensitive to the initial scale, though not to symmetry.
  • Pretrained weights are the most effective initialization of all: start from a network already trained on a related task and fine-tune it.

Pitfalls

  • Constant initialization. All zeros, or all the same value, never breaks symmetry, however long you train.
  • Mismatched rule. Xavier with ReLU loses half the variance per layer; in a deep network that is enough to stall training. Match the rule to the activation.
  • Forgetting the fan. A convolution's \(n_{\text{in}}\) is channels × kernel height × kernel width, not the number of channels.
  • Looking only at the loss. A bad initialization can look like a bad learning rate. Plot the activations' spread per layer, as above, before blaming the optimizer.

History

Yann LeCun and colleagues gave the \(1/n\) rule in "Efficient BackProp" (1998). Xavier Glorot and Yoshua Bengio's 2010 paper measured activations layer by layer, as in the animation, and showed why deep networks had been so hard to train. Kaiming He and colleagues derived the ReLU correction in 2015, which let them train a 30-layer network from scratch and reported the first ImageNet result to beat human-level accuracy.

Connections

Builds on

Further reading