On the path Attention paperLayer normalisation keeps the numbers in range; the paper applies it after each residual addition.
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.
μ 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.
On the path Attention paper
In the paper. The Transformer uses layer normalisation after every residual addition, LayerNorm(x + Sublayer(x)) (residual connections): two in each of the six encoder layers and three in each of the six decoder layers, each over the 512 numbers of one position’s vector. Layer rather than batch normalisation suits it because sentences differ in length and every position is normalised on its own.
Test yourself
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.
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.
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
- Attention paperstep 10 of 17Follow this path