A technique to stabilize and accelerate the training of deep neural networks by normalizing the inputs across the features of a single data point, rather than across the batch.
A way to keep the numbers inside a neural network from getting too huge or too tiny. It rescales the data for each individual example so the network stays stable and learns faster, regardless of how weird the input data is.
Layer Normalization (LayerNorm) computes the mean and variance across all neurons in a given layer for a single training example. It then normalizes the values to have a mean of 0 and variance of 1, applying learnable scale and shift parameters. Unlike Batch Normalization, it is independent of batch size, making it ideal for RNNs and Transformers.
Adjusting the volume on a podcast. If one episode is too quiet and the next is too loud, LayerNorm acts as an auto-leveler, ensuring every episode plays at a consistent, comfortable volume for the listener.
# Conceptual: Layer Normalization in PyTorch
import torch
import torch.nn as nn
# Normalize across the last dimension (features)
layer_norm = nn.LayerNorm(normalized_shape=512)
# Input tensor: (Batch size, Sequence length, Features)
x = torch.randn(32, 10, 512)
# Output will have mean ~0 and variance ~1 across the 512 features for each token
output = layer_norm(x)