TensorTau PathToAGI Training Deep Networks Level 07

Level 07 · Training Deep Networks · Chapter 100

Layer Normalization

How can we normalize activations without depending on which other examples happen to share the batch?

LayerNorm normalizes features within each example rather than a feature across batch examples.

For hidden vector x∈RDx\in\mathbb R^D:

μ=1D∑jxj,\mu=\frac1D\sum_jx_j,

σ2=1D∑j(xj−μ)2,\sigma^2=\frac1D\sum_j(x_j-\mu)^2,

yj=γjxj−μσ2+ϵ+βj.y_j=\gamma_j\frac{x_j-\mu}{\sqrt{\sigma^2+\epsilon}}+\beta_j.

1. BatchNorm versus LayerNorm

BatchNorm: normalize a feature using many examples
LayerNorm: normalize many features inside one example

Therefore LayerNorm behaves consistently for batch size 1 and does not require running batch statistics at inference.

2. Why Transformers use it

Sequence batches can have variable lengths and batch composition. LayerNorm’s per-token/per-example hidden-feature normalization fits naturally with Transformer representations.

For a tensor [B,T,D], LayerNorm(D) normalizes along the last D dimension while preserving shape.

Shape Check

[B,T,D] → LayerNorm(D) → [B,T,D]

3. Pre-norm and post-norm

Transformer blocks differ in where LayerNorm is placed relative to attention/MLP and residual connections. Pre-norm architectures often improve optimization stability in deep Transformers.

What to Remember

LayerNorm stabilizes feature scale within each example/token and is independent of batch statistics.

Next → Dropout: instead of normalizing activations, can random temporary removal of features discourage brittle co-adaptation?

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

  1. Explain: BatchNorm versus LayerNorm axis choices.
  2. Calculate: Normalize [1,2,3] ignoring epsilon and γ/β.
  3. Predict: Does LayerNorm need running means for inference?
  4. Break: Normalize over the wrong tensor axis and inspect behaviour.
  5. Build: Implement LayerNorm forward in NumPy.
  6. Investigate: Compare BatchNorm and LayerNorm on different batch sizes.