Skip to the content

fatehi.devx2Learning

Deep learning

Normalisation layers

Layers that shift and scale numbers to mean 0 and spread 1, along each vector (layer norm) or across a batch (batch norm), then let the network rescale them.

Keeping the numbers in range

As a vector passes through many layers its numbers can drift: grow, shrink or shift, and differently in different parts of the network. Each layer then has to keep adjusting to a moving input, and training slows or breaks. A normalisation layer brings the numbers back to a standard range: it subtracts their mean and divides by their spread (the standard deviation), then multiplies by a learned scale γ and adds a learned shift β, so the network can still choose the range it wants.

y=γx−μσ2+ε+β

μ and σ2 are the mean and variance of the numbers being normalised; ε, a tiny number such as 10−6, keeps the division safe; γ and β are learned, one pair per feature. The two kinds below differ only in which numbers μ and σ are taken over.

Across the batch or along the vector

Batch normalisation (Ioffe and Szegedy, 2015) takes each feature, one number of the vector, across all the examples of the batch: a column at a time. It works well in image networks, but it ties each example to the others in its batch, needs running averages to use at test time, and fits poorly with sequences of varying length.

Layer normalisation (Ba, Kiros and Hinton, 2016) takes all the numbers of one vector: a row at a time. Each vector is normalised on its own, in the same way in training and at test time, whatever the batch or the sequence length.

The same idea along two directions. Above, layer normalisation, one vector at a time. Below, batch normalisation over a batch of four vectors, one feature (a column) at a time.

Test yourself

  1. Over which numbers does layer normalisation take its mean and spread, and over which does batch normalisation?

    Layer norm: all the numbers of one vector. Batch norm: one feature across all the examples of the batch.

  2. Why are γ and β learned after normalising?

    So that the network can still give the numbers whatever scale and offset suit the next layer; normalising alone would force mean 0 and spread 1.

  3. How many layer normalisations does the Transformer of the paper have in its two stacks?

    Two per encoder layer and three per decoder layer, one after each sublayer: 12 + 18 = 30.

Where it leads

Kept in this browser only, never sent.

Paths through it