Search

Search papers and lessons

Layer Normalization

Jimmy Lei Ba, Jamie Ryan Kiros, Geoffrey E. Hinton · 2016

Ba, Kiros and Hinton (2016) move batch normalization's statistics from the batch to a single example's layer, which makes normalization work for recurrent networks, online learning and batch size one.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Neural network basics
  • Linear algebra
  • Architectures
  • Losses & regularization
  • Sign in to see your progress

TL;DR

Batch normalization had made deep image networks train faster, but it needs statistics from a mini-batch, which is awkward for recurrent networks, online learning and very small batches. Ba, Kiros and Hinton keep the same idea and change where the statistics come from: they average over all the hidden units of one layer for one training case. The result is a normalization that behaves the same at training and test time and that stabilizes the hidden state of recurrent networks.

Where you'll see it: the normalization layer used in Transformer models and most large language models, which descends directly from this paper.

The problem

Deep networks trained with Stochastic Gradient Descent were slow to train. The paper names two routes to speed: spread the computation over many machines, which brings communication costs and diminishing returns, or change the forward pass so that learning is easier. Batch normalization took the second route, and it worked well on feed-forward networks.

It had three limits that the authors wanted removed.

  1. Its statistics are estimated from the mini-batch, so the effect depends on the batch size. The paper says this puts constraints on the size of a mini-batch, and in practice small batches give noisy estimates.
  2. In a recurrent network the summed input to a unit changes with the time step. Batch normalization then needs separate running statistics for every step. A test sequence longer than any training sequence has no statistics at all.
  3. It cannot be used for pure online learning with one example at a time, and it does not fit very large distributed models whose per-machine batches must be small.

The paper asks for a method that keeps the training speed-up but computes its statistics from a single training case, so that no two examples depend on each other.

Key idea

Batch normalization asks, for one hidden unit, how its value varies across the batch. Layer normalization asks, for one training case, how all the hidden units of the layer vary together. Each layer's summed inputs for one example form a vector, and that vector is re-centred and re-scaled using its own mean and standard deviation. The paper also keeps an adaptive gain and bias for each unit, applied after the normalization and before the nonlinearity, as batch normalization does.

Picture a stack of hidden vectors over time. For every time step, the recurrent layer takes its own vector, computes two numbers from it, and rescales the vector. Nothing is stored from earlier steps and nothing is taken from other sequences in the batch. The same gain and bias are shared across all time steps, so the parameter count does not grow with sequence length.

The authors also argue, in the analysis section, that normalizing the summed inputs changes the geometry of learning in a useful way. The normalizing scale acts like a learning-rate control on each neuron's weight vector, which has the effect of an early-stopping pressure during training. This is a secondary argument, but it explains why the method speeds up training and not only why it is convenient.

The method

Let aila^l_i be the summed input to hidden unit ii in layer ll, and let HH be the number of hidden units in that layer. Layer normalization computes

μl=1H∑i=1Hail,σl=1H∑i=1H(ail−μl)2.\mu^{l} = \frac{1}{H}\sum_{i=1}^{H} a^{l}_{i}, \qquad \sigma^{l} = \sqrt{\frac{1}{H}\sum_{i=1}^{H}\left(a^{l}_{i} - \mu^{l}\right)^{2}}.

Every unit in the layer shares the same two numbers, while different training cases have different numbers. This is the key difference from batch normalization, whose statistics for unit ii run over the training cases. The normalized, rescaled activation is

hil=f ⁣(gilσl(ail−μl)+bil),h^{l}_{i} = f\!\left(\frac{g^{l}_{i}}{\sigma^{l}}\left(a^{l}_{i} - \mu^{l}\right) + b^{l}_{i}\right),

where ff is the nonlinearity, gilg^{l}_{i} is a per-unit gain and bilb^{l}_{i} is a per-unit bias. Batch normalization has the same form, but its μ\mu and σ\sigma are computed per unit over the mini-batch. The same formula, with the statistics taken over one vector of width dd, is the layer normalization that the rest of the field uses today.

The recurrent version

For a standard recurrent layer, the summed input at time tt is built from the previous hidden state and the current input:

at=Whh ht−1+Wxh xt.\mathbf{a}^{t} = W_{hh}\,\mathbf{h}^{t-1} + W_{xh}\,\mathbf{x}^{t}.

The layer-normalized recurrent update is

ht=f ⁣[gσt⊙(at−μt)+b],μt=1H∑i=1Hait,σt=1H∑i=1H(ait−μt)2.\mathbf{h}^{t} = f\!\left[\frac{\mathbf{g}}{\sigma^{t}} \odot \left(\mathbf{a}^{t} - \mu^{t}\right) + \mathbf{b}\right], \qquad \mu^{t} = \frac{1}{H}\sum_{i=1}^{H} a^{t}_{i}, \qquad \sigma^{t} = \sqrt{\frac{1}{H}\sum_{i=1}^{H}\left(a^{t}_{i} - \mu^{t}\right)^{2}}.

Here ⊙\odot is element-wise multiplication. The statistics depend only on the summed inputs at the current step. The gain g\mathbf{g} and bias b\mathbf{b} are shared across steps.

In the experiments, the LSTM and GRU versions normalize each gate's pre-activation separately, and the cell state is normalized before the output nonlinearity. The supplementary material gives the exact equations. For an LSTM, the four gate pre-activations are the sum of two normalized terms, one from Whht−1W_h \mathbf{h}_{t-1} and one from WxxtW_x \mathbf{x}_t, and the cell output is σ(ot)⊙tanh⁡(LN(ct))\sigma(\mathbf{o}_t) \odot \tanh(\mathrm{LN}(\mathbf{c}_t)). Each normalized term has its own gain and bias. Here the paper disagrees with itself: Section 6 says gains start at 1 and biases at 0, which is the sensible choice and the one used here, while the supplement's LSTM paragraph says each αi\alpha_i (the additive parameter in its notation) starts at zeros and each βi\beta_i (the multiplicative one) at ones. Read that as the same ones-and-zeros initialization with the names swapped.

Invariance

Table 1 of the paper compares which transformations leave each method's output unchanged. Layer normalization is invariant to re-scaling the whole weight matrix, to shifting every incoming weight by the same constant vector, and to re-scaling each individual training case. It is not invariant to re-scaling a single neuron's weight vector, which batch normalization is. The paper argues that these properties matter because layer normalization cannot be turned into a simple re-parameterization of the original network, so its effect on learning is different.

Results

The paper states that it runs six tasks: image-sentence ranking, question answering, contextual language modelling (skip-thought), generative modelling (DRAW), handwriting generation and MNIST classification. It adds a short preliminary convolutional experiment as a seventh subsection. The focus is recurrent networks.

Order-embeddings for images and sentences (MSCOCO). The authors add layer normalization to the GRU that encodes captions in the order-embedding model of Vendrov et al. The test results are averages over five splits of 1000 images and 5000 captions. Caption retrieval R@1 goes from 46.6 for the reproduced baseline to 48.5, and image retrieval R@1 goes from 37.8 to 38.9. The model reaches its best validation point in about 60 percent of the baseline's training time.

Attentive reader (CNN corpus). This question-answering model is compared with recurrent batch normalization. Layer normalization trains faster and reaches a better validation result than both the baseline and the batch-normalized variants. With the attentive reader, the authors also found that a gain of 1.0 worked much better than the 0.1 that recurrent batch normalization needed, which means the method is less sensitive to initialization.

Skip-thought vectors (BookCorpus). Layer normalization gives a speed-up with no loss at the end. After 1 million iterations, the model with layer normalization scores 79.5 on MR against 77.3 for the reproduced baseline and 75.5 for the original Kiros et al. result. On the SICK relatedness task, the Pearson correlation is 0.854 against 0.842. Training for about a month (roughly 1.7 million iterations) improves every column except MR, which dips from 79.5 to 79.4.

DRAW on binarized MNIST. With layer normalization applied to the LSTM output, the model converges almost twice as fast over the first 100 epochs. After 200 epochs, the baseline reaches a test variational bound of 82.36 nats and the normalized model reaches 82.09 nats. The speed-up is the main effect here; the final numbers are close.

Handwriting sequence generation (IAM-OnDB). The paper's text says the average handwriting line has length around 700, while the Figure 5 caption says the models were trained with sequence length 500 (presumably a truncation length for training, which the paper does not explain). Mini-batches have size 8. Layer normalization reaches a log likelihood comparable to the baseline and gets there much faster. The authors present this as evidence that the hidden dynamics stay stable on long sequences with small batches.

Permutation-invariant MNIST. A 784-1000-1000-10 fully connected network is trained with batch sizes 128 and 4. Layer normalization is faster than batch normalization applied to all layers, and it is not sensitive to the batch size.

Convolutional networks. The result is negative. Layer normalization gives a speed-up over no normalization, but batch normalization is better. The authors suggest that the units of a convolutional layer do not contribute equally, since units near the border of the image have very different statistics, so pooling all of them into one mean and variance is a poor match. They call this an open problem.

ExperimentBatch-normalization or baselineLayer normalizationWhat the paper claims
MSCOCO caption R@146.648.5Better, and faster to the best model
Skip-thought MR77.379.5Faster and better
DRAW, 200 epochs (nats)82.3682.09About twice as fast
Convolutional netsBN betterLN worse than BNOpen problem

Why it mattered

Layer normalization became a standard component of the transformer, introduced in 2017, which applies it around every attention and feed-forward sublayer. Later work moved it to the pre-norm position and simplified it, for example to root-mean-square normalization without the mean subtraction. The core design choice, normalizing the features of one token instead of one feature over a batch, remains the one in use. It also made recurrent sequence models easier to train at longer lengths, which mattered for the sequence-to-sequence systems of the time.

The paper also showed that a normalization method could be designed around a particular failure of batch normalization, and that the failure can be reasoned about with invariance and geometry. That style of analysis is now common in the design of normalization schemes.

Reading guide

Read the paper in this order and use the prerequisites as follows.

  1. Abstract and Introduction (Section 1). Needs Stochastic Gradient Descent and the idea of a batch normalization layer. The motivation is training time and the three limits listed above.
  2. Background (Section 2). Needs Feedforward Neural Network, Backpropagation and the notion of a summed input and an Activation Function. The batch-normalization equations use Mean and Variance.
  3. Layer normalization (Section 3). Needs Mean and Variance and Vector. The equations for μ\mu and σ\sigma are a single-vector sample mean and variance. Section 3.1 needs Recurrent Neural Network and the idea of time steps.
  4. Related work (Section 4). Needs Batch Normalization. Covariate Shift, the problem both methods aim to reduce, is first raised in Sections 2 and 3.
  5. Analysis (Section 5). This is the hardest part. Section 5.1 needs only the invariance definitions. Section 5.2 needs Kullback–Leibler Divergence and Fisher Information, which define the Riemannian metric used for the geometry argument. Read Section 5.1 first and skim 5.2 unless you need the details. The Vanishing Gradient Problem is the motivation for the stability claim, which appears at the end of Section 3.1.
  6. Experiments (Section 6). The sequence-to-sequence background is in Sequence to Sequence Learning with Neural Networks. Read the experiment in which the claim matters for your interest: the attentive reader or skip-thought for language, DRAW for generative modelling, and the MNIST experiment for feed-forward networks.

Check yourself

QuestionFor a layer with H = 3 summed inputs a = [1, 2, 6], compute the layer-normalized mean and standard deviation, with gain 1 and bias 0 and the epsilon term ignored.Show answer

The mean is (1 + 2 + 6) / 3 = 3. The deviations are [-2, -1, 3], their squares are [4, 1, 9], and the variance is 14 / 3, so sigma is about 2.160. The normalized output is about [-0.926, -0.463, 1.389]. Since every unit in the layer uses the same mean and standard deviation, this is the whole calculation for the layer on this training case.

QuestionWhy does layer normalization handle a test sequence that is longer than any training sequence, while batch normalization in a recurrent network does not?Show answer

Batch normalization in a recurrent network needs separate running statistics for each time step, which come from training sequences. A longer test sequence reaches time steps for which no statistics were collected. Layer normalization computes its mean and standard deviation from the summed inputs at the current step only, using the same shared gain and bias at every step, so there is nothing to look up and nothing that depends on the sequence length.

QuestionThe paper notes that batch normalization is invariant to re-scaling a single neuron's weight vector, but layer normalization is not. What does this mean for the individual neuron, and why might the authors consider it acceptable?Show answer

If a single neuron's incoming weights are multiplied by a constant, batch normalization's mean and standard deviation for that neuron scale by the same constant, so the normalized output is unchanged. Layer normalization's statistics are shared across the layer, so scaling one neuron's weights changes that neuron's contribution relative to the others and changes the output. The paper does not call this a flaw. It presents it as a different set of invariances: layer normalization is invariant to scaling the whole weight matrix and to shifting all incoming weights by a common vector, and (as Table 1 also shows) to re-scaling an individual training case, which batch normalization is not.

QuestionA team wants to apply layer normalization to convolutional image features. Based on the paper's convolutional experiment, what should they expect, and what does the paper suggest is the reason?Show answer

They should expect layer normalization to speed up training relative to no normalization, but to do worse than batch normalization. The paper's reason is that the hidden units of a convolutional layer do not contribute equally to the output. Units whose receptive fields touch the image boundary are rarely active and have different statistics from the others, so one mean and standard deviation over the whole layer is a poor summary. The authors leave a fix to future work.

Sources

Read the full paper

Jimmy Lei Ba, Jamie Ryan Kiros, Geoffrey E. Hinton, 2016. arXiv:1607.06450