Gradients and the chain rule
The gradient points uphill on the loss surface, and the chain rule multiplies derivatives through each stage of a model. Work both by hand on a one-neuron model, then see why the same steps scale to millions of weights.
You will be able to
- Compute partial derivatives and assemble the gradient of a loss
- Explain why the gradient is the direction of steepest increase
- Apply the chain rule to a composition of functions, including the Jacobian form
One slope per parameter
ReadA loss depends on two weights. The partial derivative is the slope when you nudge and hold still.
Stack the partials and you get the gradientGradientThe vector of a function's partial derivatives: it points in the direction of steepest increase.:
For , . At it is : the loss climbs three times faster along .
Why it points uphill
ReadFor a small step , the change in loss is about . Among all steps of the same length, that dot product is largest when points along and most negative when it points the opposite way.
So is the direction of steepest increase, of steepest decrease, and the gradient is always at right angles to the contour lines of the loss. Gradient descent steps along .
DExplorable · Gradient descent on a loss surface
Why does the learning rate decide whether training works at all?
With this learning rate and this surface, what will the path do?
Surface
Optimizer
Orange dot: the start. Blue dot: where the path has reached. Darker shading is higher loss.
Commit your prediction above to see the result
Make your prediction to see the result.
Functions of functions
ReadModels are stacks: a linear score, then a squashing function, then a loss. The chain ruleChain ruleThe derivative of a composition is the product of the derivatives of its pieces, taken along the path. says the derivative of a composition is the product of the pieces' derivatives:
Each stage only needs its own local derivative. Multiply them along the path from the parameter to the loss.
A one-neuron model by hand
ReadScore , probability , loss .
The local derivatives are
Multiply them and most of it cancels:
That is the gradient logistic regression uses, summed over rows: .
Many outputs: the Jacobian
ReadWhen a stage maps a vector to a vector, its derivative is a matrix, the JacobianJacobianFor a function with several outputs, the table of every output's derivative with respect to every input.: entry is .
The chain rule keeps its shape: the Jacobian of a composition is the product of the Jacobians, . A deep network is a long product of Jacobians, one per layer. The order you multiply them in turns out to matter enormously for cost, which is the subject of the next lesson.
Check: gradients
CheckpointCheck yourself
. What is at ?
Show the answer
Answer: 12
.
Check yourself
Which statement about the gradient at a point is true?
Show the answer
Answer: a
The gradient is a local quantity: steepest ascent right here. It need not point at the minimum, which is why stretched bowls make descent zigzag.
Check yourself
. Use the chain rule: what is ?
Show the answer
Answer: 24
Outer derivative , inner derivative 3. At : .
Check yourself
In the one-neuron logistic model, the prediction is for a row with and . What is ?
Show the answer
Answer: a
. It is negative, so a gradient step raises and pushes further toward 1.
Check yourself
A layer maps 4 inputs to 3 outputs. What shape is its Jacobian?
Show the answer
Answer: a
One row per output, one column per input: .
Recap
Recap- The gradient stacks partial derivatives and points in the direction of steepest increase.
- The chain rule multiplies local derivatives along the path from parameter to loss.
- For vector stages the local derivatives are Jacobians, and a network is a product of them.
Concepts in this lesson