Elio Saade
Note

Layer Normalization

Topics: Neural Networks

Motivation

In traditional neural network training, the activations of each layer can vary drastically, which is known as internal covariance shift. This leads to issues like exploding or vanishing gradients. Similarly to Batch Normalization, Layer Normalization addresses these issues, as well as some of the shortcomings of BN.
The working principle is similar to BN (compute metrics, normalize, and scale); however, in LN, the mean and variance are computed across the features for every data vector in the batch instead of computing them across the batch for every feature.

Details of Layer Normalization

Step 1: Compute Mean and Variance for Each Feature

The mean and variance are computed across features (or neurons) for every element of the batch:
μ=1H∑i=1Hxi\mu = \frac{1}{H} \sum_{i=1}^H x_i
σ2=1H∑i=1H(xi−μ)2\sigma^2 = \frac{1}{H} \sum_{i=1}^H (x_i - \mu)^2
where HH is the number of features.

For example, if the first layer has 256256 outputs, and a batch size of 3232 is used, then, 3232 means and variances are computed across the 256256 features.

Step 2: Normalize the Input

Each feature is normalized by the calculated layer mean and variance to obtain features with mean 00 and unit variance:
x^i=xi−μσ2+ϵ\hat{x}_i = \frac{x_i - \mu}{\sqrt{\sigma^2 + \epsilon}}
where ϵ\epsilon is a small constant added for numerical stability.

Step 3: Apply Scaling and Shifting

Similarly to BN, scaling and shifting is applied using learnable parameters γ\gamma and β\beta to ensure that the normalized activations can still cover a wide range of values:
yi=γx^i+βy_i = \gamma \hat{x}_i + \beta
In practice, γ\gamma and β\beta are not single scalars, but vectors of parameters, where each feature has its own parameters: γ∈RH,  β∈RH\gamma \in \mathbb{R}^H, \; \beta \in \mathbb{R}^H.

PyTorch Example

LayerNorm_PyTorch.png

References

  1. https://www.geeksforgeeks.org/deep-learning/what-is-layer-normalization/

Connections

Direct relationships to this note.