Training deep networksabout 10 minutes

Where gradient descent starts matters more for deep networks than for anything you've trained so far. Two things can go wrong at initialisation.

Symmetry

Set every weight to zero (or to the same constant) and every hidden unit in a layer computes the same thing, receives the same gradient, and updates identically. They stay clones forever, and a layer of 512 units behaves like one. Weights must start random to break the symmetry. Biases can start at zero.

Scale

Random, but how big? Each unit sums inputs times weights. If the weights have variance and inputs have variance , the sum has variance about . If , activations grow layer after layer and explode; if it's , they shrink toward zero. The same multiplication happens to gradients flowing backward. Over 50 layers, a factor of 1.1 per layer becomes 117, and 0.9 becomes 0.005.

The spread of activations through 20 layers of 64 units, on a log scale. Only one scale keeps the signal steady.
  • Xavier / Glorot initialisation (2010): variance , derived for tanh and sigmoid-like activations.
  • He (Kaiming) initialisation (He et al., 2015): variance for ReLU. The 2 compensates for ReLU zeroing half its inputs. This is PyTorch's default family for ReLU networks.

Vanishing and exploding gradients

Even with good initialisation, gradients through many layers are products of many factors: weight matrices and activation derivatives. Saturating activations like sigmoid shrink each factor, and this vanishing gradient problem (analysed by Hochreiter in 1991 and Bengio et al. in 1994) is why deep networks were considered nearly untrainable before about 2010. The cures, which you'll meet throughout module 5, are:

  • ReLU-family activations, whose derivative is 1 where active.
  • Careful initialisation, as above.
  • Normalisation layers (batch norm, layer norm) that re-standardise activations inside the network.
  • Residual connections , which give gradients a direct path through every layer. They're what made 100+ layer networks practical.
  • Gradient clipping for explosions: rescale the gradient when its norm exceeds a limit.
try itedit the code and run it, nothing can break
python sleeps until you press run

Initialise randomly to break symmetry, at a scale like He's for ReLU, so signals neither vanish nor explode as they pass through depth.

← Backward through a layer