Repository navigation
Module 09
⬅️ Module 08 · 🗺️ Curriculum · 🏠 Home · Module 10 ➡️
Level: Intermediate · Prerequisites: Modules 05, 07, 08 · Time: about 6–7 hours
On this page: Overview and learning goals · Beginner: the big picture · Core concepts and derivations · Worked example · Implementation · Advanced topics · Pitfalls and best practices · Conceptual questions and answers · Sources and further reading
A deep network is a long product of matrices and nonlinearities. Unless each layer roughly preserves the scale of signals going forward and gradients going backward, activations and gradients shrink to zero or blow up exponentially with depth. This module explains how initialisation, normalisation and residual connections keep training stable.
After this module you should be able to:
- explain symmetry breaking and derive Xavier and He initialisation from variance arguments;
- analyse vanishing/exploding signals through depth;
- write BatchNorm, LayerNorm, GroupNorm, InstanceNorm and RMSNorm and say which axes each normalises;
- explain train/eval differences and small-batch issues of BatchNorm;
- explain why residual connections create identity gradient paths.
Imagine passing a message through 50 people where each person either halves or doubles its volume: after 50 steps it is silent or deafening. Layers act the same way. Good initialisation sets each layer's gain to about 1; normalisation re-centres and re-scales activations on the fly; residual connections give the signal a "highway" that bypasses layers.

Figure 9.1 — Mean squared activation in a 40-layer, width-512 ReLU network. He init (figures/make_figs_2.py).
flowchart LR
X["x"] --> F["F(x): conv/linear, norm, activation"]
X --> ID["identity shortcut"]
F --> ADD(("+"))
ID --> ADD
ADD --> Y["y = x + F(x)"]
All-zero (or all-equal) weights give identical neurons identical gradients, so they never differentiate. Random initialisation breaks the symmetry; biases may start at zero.
For
He/Kaiming (2015) for ReLU: if
Per layer, the second moment is multiplied by
The backward recursion
For a linear chain the signal scales with the product of spectral norms:
For a mini-batch of activations of one feature, with batch mean
with learnable scale
LayerNorm normalises across the features of one example (Ba et al. 2016):
| Method | Statistics computed over | Typical use |
|---|---|---|
| BatchNorm | batch (+ spatial), per channel | CNNs with large batches |
| LayerNorm | all features of one sample | Transformers, RNNs |
| InstanceNorm | spatial axes, per sample & channel | style transfer |
| GroupNorm | groups of channels, per sample | small-batch vision |
| RMSNorm | RMS over features (no mean) | LLMs |
GroupNorm (Wu & He 2018) splits
BatchNorm with tiny batches has noisy statistics and a train/test mismatch; use GroupNorm/LayerNorm, or synchronised BatchNorm across devices. Remember model.eval() before inference, and beware stale running statistics after fine-tuning on a new domain.
A residual block computes
The leading term
He init width. For
BatchNorm on one channel with batch values
import numpy as np
def batchnorm(x, gamma, beta, eps=1e-5): # x: (B, C); stats over batch axis
mu, var = x.mean(0), x.var(0)
return gamma * (x - mu) / np.sqrt(var + eps) + beta
def layernorm(x, gamma, beta, eps=1e-5): # stats over feature axis
mu, var = x.mean(-1, keepdims=True), x.var(-1, keepdims=True)
return gamma * (x - mu) / np.sqrt(var + eps) + beta
def rmsnorm(x, g, eps=1e-6): return g * x / np.sqrt((x ** 2).mean(-1, keepdims=True) + eps)
x = np.array([[1.], [2.], [3.], [6.]]); print(batchnorm(x, 1., 0.).ravel()) # [-1.069 -0.535 0 1.604]import torch.nn as nn
nn.init.kaiming_normal_(layer.weight, nonlinearity='relu') # He
nn.init.xavier_uniform_(layer.weight) # Glorot
# model.train() uses batch stats; model.eval() uses running stats- Dynamical isometry and mean-field theory: choose initialisations (e.g. orthogonal) so the input–output Jacobian has singular values near 1, enabling trainable networks of 10,000 layers.
- Fixup/SkipInit/ReZero: residual networks without normalisation by scaling branches at init.
-
$\mu$ P (maximal update parametrisation): scales init and learning rates with width so hyperparameters transfer. - Weight normalisation, spectral normalisation (GANs, Module 19), QK-norm in attention.
-
Initialising residual branches to zero (final norm scale 0) makes each block start as the identity; scaling by
$1/\sqrt{2L}$ in deep Transformers. -
Normalisation and optimisation: BN makes loss scale-invariant in weights, giving an effective learning rate that depends on
$\lVert w\rVert$ (interaction with weight decay).
- Forgetting
model.eval(): BatchNorm keeps using batch statistics (noisy, batch-dependent predictions). - BatchNorm with batch size 1–4, or with sequences of varying padding.
- Applying weight decay to
$\gamma,\beta$ and biases. - Using Xavier with ReLU for very deep plain nets; use He.
- Normalising inputs but forgetting the same preprocessing at inference.
Q1. Derive He initialisation.
Show answer
Q2. Why does zero initialisation fail?
Show answer
All neurons in a layer compute and receive the same values, so they stay identical; with ReLU, zero weights also give zero gradients to earlier layers.
Q3. Why does BatchNorm behave differently in train and eval mode?
Show answer
Training normalises with the current batch's statistics; evaluation uses running averages so that each prediction is independent of the other examples. Mismatch between the two causes degraded accuracy with small or shifted batches.
Q4. Why do Transformers use LayerNorm rather than BatchNorm?
Show answer
Sequence lengths and padding vary, batch statistics are noisy for token positions, and LayerNorm is independent of batch size and identical at train and test time.
Q5. Why do residual connections make very deep networks trainable?
Show answer
The Jacobian
Q6. What is the role of
Show answer
They restore representational power: the network can learn any scale and shift, including undoing the normalisation if that is optimal.
Q7. Pre-LN versus post-LN: which is more stable and why?
Show answer
Pre-LN: the residual stream is never normalised inside the block, preserving the identity gradient path, so gradients are better-behaved at initialisation and warm-up requirements are milder; post-LN can have large gradients near the output.
Q8. When do you choose GroupNorm?
Show answer
For vision models with small per-device batches (detection, segmentation, video) where BatchNorm statistics would be unreliable.
Q9. How does RMSNorm differ from LayerNorm?
Show answer
It omits mean subtraction (and usually the bias), rescaling only by the root-mean-square; this is cheaper and works comparably in practice.
- Glorot, X., & Bengio, Y. (2010). Understanding the difficulty of training deep feedforward neural networks. AISTATS.
- He, K., Zhang, X., Ren, S., & Sun, J. (2015). Delving deep into rectifiers: surpassing human-level performance on ImageNet classification. ICCV. arXiv:1502.01852.
- Ioffe, S., & Szegedy, C. (2015). Batch normalization: accelerating deep network training by reducing internal covariate shift. ICML. arXiv:1502.03167.
- Ba, J. L., Kiros, J. R., & Hinton, G. E. (2016). Layer normalization. arXiv:1607.06450.
- Wu, Y., & He, K. (2018). Group normalization. ECCV.
- Ulyanov, D., Vedaldi, A., & Lempitsky, V. (2016). Instance normalization. arXiv:1607.08022.
- Zhang, B., & Sennrich, R. (2019). Root mean square layer normalization. NeurIPS.
- He, K., Zhang, X., Ren, S., & Sun, J. (2016). Deep residual learning for image recognition. CVPR. arXiv:1512.03385.
- Santurkar, S., et al. (2018). How does batch normalization help optimization? NeurIPS.
- Xiong, R., et al. (2020). On layer normalization in the Transformer architecture. ICML.
- Veit, A., Wilber, M., & Belongie, S. (2016). Residual networks behave like ensembles of relatively shallow networks. NeurIPS.
- Pennington, J., Schoenholz, S., & Ganguli, S. (2017). Resurrecting the sigmoid in deep learning through dynamical isometry. NeurIPS.
⬅️ Module 08 · 🗺️ Curriculum · 🏠 Home · Module 10 ➡️
🏠 Home · 🗺️ Curriculum · 🧭 Learning paths · ❓ Question bank · 📖 Glossary · 📚 References · ✏️ Contributing
🏠 Home · 🗺️ Curriculum · 🧭 Learning paths
❓ Question bank · 📖 Glossary · 📚 References
Jump to module:
01 02 03 04 05 06 07 08 09 10 11 12
13 14 15 16 17 18 19 20 21 22 23 24