liveBook · Manning

Vanishing gradient problem

As explained in Build a Large Language Model (From Scratch)

A training difficulty in deep neural networks where gradients shrink as they propagate backward, hindering learning in early layers.

The vanishing gradient problem occurs when gradients become progressively smaller as they propagate backward through a deep neural network. Early layers then receive weak weight updates, making them difficult to train. Shortcut, skip, or residual connections create shorter gradient paths by adding an earlier layer’s output to a later layer’s output. section 4.4

Figure 4.12 A comparison between a deep neural network consisting of five layers without (left) and with shortcut connections (right). Shortcut connections involve adding the inputs of a layer to its outputs, effectively creating an alternate path that bypasses certain layers. The gradients denote the mean absolute gradient at each layer, which we compute in listing 4.5.
figure

Compare five-layer networks with and without shortcut connections; the figure traces mean absolute gradients across layers. section 4.4

A small PyTorch experiment makes the training effect measurable:

  • Build five Linear–GELU layers with three-value intermediate representations and a one-value output. section 4.4
  • Compute mean absolute weight gradients after applying MSE loss and calling loss.backward(). section 4.4
  • Compare models without and with shortcuts: the reported gradients rise from roughly 0.0001–0.0050 to 0.207–1.326. section 4.4

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Traces backward gradient flow through a five-layer network and measures weight gradients, without deriving the underlying calculus.
Examples
Uses a five-layer Linear–GELU network, MSE loss, backward propagation, and printed mean absolute gradients.
What you can do after
Diagnose weak gradient flow with PyTorch and apply shortcut connections to improve gradient propagation.
How it connects
Connects vanishing gradients with shortcut connections, residual connections, layer normalization, transformer blocks, and GPT models.

Also explained in

Compare how 14 books explain vanishing gradient problem →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.