liveBook · Manning

Layer normalization

As explained in Build a Text-to-Image Generator (from Scratch)

Layer normalization normalizes the activations within each example to stabilize neural-network computation and training.

Layer normalization standardizes each example’s activations across its features, producing zero mean and unit standard deviation. Unlike batch-dependent normalization, it treats each input independently, helping stabilize neural-network training. section 2.1.2 section 2.3.2

In a transformer, place normalization inside encoder and decoder sublayers alongside residual connections. Self-attention and feed-forward transformations operate on normalized representations, while residual additions preserve information flow. section 2.1.2 section 3.3.4

A transformer block applies the mechanism in a repeatable sequence:

  • Normalize the input before the self-attention transformation. section 3.3.4
  • Add the attention output back to the original representation through a residual connection. section 3.3.4
  • Normalize the updated representation before the feed-forward network, then add the feed-forward output residually. section 3.3.4

Encoder-block implementation

A PyTorch-style block defines two normalization layers: one before multi-head attention and one before the MLP. Its forward pass computes attention from normalized input, adds that result to x, normalizes the updated x, computes the MLP output, and adds it again.

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Defines per-example standardization and traces placement in transformer blocks, but does not derive the normalization calculation.
Examples
Uses encoder and decoder transformer layers plus a four-block vision-transformer encoder to show normalization placement.
What it assumes
Assumes familiarity with transformer sublayers, self-attention, feed-forward networks, and residual connections.
What you can do after
Implement it via PyTorch-style LayerNorm calls inside attention and MLP residual blocks.
How it connects
Connects layer normalization with transformers, self-attention, feed-forward networks, residual connections, and vanishing gradients.

Also explained in

Compare how 7 books explain layer normalization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.