Difference between Batch Normalization and Layer Normalization

Graphic comparing Layer Normalization to Batch/Power Normalization, through cubes with Sentence Length, Feature, and Batch Dimension
Title: Layer Normalization vs. Batch Normalization
Source: PowerNorm: Rethinking Batch Normalization in Transformers

Introduction

In deep learning, normalization plays a crucial role in accelerating training, improving convergence, and stabilizing neural networks. Two widely used normalization methods are Batch Normalization (BatchNorm) and Layer Normalization (LayerNorm). While both methods aim to address the problem of internal covariate shift, they differ in their implementation, behavior, and use cases. This article dives into the key differences and applications of these techniques.

BatchNorm Review

Batch Normalization, introduced by Sergey Ioffe and Christian Szegedy in 2015, normalizes inputs across a mini-batch of data. It scales and shifts the normalized values with learnable parameters, improving the network’s ability to train faster and generalize better. Mentioned below are the key steps to follow for BatchNorm:

  1. Compute Mean and Variance: For each feature in the mini-batch, calculate its mean and variance.
  2. Normalize: Subtract the mean and divide by the square root of the variance (with a small for numerical stability).
  3. Scale and Shift: Apply learnable parameters (scale) and (shift) to the normalized values.

Diagram showing BatchNorm Illustration, with features, samples, and normalized features.
Title: BatchNorm Illustration
Source: AIML.com Research

Advantages:

  1. Improves convergence speed.
  2. Acts as a regularizer, reducing the need for Dropout in some cases.
  3. Effective for convolutional neural networks (CNNs) and fully-connected neural networks

Limitations:

  1. Dependence on batch size: Small batch sizes can lead to noisy estimates of mean and variance.
  2. Inference behavior differs from training: During inference, moving averages of mean and variance are used instead of batch statistics.

LayerNorm Review

Layer Normalization, introduced by Jimmy Lei Ba, Jamie Ryan Kiros, and Geoffrey E. Hinton in 2016, normalizes inputs across all features for each data sample independently, rather than across a mini-batch. Mentioned below are the key steps to follow for LayerNorm:

  1. Compute Mean and Variance: For each data sample, calculate the mean and variance across all features.
  2. Normalize: Subtract the mean and divide by the square root of the variance (with a small for stability).
  3. Scale and Shift: Apply learnable parameters (scale) and (shift) to the normalized values.
LayerNorm illustration with features, samples, and independent normalization across features
Title: LayerNorm Illustration
Source: AIML.com Research

Advantages:

  1. Performs well with small batch sizes or even single-sample inputs.
  2. Network Commonly used in recurrent neural networks (RNNs) and transformer architectures.

Limitations:

  1. Computationally more expensive than BatchNorm for large feature sizes.

BatchNorm v.s. LayerNorm

From the overviews of the two stakeholders, we can find that they have very similar ideas. Both take mean and variance to perform subtraction and division, followed by scaling and shifting with two learnable parameters. That is to say that they can both be formulated as:

$\mu = \frac{1}{m} \sum\limits_{i=1}^{m} x_i$

$\sigma^2 = \frac{1}{m} \sum\limits_{i=1}^{m} (x_i – \mu)^2$

$y_i = \frac{x_i – \mu}{\sqrt{\sigma^2 + \epsilon}} \cdot \gamma + \beta$

where,

$\mu$ = mean for the mini-batch
$\sigma^2$ = variance for the mini-batch
$m$ = input size
$x_i$ = $i^{\text{th}}$ dimension of input $x$
$y_i$ = $i^{\text{th}}$ dimension of input $y$
$\gamma$ = scale, learnable parameters
$\beta$ = shift, learnable parameters
$\epsilon$ = small constant to avoid division by zero

However, as mentioned above, BatchNorm takes mean and variance across samples while LayerNorm takes mean and variance across features. Additionally, BatchNorm uses moving averages for mean/variance during inference, while LayerNorm maintains the same behavior during both training and inference. Such differences essentially render different applications and usages for the two.

Applications and Use Cases

Batch Normalization

  1. Commonly used in convolutional neural networks (CNNs).
  2. Helpful in stabilizing training for large-scale feedforward networks.
  3. Effective in architectures where mini-batch statistics are meaningful.

Layer Normalization:

  1. Preferred for sequential models like recurrent neural networks (RNNs).
  2. Widely adopted in transformer-based architectures, such as BERT and GPT.
  3. Ideal for scenarios where batch size is small or varies dynamically.

The table below summarizes the difference between BatchNorm and LayerNorm.

Chart comparing BatchNorm and LayerNorm
Title: BatchNorm v.s. LayerNorm
Source: Paper on Batch Normalization by Ioffe et al., 2015,
and Layer Normalization by Ba et al., 2016

Summary

Both Batch Normalization and Layer Normalization have proven to be powerful tools in modern deep learning. While BatchNorm excels in networks like CNN with large batch sizes, LayerNorm shines in sequential and attention-based models with smaller or dynamic batch sizes. As the field evolves, newer normalization techniques, such as Group Normalization (Wu et al., 2018) and Instance Normalization (Ulyanov et al., 2016), continue to expand the toolkit available for improving neural network training and performance. Understanding the strengths and limitations of each can help you choose the appropriate normalization technique for their specific tasks.

Video Explanation

  • This video by ChiDotPhi on Youtube reviews different kinds of normalizations used in deep learning, including Batch and Layer Normalization discussed in the article, as well as Instance and Group Normalization. A walkthrough on Google Colab is provided afterwards. (Runtime: ~42 mins)
YouTube video
All About Normalizations by ChiDotPhi on Youtube

  1. What is Batch Normalization? How does it help with the training?
  2. Describe briefly the training process of a Neural Network model

Author

Help us improve this post by suggesting in comments below:

– modifications to the text, and infographics
– video resources that offer clear explanations for this question
– code snippets and case studies relevant to this concept
– online blogs, and research publications that are a “must read” on this topic

Leave the first comment

Partner Ad
Find out all the ways that you can
Contribute