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:

f′(x)=lim⁡h→0f(x+h)−f(x)hf'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}

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:

FunctionDerivativeWhere it appears
xⁿnxⁿ⁻¹Polynomial features
eˣeˣSoftmax, log-likelihood
ln(x)1/xCross-entropy loss
σ(x) = 1/(1+e⁻ˣ)σ(x)(1 − σ(x))Sigmoid activation
max(0, x)1 if x > 0, 0 if x < 0ReLU 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:

∇f(w1,w2,…,wn)=(∂f∂w1, ∂f∂w2, …, ∂f∂wn)\nabla f(w_1, w_2, \ldots, w_n) = \left(\frac{\partial f}{\partial w_1},\, \frac{\partial f}{\partial w_2},\, \ldots,\, \frac{\partial f}{\partial w_n}\right)

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):

w←w−η⋅∇L(w)\mathbf{w} \leftarrow \mathbf{w} - \eta \cdot \nabla L(\mathbf{w})

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.

3Blue1Brown's visual explanation of how gradient descent navigates the loss landscape.

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:

ddx[f(g(x))]=f′(g(x))⋅g′(x)\frac{d}{dx}\bigl[f(g(x))\bigr] = f'(g(x)) \cdot g'(x)

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:

∂L∂x=∂L∂hn⋅∂hn∂hn−1⋯∂h2∂h1⋅∂h1∂x\frac{\partial L}{\partial x} = \frac{\partial L}{\partial h_n} \cdot \frac{\partial h_n}{\partial h_{n-1}} \cdots \frac{\partial h_2}{\partial h_1} \cdot \frac{\partial h_1}{\partial x}

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 chain rule applied to neural networks - the actual calculus behind backpropagation.

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.

J=[∂f1∂x1∂f1∂x2∂f2∂x1∂f2∂x2]\mathbf{J} = \begin{bmatrix} \dfrac{\partial f_1}{\partial x_1} & \dfrac{\partial f_1}{\partial x_2} \\[6pt] \dfrac{\partial f_2}{\partial x_1} & \dfrac{\partial f_2}{\partial x_2} \end{bmatrix}

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:

H=[∂2f∂w12∂2f∂w1∂w2∂2f∂w2∂w1∂2f∂w22]\mathbf{H} = \begin{bmatrix} \dfrac{\partial^2 f}{\partial w_1^2} & \dfrac{\partial^2 f}{\partial w_1 \partial w_2} \\[6pt] \dfrac{\partial^2 f}{\partial w_2 \partial w_1} & \dfrac{\partial^2 f}{\partial w_2^2} \end{bmatrix}

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.

Further reading

Search Data Science After Dark

Search across prompts, models, papers, guides, and library topics.