Start studying
Module 2 · Deep Learning Module contents
Module 2  ·  Card 4 / 25intermediate

Weight Initialization

Weights start as small random numbers, and the size of those numbers matters more than it sounds.

Set them all to zero and every neuron in a layer computes the same thing forever.

Too large and the signal grows layer by layer until the activations saturate.

Too small and it shrinks until the deepest layers have almost nothing to learn from.

Xavier initialisation suits sigmoid and tanh, and He initialisation is the default for ReLU.

Learn in depth ↓
Module 02 · Deep Learning · 25 concepts
In depth  /  15 lines

Weight Initialization, in more detail

Before training starts every weight needs a value, and the choice is not free.

Set them all to zero and every neuron in a layer sees the same input and the same gradient.

They then stay identical forever, so a layer of 500 neurons learns one thing.

Small random values break that symmetry and let the neurons specialise.

The scale of that randomness is the part that matters.

Each layer multiplies the signal, so a scale slightly above one compounds layer by layer.

Twenty layers later the activations are enormous and the gradients explode.

A scale slightly below one compounds the other way and the signal fades to nothing.

The fix is to size the random numbers by how many inputs a neuron has.

Xavier initialisation does this for sigmoid and tanh, which are symmetric around zero.

He initialisation doubles the variance to account for ReLU discarding half its input.

ReLU networks with Xavier initialisation train visibly worse, which is how the split was found.

Biases are usually just set to zero, since the weights already broke the symmetry.

Every framework picks a sensible default, so you rarely set this by hand.

It matters when a deep network refuses to learn at all from the very first epoch.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath Weight Initialization

Zero does not work, and neither does large

W=0  ⟹  every unit in a layer computes the same thingW = 0 \implies \text{every unit in a layer computes the same thing}

If every weight starts identical, every unit in a layer receives the same gradient and stays identical forever. The layer has one unit's worth of capacity, whatever its width.

Randomness is what breaks that symmetry, which is why initialisation is random rather than clever.

Var(z)=nin Var(w) Var(x)\mathrm{Var}(z) = n_{\mathrm{in}} \, \mathrm{Var}(w) \, \mathrm{Var}(x)

The scale is the harder half. A layer's pre-activation is a sum of ninn_{\mathrm{in}} products, so its variance grows with the fan-in unless the weights shrink to match.

Keeping the variance at one

Var(w)=1nin\mathrm{Var}(w) = \frac{1}{n_{\mathrm{in}}}

Setting the weight variance to the reciprocal of the fan-in makes the output variance equal to the input variance, so a signal neither explodes nor dies as it passes through the stack.

W∼N(0,2nin+nout)W \sim \mathcal{N}\left(0, \frac{2}{n_{\mathrm{in}} + n_{\mathrm{out}}}\right)

Xavier initialisation averages the two directions, because the forward pass is scaled by the fan-in and the backward pass by the fan-out.

It assumes the activation is roughly linear near zero, which is true of tanh and false of ReLU.

W∼N(0,2nin)W \sim \mathcal{N}\left(0, \frac{2}{n_{\mathrm{in}}}\right)

He initialisation corrects for that. ReLU zeroes half its inputs and so halves the variance, and the factor of 2 puts it back.

Use He with ReLU and its relatives, Xavier with tanh and sigmoid. Getting this one factor wrong is enough to stop a 30-layer network training at all.

One initialisation, in order

  1. Read the layer's fan-in, and its fan-out if using Xavier.
  2. Pick the variance from the rule that matches the activation.
  3. Draw the weights from a normal or uniform distribution with that variance.
  4. Set every bias to zero, since the weights have already broken the symmetry.
  5. Check the activation statistics on one forward pass before training anything.

What to look at when it goes wrong

std(a[l])≈std(a[l−1])\mathrm{std}\big(\mathbf{a}^{[l]}\big) \approx \mathrm{std}\big(\mathbf{a}^{[l-1]}\big)

Push one batch through an untrained network and print the standard deviation of each layer's activations.

Falling steadily towards zero means the initialisation is too small and the signal is dying. Growing without bound means it is too large.

A healthy start is roughly flat across depth, and it costs one forward pass to check.

Why it matters less than it used to

Normalisation layers rescale activations at every step, so a network with batch norm or layer norm tolerates an initialisation that would otherwise be fatal.

Var(w)=2ninL\mathrm{Var}(w) = \frac{2}{n_{\mathrm{in}} L}

Residual networks go further and scale the residual branches down by the depth, so the identity path dominates at the start and the network begins close to shallow.

Initialisation is now insurance rather than the main event, and it is still the first thing to check when the loss does not move at all.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
Why is setting every weight in a layer to zero a bad start?
Deep Learning

25 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close