liveBook · Manning

Gradient

As explained in Grokking AI Algorithms, Second Edition

A gradient is a vector or matrix of partial derivatives that describes how a function changes with respect to multiple parameters.

A gradient tells a model how its loss changes when its parameters change. For one weight, a partial derivative gives the direction and sensitivity: a positive value means increasing that weight increases error; a negative value means increasing it decreases error. The symbol ∇W collects the partial derivatives for every weight in a layer into a matrix. section 9.26

Gradients turn a mistake into small weight changes:

  • Make a prediction and calculate its loss by comparing it with the correct result. section 11.32
  • Work backward from the loss. The chain rule links a weight to the error through the layers between them, producing a gradient for each parameter. section 9.26 section 11.30
  • Update each weight using its gradient and a learning rate, which controls the size of the change. Repeat with more batches. section 11.30 section 11.32

One small update

Suppose a weight is 0.5, its gradient is −0.5, and the learning rate is 0.01. Subtracting learning rate × gradient gives 0.5 − (0.01 × −0.5) = 0.505. The weight increases slightly because its negative gradient indicates that increasing it should reduce loss. section 9.26 section 11.30

The same idea applies when training different networks. A language model calculates gradients back through its output layers and attention mechanism to its embeddings. A diffusion model uses gradients from noise-prediction loss to update its weights. These are training steps; generating an image with an already trained model is inference. section 11.30 section 12.23

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Defines single-weight derivatives and layer-wide matrix gradients, then traces backward calculation and an update without deriving a complete implementation.
Examples
Works through a weight changing from 0.5 to 0.505 and describes gradients in token prediction and noise prediction.
What it assumes
Builds from a single weight’s partial derivative and explains the chain rule and matrix-gradient notation.
What you can do after
Practice/perform a single gradient-based weight update and follow where gradients fit in a repeated training cycle.
How it connects
Links partial derivatives and the chain rule to backpropagation, loss, learning rates and gradient descent.

Also explained in

Compare how 20 books explain gradient →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.