Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

ML coding · Deep learning layers

Layer normalisation

Easy · Target 8 minutes · Python and NumPy

Given X (N × D), and gamma, beta of length D, return Y with Yn = γ ⊙ (Xn − μn) / √(σ²n + eps) + β, where μn and σ²n are the mean and the biased variance (divide by D) of row n.

Examples

Input X = [[1, 2, 3]], gamma = [1, 1, 1], beta = [0, 0, 0], eps = 0

Output [[-1.2247, 0, 1.2247]]

Mean 2, variance 2/3: the row becomes (−1.2247, 0, 1.2247).

Input X = [[5, 5, 5, 5]], gamma = [1, 1, 1, 1], beta = [0.5, 0.5, 0.5, 0.5], eps = 1.000e-5

Output [[0.5, 0.5, 0.5, 0.5]]

A constant row: ε keeps the division finite and the output is β.

Input X = [[1, 2], [10, 30]], gamma = [2, 1], beta = [0, 1], eps = 0

Output [[-2, 2], [-2, 2]]

Each row is normalised on its own, whatever its scale.

Constraints

  • 1 ≤ N ≤ 512, 1 ≤ D ≤ 1024
  • eps ≥ 0; when eps = 0 no row is constant
  • Checked to a relative tolerance of 10⁻⁹

Hints

Hint 1

Take the mean and variance along axis 1, the features, with keepdims=True.

Hint 2

np.var divides by n by default, which is the convention here. Do not pass ddof=1.

Hint 3

Add eps to the variance before the square root, then multiply by gamma and add beta.

Where candidates go wrong

  • Normalising over axis 0, the batch. That is batch norm.
  • Using the unbiased variance (ddof=1). PyTorch's LayerNorm divides by D.
  • Adding eps to the standard deviation instead of to the variance.
Console⌘/Ctrl + Enter runs

Python runs in your browser with NumPy. The first run downloads the runtime, which takes a few seconds.


Why interviewers ask it

It is short, and it is in every transformer block, so it is a natural opener in an ML engineering screen. The interviewer is checking the axis, the biased variance and where ε goes.

The theory behind it is in the lesson Attention, normalisation and convolution from scratch.


Keep going