Equations · Ba, Kiros and Hinton, “Layer Normalization” (2016)
The LayerNorm formula, explained
LayerNorm rescales each token's feature vector to mean 0 and variance 1, then applies a learned scale and shift. Unlike BatchNorm, it does not look at other examples in the batch.
Term by term
One token's feature vector, of length .
The variance of the entries. Here is a standard deviation, not the sigmoid.
The standard deviation, with a small (about ) so a constant vector does not cause division by zero.
A learned per-feature scale and shift, applied elementwise, so the layer can undo the normalization where that helps.
A small worked example
: , , . Normalized: . With and , that is the output.
In code
import numpy as np
def layer_norm(x, gamma, beta, eps=1e-5):
mu = x.mean(-1, keepdims=True)
var = x.var(-1, keepdims=True)
return gamma * (x - mu) / np.sqrt(var + eps) + betaEvery symbol in it
Bold lowercase (vector)MuSummationFractionSigma (lowercase)VarianceSquare rootEpsilonGammaBetaHadamard (elementwise) product
Other equations
- Scaled dot-product attention
- Softmax
- Cross-entropy loss
- Adam update
- RMSNorm
- The ELBO
- KL divergence
- Backpropagation through one layer
- LoRA
- GELU
- Dropout
- Batch normalization
- Rotary position embedding (RoPE)
- Multi-head attention
- PPO clipped objective
- DPO loss
- Policy gradient (REINFORCE)
- Bellman equation
- Q-learning update
- InfoNCE contrastive loss
- Diffusion forward process
- DDPM simplified loss
- VAE reparameterization trick
- GAN minimax objective
- AdamW
- Cosine learning-rate schedule
- Perplexity
- Logistic regression
- Normal equation (linear regression)
- Singular value decomposition (SVD)
- Principal component analysis (PCA)