Machine learning building blocks
Layer normalization
ml.layer-normAlso called layer norm, layernorm, layer normalization, ln
where
where
Normalizes a feature vector to zero mean and unit variance, using the population (biased) variance, then applies a per-feature scale γ and shift β. Defaults match torch.nn.LayerNorm: γ = 1, β = 0, ε = 1e-5.
Notation
features of one example
x ∈ ℝn
per-feature scale
γ ∈ ℝn · default 1 in every entry
per-feature shift
β ∈ ℝn · default 0 in every entry
added to the variance before the square root
ε ≥ 0 · default 1e-5 · also common: 1e-6, 1e-12, 0
number of entries
Size, at least 2
Result: the normalized features
vector
Worked examples
Each result is solved exactly and confirmed by two independent computations.
Given
Result
Given
Result
≈ (-1.3416407865, -0.4472135955, 0.4472135955, 1.3416407865)
Given
Result
≈ (-0.999990000075, 0.999990000075)
Common mistakes
Uses the sample variance (divides by n − 1)
Layer normalization uses the population variance; torch.var and pandas default to n − 1.
Fix: Divide the squared deviations by n (torch.var(..., unbiased=False), numpy.var).
Adds ε to the standard deviation
ε is added after the square root, σ + ε, instead of inside it, √(σ2 + ε).
Fix: Add ε to the variance and then take the square root.
Notes
- ε guards against division by zero. Its default differs between libraries, which changes the result when the variance is small.
Source
Ba et al. 2016
Ba, Jimmy Lei; Kiros, Jamie Ryan; Hinton, Geoffrey E. Layer Normalization. arXiv:1607.06450, 2016.