Skip to the workspace
Pebellslearn by building
Calculus  ›  Capstone Project

Gradient, Jacobian and Hessian

Gradient, Jacobian, Hessian 16 minThe gradient lists the slopes of one output, the Jacobian lists the slopes of many outputs, and the Hessian lists the slopes of the slopes.

machine learning / Gradient, Jacobian and Hessian

Explore each scene at your own pace.

01 · The rule

The rule

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 =6w+b= 6w + b for flat B, whose rent is 22. The loss is L(w,b)=(6w+b−22)2L(w, b) = (6w + b - 22)^2. Call the error e=6w+b−22e = 6w + b - 22.

Gradient: all first partials of a single number, stacked in a vector. By the chain rule,

∇L=(∂L∂w,∂L∂b)=(12e,  2e)\nabla L = \left( \frac{\partial L}{\partial w}, \frac{\partial L}{\partial b} \right) = (12e, \; 2e)

At (w,b)=(3,1)(w, b) = (3, 1): e=18+1−22=−3e = 18 + 1 - 22 = -3, so ∇L=(−36,−6)\nabla L = (-36, -6). The gradient points in the direction of steepest increase. Gradient descent steps the other way: new weights == old weights −- learning rate ×\times gradient. With learning rate 0.01, ww becomes 3+0.36=3.363 + 0.36 = 3.36 and bb becomes 1+0.06=1.061 + 0.06 = 1.06. The error shrinks to −0.78-0.78 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 (x=4x = 4) and flat B (x=6x = 6) are 4w+b4w + b and 6w+b6w + b, so

J=(4161)J = \begin{pmatrix} 4 & 1 \\ 6 & 1 \end{pmatrix}

Rows are outputs, columns are inputs.

Hessian: the partials of the partials of one number, showing how the slope itself changes (the curvature). For f=w2+3wb+2b2f = w^2 + 3wb + 2b^2, the gradient is (2w+3b,  3w+4b)(2w + 3b, \; 3w + 4b), and differentiating again gives

H=(2334)H = \begin{pmatrix} 2 & 3 \\ 3 & 4 \end{pmatrix}

It is symmetric, because the order of mixed partials does not matter for smooth functions.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.