๐ Weight Initialization
Descriptionโ
< What is it? >โ
Weight initialization is the choice of the values a network's parameters start at, before the first forward pass. Biases are almost always started at zero; the weights cannot be, and the distribution they are drawn from decides whether the very first gradients are usable.
The reason it matters is the same repeated multiplication that drives Vanishing & Exploding Gradients. A signal passing through layers is multiplied by weight matrices on the way forward and by Jacobians on the way back. If each layer shrinks the signal by a factor slightly below 1, the product decays geometrically with depth; if each layer amplifies it slightly, the product blows up. Initialization sets that per-layer factor at step 0.
< The two degenerate cases >โ
| Start | What happens |
|---|---|
| All zeros (or any constant) | Every unit in a layer computes the same output and receives the same gradient, so they stay identical forever. The layer has the capacity of a single unit โ this is the symmetry problem, and it is why weights must be random. |
| Too large / too small | Activation variance grows or decays layer by layer. With sigmoid / tanh the units saturate and their derivatives go to zero; with ReLU the activations either explode or die out. |
Random initialization breaks symmetry; the scale of that randomness is what keeps the signal alive through depth.
Key pointsโ
< The variance argument >โ
For a linear layer with inputs, independent zero-mean weights of variance and inputs of variance :
Holding โ so that neither the forward activations nor the backward gradients change scale with depth โ requires . Every standard scheme is a version of this fraction, adjusted for what the activation function does to the variance.
< The standard schemes >โ
| Scheme | Variance | Use with | Why |
|---|---|---|---|
| Xavier([หzรฆvษชr]) / Glorot | tanh, sigmoid, linear | Averages the forward () and backward () constraints, so activations and gradients keep roughly constant variance. Assumes an activation that is near-linear around 0. | |
| He / Kaiming | ReLU, Leaky ReLU, GELU | ReLU zeroes about half its inputs and so halves the variance; the factor 2 compensates. Using Xavier with ReLU makes activations decay with depth. | |
| LeCun | SELU, self-normalizing nets | Forward-only constraint; the fixed point SELU's self-normalizing property is derived from. | |
| Orthogonal | โ (orthogonal matrix, optionally scaled by a gain) | deep RNNs, very deep stacks | An orthogonal preserves vector norms exactly, so repeated multiplication neither shrinks nor grows the signal. |
Each comes in a normal and a uniform flavor: draw from with from the table, or from with (which gives the same variance).
fan_inis the number of inputs to the layer,fan_outthe number of outputs. For a conv layer,fan_in = in_channels ร kernel_height ร kernel_width.- The choice follows the activation function, not the task. The one question worth asking is "does this layer's nonlinearity kill part of the signal?" โ if it does (ReLU family), use He.
< In practice >โ
import torch.nn as nn
layer = nn.Linear(512, 512)
# ReLU-family activations -> He / Kaiming
nn.init.kaiming_normal_(layer.weight, nonlinearity='relu')
# tanh / sigmoid -> Xavier / Glorot
nn.init.xavier_uniform_(layer.weight, gain=nn.init.calculate_gain('tanh'))
nn.init.zeros_(layer.bias)
- Framework defaults are not always the right scheme. PyTorch's
nn.Linearandnn.Conv2ddefault to a Kaiming-uniform variant witha=โ5, which is a historical choice rather than a match for your activation; setting the initializer explicitly is cheap insurance for a deep model. - Biases start at zero. Exceptions: LSTM forget-gate biases are often set to 1 so the cell remembers by default, and a final-layer bias can be set to the log-odds of the base rate to save the model from learning the class prior.
- Residual branches are often started at zero (a zero-init final BatchNorm , or a learnable scalar at zero), so a deep network starts as the identity and gains depth as training proceeds.
- Normalization reduces the sensitivity but does not remove it. BatchNorm/LayerNorm rescale activations after the fact, so a badly scaled init hurts less โ but the backward pass at step 0, and networks without normalization, still depend on it.
- Transformers typically use a small fixed standard deviation (e.g. ) and scale the residual-projection weights by so the residual stream's variance does not grow with layer count.
< How to tell it is wrong >โ
Run one forward and one backward pass on a single batch, and print per-layer statistics:
- Activation standard deviation shrinking steadily with depth โ scale too small (or Xavier used with ReLU).
- Activation standard deviation growing with depth, or
inf/NaNat step 0 โ scale too large. tanh/sigmoidoutputs clustered at / and โ saturated from the start.- A large share of
ReLUunits at exactly 0 across all batches โ dead units. - Early-layer gradient norms orders of magnitude below later ones โ the backward signal is decaying.
< Related ideas >โ
- Vanishing & Exploding Gradients โ the failure mode initialization is chosen to avoid.
- Data Normalization โ the same variance argument applied to the network's input layer.
- Gradient Descent โ what happens once the first gradients arrive.
Implementationโ
< Notebook >โ
Open the weight-initialization notebook on GitHub
< Weight-matrix dimensions >โ
Q: Why is Wโ shaped (2, 3) and Wโ shaped (1, 2)?
layers_dims = [3, 2, 1] describes this network:
Each row of a weight matrix belongs to one neuron in the current layer; each column corresponds to one input from the previous layer.
| Matrix | Connects | Shape |
|---|---|---|
| 3 input features to 2 hidden neurons | ||
| 2 hidden activations to 1 output neuron |
For the first layer, and :
The result has one value for each hidden neuron. The second layer receives those two activations:
In general:
W_l.shape = (layers_dims[l], layers_dims[l - 1])
Crash courseโ
- Weight Initialization Explained โ Xavier, He & Why It Matters
Referenceโ
- Symmetry Breaking versus Zero Initialization (Paul Mielke)
- Understanding the difficulty of training deep feedforward neural networks โ Glorot & Bengio (2010)
- Delving Deep into Rectifiers โ He et al. (2015)
- Exact solutions to the nonlinear dynamics of learning in deep linear networks โ Saxe et al. (2014)
- Self-Normalizing Neural Networks โ Klambauer et al. (2017)
- torch.nn.init โ PyTorch documentation