Step 5 of 6
Derivatives, gradients & the chain rule
What a derivative measures, how gradients generalize it to many dimensions, and why the chain rule is the engine behind backpropagation.
16 min read
Training a neural network means adjusting millions of parameters to minimize a loss function. To know which way to adjust them, you need derivatives. Calculus gives you the gradient - the direction of steepest ascent - so you can walk downhill.
Derivatives - the rate of change#
A derivative measures how fast a function's output changes when you nudge its input. For a function f(x), the derivative f′(x) is the slope of the tangent line at x:
The derivative is the limit of the slope between two nearby points as they get infinitely close.
If f′(x) > 0, the function is increasing at x. If f′(x) < 0, it's decreasing. If f′(x) = 0, you're at a flat point - possibly a minimum, maximum, or saddle point.
Key derivatives you'll see in ML:
| Function | Derivative | Where it appears |
|---|---|---|
| xⁿ | nxⁿ⁻¹ | Polynomial features |
| eˣ | eˣ | Softmax, log-likelihood |
| ln(x) | 1/x | Cross-entropy loss |
| σ(x) = 1/(1+e⁻ˣ) | σ(x)(1 − σ(x)) | Sigmoid activation |
| max(0, x) | 1 if x > 0, 0 if x < 0 | ReLU activation |
Partial derivatives and gradients#
When a function has multiple inputs - like a loss function that depends on millions of weights - you take partial derivatives: the derivative with respect to one variable while holding the others fixed.
The gradient ∇f collects all partial derivatives into a vector. It points in the direction of steepest ascent:
The gradient is a vector of partial derivatives - one per input dimension. It points uphill; negate it to go downhill.
Gradient descent updates parameters by moving in the opposite direction of the gradient (downhill):
Gradient descent: step in the negative gradient direction, scaled by learning rate η.
The learning rate η controls step size. Too large → you overshoot the minimum and diverge. Too small → training takes forever. This is one of the most important hyperparameters in all of ML.
The chain rule - the engine of backpropagation#
The chain rule is the single most important calculus rule for ML. It tells you how to differentiate a composition of functions - and a neural network is nothing but a composition of functions:
If y = f(g(x)), then dy/dx = f′(g(x)) · g′(x). Derivatives multiply through the chain.
In a neural network with layers h₁ = f₁(x), h₂ = f₂(h₁), ..., output = fₙ(hₙ₋₁), the chain rule chains together:
The gradient flows backward through the network: each layer multiplies its local derivative.
This is backpropagation: compute the loss at the output, then propagate the gradient backward through each layer by multiplying local derivatives. The chain rule makes it efficient - you compute each layer's contribution once and reuse it.
The Jacobian - gradients for vector functions#
When a function maps vectors to vectors (like a neural network layer), the gradient becomes a Jacobian matrix: each row is the gradient of one output with respect to all inputs.
The Jacobian is the matrix of all partial derivatives: row i, column j is ∂fᵢ/∂xⱼ.
In deep learning, you rarely compute the full Jacobian (it's enormous). Instead, you compute Jacobian-vector products (forward mode) or vector-Jacobian products (reverse mode, i.e., backprop). Reverse mode is more efficient when outputs are fewer than inputs - which is exactly the case for a scalar loss with millions of parameters.
The Hessian - second derivatives and curvature#
The Hessian is the matrix of second partial derivatives. While the gradient tells you the slope, the Hessian tells you the curvature - how the slope itself is changing:
The Hessian is the Jacobian of the gradient: Hᵢⱼ = ∂²f/∂wᵢ∂wⱼ.
Second-order optimizers (Newton's method, natural gradient) use the Hessian to take better steps - but computing and storing it for millions of parameters is prohibitive. Approximations like Adam's adaptive learning rates, K-FAC, and Shampoo capture some curvature information without the full Hessian.
- Softmax
- Converts a vector of raw scores into probabilities that sum to 1 - used as the final layer in classification and inside attention mechanisms.
- Cross-entropy loss
- Measures how surprised the model is by the correct answer - the standard loss function for classification tasks.
- Backpropagation
- The algorithm that computes gradients for every parameter by applying the chain rule backward through the network.
- Adam
- An optimizer that adapts the learning rate for each parameter individually, combining momentum with gradient magnitude tracking.
- Jacobian
- A matrix of all partial derivatives of a vector-valued function - generalizes the gradient to functions with multiple outputs.
Where it shows up in ML#
Gradient descent trains everything
Every parameter update in every neural network follows the negative gradient of the loss - scaled by the learning rate.
The chain rule enables backpropagation
Derivatives flow backward through the computation graph, layer by layer, via the chain rule.
Activation derivatives matter
ReLU: gradient is 0 or 1 (dead neurons). Sigmoid: gradient vanishes near 0 and 1. GELU: smooth everywhere.
The Hessian shapes optimization
Curvature determines whether SGD, Adam, or a second-order method converges fastest.