Concepts / Normalization in Neural Networks

Normalization in Neural Networks

Batch normalization normalizes across the batch dimension, pooling values across different inputs.

  • Programming

The Question Hidden Inside Normalization

Suppose you already know batch normalization, the traditional normalization method used in many neural networks. A natural question follows: if you want to normalize the activations inside a large language model, is there another option, and how does it differ? The answer depends on one decision that is easy to overlook. Normalizing always involves choosing a set of values to summarize together.

What do you think happens?

Before reading on: if the set of values being summarized changed, what would change about the normalization result?

Reveal answer
Answer:

The statistics computed from those values change, so every value that gets normalized using them also changes.

Normalization works by summarizing a group of values and then rescaling values relative to that summary. Change the group and you change the summary, which changes the output. This is the single idea the rest of the article builds on.

Reading a Training Batch as a Grid

Think of a training step as a grid. Each row is one input from the batch, and each column is one feature of that input. Normalization needs statistics, and the direction in which you gather those statistics is the whole difference between the two methods. The grid below shows three inputs, each with three features, so you can see the two directions of gathering side by side.

x1Ainput 1, feature Ax2Ainput 2, feature Ax3Ainput 3, feature Ax1Binput 1, feature Bx2Binput 2, feature Bx3Binput 3, feature Bx1Cinput 1, feature Cx2Cinput 2, feature Cx3Cinput 3, feature C
When a tensor of shape [batch, features] is normalized, which entries are grouped together to compute the statistics: the same feature across all inputs in the batch, or all features within one input?

Each cell holds one activation value. Each column is a feature, and each row is an input. Batch normalization and layer normalization differ only in which of these cells they treat as belonging to the same group when they compute their statistics.

Two Directions for Gathering Statistics

The source describes the two methods as moving in opposite directions across this grid. Each method has its own pooling pattern, and that pattern determines what the statistics depend on.

Why Batch Size Changes the Answer

The practical pressure behind this comparison is resources. LLMs often require significant computational resources, and the available hardware and the specific use case can dictate how large a batch you are able to use, both during training and during inference. When the batch is limited, the set of values that batch normalization pools together is limited too. A method whose behavior depends on how many inputs are processed together will therefore behave differently when the batch changes.

Pooled valuessame feature across inputsPooled valuesall features of one inputDependence on batchchanges with batch sizeDependence on batchnone; each input standsalone
If the batch has one example, or hundreds spread across many devices, how does the set of values used for normalization change for batch normalization versus layer normalization?

Layer normalization avoids this dependence. Because it normalizes each input independently of the batch size, the behavior for one input does not depend on which other inputs are sitting beside it. The source identifies this as offering more flexibility and stability in scenarios where resources are constrained, and it is particularly useful for distributed training or when models are deployed in environments with limited resources.

Choosing a Method for Your Situation

Given an input layout and a batch situation, the choice follows from which direction of gathering suits the constraint. The flowchart below captures the decision the source points toward: when the batch is limited, constrained, or spread across devices, the method that works within each input is the safer choice.

Batch constrainedor distributed?limited inputs per step, orresources limitedLayer normalizationstatistics across featuresof each inputBatch normalizationstatistics across inputsfor each featureyesno
Given the batch situation, which normalization method applies, and along which dimension are its statistics gathered?
Worked example

Applying the choice to two situations

Situation A: a training run where each device holds only a small slice of the batch. Situation B: a run on one machine where the batch is large and the same features are compared across many inputs. Which method fits each?

  1. 1
    Identify the constraint in Situation A

    Each device holds only a small slice of the batch, so batch statistics would have to be built from a small, split set of inputs. The source notes that layer norm can avoid the step of sharing batch statistics across devices.

  2. 2
    Choose for Situation A

    Layer normalization fits, because each input's statistics come from its own features and no cross-device sharing is needed.

  3. 3
    Identify the situation in Situation B

    The batch is large and available on one machine, so the batch-direction statistics are well defined and batch normalization is an available option.

  4. 4
    Choose for Situation B

    Batch normalization fits, since it pools each feature across the inputs in the batch.

Result

Constrained or distributed training points toward layer normalization. A large batch on one machine leaves batch normalization as a natural choice.

  • Common mistake

    Assuming layer normalization pools values across the batch

    What it looks like

    Computing the statistics for feature A from x1A, x2A, and x3A and calling it layer normalization.

    Why it fails

    That is the column direction, which is batch normalization. Layer normalization works along the row.

    Do this instead

    For layer normalization, gather the statistics from one input's features only.

  • Common mistake

    Thinking the choice of method does not depend on the batch

    What it looks like

    Using batch normalization on a device that holds only one or two inputs per step.

    Why it fails

    Batch normalization's behavior depends on how many inputs are processed together, so a small or split batch changes its results.

    Do this instead

    When the batch is limited or spread across devices, consider the method whose statistics do not depend on the batch.

Where GELU Fits in the Transformer Block

Normalization is only one part of the story in a transformer block. The source moves on to a small neural network submodule that is used as part of the transformer block in LLMs, called the feed forward submodule. Its first step is implementing the GELU activation function, which the source describes as playing a crucial role in this submodule.

Feed forwardsubmodulepart of the transformerblock in LLMsGELU activationplays a crucial role inthis submoduleTransformed valuesoutput of the submoduleappliesproduces
Where the GELU activation sits within the feed forward submodule, and how values move through it. This is a simplified view of the submodule's internal flow.

Practice Choosing the Right Direction

MEDIUM

A training run uses a batch of two inputs spread across two devices, one input per device. Which normalization method avoids needing batch statistics shared across the devices, and along which dimension does it gather its statistics?

Hints
  • Ask which method's statistics depend on the other inputs in the batch.
  • Layer normalization gathers its statistics along one input's own features, which means it looks along the row.

Summary of Normalization Directions

  1. 1
    Normalizing always involves choosing a set of values to summarize together, and that choice determines the result.
  2. 2
    Batch normalization pools values across the batch dimension, down each feature column across different inputs.
  3. 3
    Layer normalization pools values across the feature dimension, along each input's row, so it does not depend on batch size.
  4. 4
    Layer normalization offers more flexibility and stability when resources are constrained or training is distributed.
  5. 5
    GELU is the activation function that plays a crucial role in the feed forward submodule of a transformer block in LLMs, separate from the normalization step.

At a glance

  • Normalizing always involves choosing a set of values to summarize together, and that choice determines the result.
  • Batch normalization pools values across the batch, while layer normalization pools values across the features of each input.
  • Because layer normalization does not depend on batch size, it offers more flexibility and stability for constrained or distributed training.
  • GELU is the activation function that plays a crucial role in the feed forward submodule of a transformer block in LLMs.

Related concepts

More concepts starting with N