Layer Normalization › The Layer Normalization Formula
Language AI HandbookAll chapters Layer Normalization › The Layer Normalization FormulaOne section · Sections in this chapter3 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To understand layer normalization, let's start with a fundamental question: what does it mean for a neural network layer to have "unstable" activations, and how can we fix it?
The Problem: Activation Scale Drift
Imagine a token's hidden representation as a vector of 768 numbers (a typical transformer dimension). During training, these numbers can drift: some become very large, others very small, and their collective distribution shifts unpredictably. This creates two problems. First, downstream layers must constantly adapt to changing input statistics, making learning inefficient. Second, when values grow too large or too small, gradients either explode or vanish, destabilizing training entirely.
The solution is elegant: before each token's representation moves to the next layer, we transform it to have a predictable, standardized distribution. Specifically, we want the 768 features to have zero mean and unit variance. This "resets" the scale at every layer, preventing drift from accumulating.
The key insight is that we don't need to look outside the current token to achieve this. The token's own 768 values carry all the information we need to compute a mean and a standard deviation. Subtract the mean, divide by the standard deviation, and you have a normalized representation that is stable regardless of how the upstream weights evolved during training.
In practice, this normalization is applied at multiple points within each transformer block. This ensures that neither the attention mechanism nor the feed-forward network ever sees wildly out-of-distribution inputs. The cumulative effect over many layers is dramatic: networks with layer normalization can train stably with learning rates that would cause catastrophic divergence in networks without it.
Step 1: Finding the Center
The first step is computing where the current distribution is centered. Given a hidden state vector representing one token, we calculate its mean:
where:
- : the arithmetic mean of all features in this token's representation
- : the hidden dimension (e.g., 768 for BERT-base, 4096 for LLaMA-7B)
- : the value of the -th feature
This tells us the "center of mass" of the representation. If , the features are shifted toward positive values; if , they lean negative. The goal is to shift this center to zero.
Step 2: Measuring the Spread
Next, we need to know how spread out the values are. A representation where all values cluster tightly around the mean is very different from one where values are scattered widely. We capture this with variance:
where:
- : the variance, measuring how much the features deviate from their mean
- : the squared deviation of each feature from the mean
Squaring ensures that positive and negative deviations don't cancel out. If is large, the features are spread out; if small, they're tightly clustered. We'll use the standard deviation to rescale the values to unit variance.
Notice that we use the population variance (dividing by ) rather than the sample variance (dividing by ). This is a deliberate choice: we're not trying to estimate the variance of an underlying population; we're computing the exact variance of the values in front of us. Using rather than gives us an unbiased estimator for the squared deviation within this specific vector, which is what we need for a deterministic normalization operation.
Step 3: The Normalization Transform
With mean and variance in hand, we can now standardize each feature:
where:
- : the normalized value of the -th feature
- : a tiny constant (typically ) added to prevent division by zero if variance is extremely small
This two-part transformation is exactly what you'd do to standardize any dataset: subtract the mean (centering at zero), then divide by the standard deviation (scaling to unit variance). The result has zero mean and approximately unit variance across the features.
The subtraction by ensures the output is centered at zero, removing any bias in the representation. The division by ensures that features with large variance are scaled down to unit range, and features with small variance are scaled up. This compression of the dynamic range is what prevents activations from exploding across layers.
Step 4: Restoring Flexibility with Learnable Parameters
Layer normalization need not be restrictive. Forcing every representation to have exactly zero mean and unit variance might seem limiting: what if the optimal representation for some layer needs a different distribution?
The solution is to add learnable parameters that can undo the normalization if needed:
where:
- : the final output for the -th feature
- : a learned scale parameter for feature (initialized to 1)
- : a learned shift parameter for feature (initialized to 0)
These parameters are learned during training, just like weights and biases. If the network discovers that feature should have mean 3.5 and standard deviation 2.0, it can learn and to recover that distribution. This means layer normalization never reduces the network's representational power: it starts from a stable baseline but can learn any distribution it needs.
The initialization of and is deliberate. At the start of training, layer normalization acts as a pure standardization with no modification: the output is exactly the normalized input. As training progresses, and adjust to whatever distribution the subsequent layers find most useful. This means the network starts with the stability benefits of normalization and gradually adapts to the specific distribution requirements of the task, without ever losing the ability to represent the original data.
The Complete Formula
Putting all the pieces together, layer normalization maps a hidden state vector of dimension into:
where:
- : the input vector representing one token's hidden state
- : the mean across all features
- : the variance across all features
- : a small stability constant (typically or )
- : learned scale parameters, initialized to ones
- : learned shift parameters, initialized to zeros
- : element-wise multiplication
The formula reads naturally: subtract the mean, divide by the standard deviation (with a safety epsilon), then apply a learned scale and shift.
Why Features, Not Samples?
The key insight that distinguishes layer normalization from batch normalization is the dimension over which we compute statistics. Batch normalization asks: "What's the typical value of feature across all samples in this batch?" Layer normalization asks: "What's the typical value across all features for this particular token?"
For transformers, the layer normalization approach is far more natural. Each token is processed independently, and we want stable statistics regardless of what other tokens or samples happen to be in the batch. This independence also means layer normalization works identically during training and inference, with no need for running statistics or batch size considerations.
To make this concrete, consider a transformer with hidden dimension 512. Batch normalization would compute 512 separate statistics (one per feature dimension), each estimated from however many tokens appear at the same position across the batch. Layer normalization computes 2 statistics (one mean and one variance) from the 512 features of a single token. The batch normalization statistics are estimates that improve with larger batches; the layer normalization statistics are exact computations that require no batch at all. During inference when you process one sequence at a time, layer normalization requires no special handling; batch normalization in inference mode must use stored running statistics from training, which may not accurately represent the test distribution.

