Gradient, Jacobian and Hessian
A real model has many weights and makes many predictions. Writing one partial derivative at a time quickly becomes a mess. Three named collections keep it tidy, and each one appears in ML papers and error messages all the time.
The rule
Gradient: slopes of one output. Jacobian: slopes of many outputs. Hessian: slopes of slopes.
Give the rent model a bias: prediction for flat B, whose rent is 22. The loss is . Call the error .
Gradient: all first partials of a single number, stacked in a vector. By the chain rule,
At : , so . The gradient points in the direction of steepest increase. Gradient descent steps the other way: new weights old weights learning rate gradient. With learning rate 0.01, becomes and becomes . The error shrinks to and the loss drops from 9 to about 0.61.
Jacobian: when there are several outputs, stack each output's gradient as a row. Predictions for flat A () and flat B () are and , so
Rows are outputs, columns are inputs.
Hessian: the partials of the partials of one number, showing how the slope itself changes (the curvature). For , the gradient is , and differentiating again gives
It is symmetric, because the order of mixed partials does not matter for smooth functions.
The shortcut
Count inputs and outputs, and the shapes fall out. With inputs and outputs: the gradient has entries, the Jacobian is , and the Hessian is .
Count
Three weights (size, rooms, metro) are the inputs. Five flats give five predictions.
Mixing up the Jacobian's orientation is the usual trap: outputs down the side, inputs along the top.
Drills
Answer fast — trust the rule.
A loss depends on 3 weights. How many entries does its gradient have?
What shape is the Jacobian of 5 predictions with respect to 3 weights?
What is the Hessian of f = w² + 3wb + 2b²?
Starting at (w, b) = (3, 1) with gradient (−36, −6) and learning rate 0.01, where does one gradient-descent step land?
Why does gradient descent step against the gradient?
Remember
- Gradient: slopes of one output. Jacobian: slopes of many outputs. Hessian: slopes of slopes.
- Shortcut: n inputs, m outputs give n, m × n and n × n.
- Trap: stepping along the gradient instead of against it, or flipping the Jacobian.
