Derivatives, gradients and the chain rule
How a tiny change in a parameter changes the loss
Learning is the question "if I nudge this parameter, does the loss go down?" asked millions of times. The derivative answers it for one variable, the gradient for all of them at once, and the chain rule lets you answer it for a function built from thousands of simpler pieces — which is what a neural network is. Backpropagation (4.4 Backpropagation, fully derived) is the chain rule organized on a graph, autograd (2.4 PyTorch I: tensors, autograd and the training loop) is software that applies it, and gradient descent (1.10 Optimization: from gradient descent to Adam) is what you do with the answer.
You'll be able to
- Differentiate the functions that appear in models (powers, exp, log, sigmoid, tanh, ReLU) and explain the derivative as a local linear approximation
- Compute partial derivatives and gradients, and prove that the gradient is the direction of steepest ascent
- Apply the chain rule through a composition and on a computation graph (multiply along paths, add over paths)
- Verify an analytic gradient with a central finite difference and interpret the relative error
Key ideas
- f(x + h) ≈ f(x) + f′(x)h: the derivative is the slope of the best local linear approximation.
- σ′ = σ(1 − σ) with maximum 1/4; tanh′ = 1 − tanh²; ReLU′ is 0 or 1 — the derivatives that govern vanishing gradients.
- The gradient ∇f collects all partial derivatives; D_u f = ∇f · u, so ∇f is the direction of steepest ascent and is perpendicular to level sets.
Show 4 more key ideas
- Chain rule: multiply derivatives along a path, add over paths — on a computation graph this is backpropagation.
- Worked neuron: ∂L/∂w = 2(a − y) · a(1 − a) · x; a gradient step lowered the loss from 0.0723 to 0.0669.
- Check analytic gradients with the central difference (f(x+ε) − f(x−ε))/2ε, ε ≈ 1e−5; relative error below 1e−7 passes.
- Learning = model + loss + gradient (chain rule) + update θ ← θ − η∇L, repeated.
Suggested path through this lesson
The derivative is the best local linear approximation
The derivative of at is defined as , but the way to think about it is
The derivative is the slope of the line that best matches near : how much the output moves per unit nudge of the input. Training a model is nothing more than asking, for each parameter, whether a nudge lowers the loss — and the derivative is the answer. The rules you need are few:
| function | derivative | note |
|---|---|---|
| power rule | ||
| , | , | exponentials and logs are everywhere in losses |
| the sigmoid's derivative reuses its own output | ||
| same trick | ||
| if , else | not differentiable at 0; libraries pick 0 | |
| product rule | ||
| chain rule — the one that runs deep learning |
Math check. . Its maximum is at — the fact behind vanishing gradients in 4.2 Activation functions.
Partial derivatives and the gradient
A loss depends on millions of parameters, so we need derivatives of functions of many variables. The partial derivative nudges only and holds the others fixed. Collecting them gives the gradient
and the linear approximation becomes . The rate of change along a unit direction — the directional derivative — is by 1.1 Vectors, dot products and norms. It is maximized when , that is, when points along . So the gradient is the direction of steepest ascent, its norm is the steepest slope, and is the direction of steepest descent. Moving along a level set (where is constant) gives , so the gradient is perpendicular to level sets. Gradient descent, , is the greedy move downhill (1.10 Optimization: from gradient descent to Adam).
Example: has . At the gradient points mostly along because the bowl is steeper there; the level curves are ellipses, and the gradient crosses them at right angles.
The chain rule, on a graph
Deep networks are compositions, so the chain rule is the law of the land. In one variable, if and then : rates multiply along a chain. With several intermediate variables that each depend on ,
Multiply along each path, add over paths. Draw the computation as a graph — inputs at the bottom, loss at the top — and the rule becomes mechanical: the derivative of the output with respect to any node is the sum, over all paths to the output, of the product of edge derivatives. Computing these from the output backward and reusing shared sub-results is backpropagation (4.4 Backpropagation, fully derived); the forward pass computes values, the backward pass computes gradients, and autograd libraries do both (2.4 PyTorch I: tensors, autograd and the training loop).
Intuition. A derivative is a conversion rate: "one unit of buys units of , and one unit of buys units of ", so one unit of buys units of . Chains multiply rates; forks add them.
Worked example: one neuron, one example
Let , and — a single sigmoid neuron trained with squared error on one example. Along the path ,
Numbers: , , , . Forward: , , . Backward: ; ; so and . A gradient step with moves to and to ; recomputing, , , . The loss went down, as the negative gradient promised. Every parameter of every network is updated by exactly this logic, only with longer chains (4.4 Backpropagation, fully derived).
import numpy as np
sig = lambda z: 1 / (1 + np.exp(-z))
x, y, w, b = 2.0, 1.0, 0.5, 0.0
z = w * x + b; a = sig(z); L = (a - y) ** 2
dL_dw = 2 * (a - y) * a * (1 - a) * x # -0.2115
dL_db = 2 * (a - y) * a * (1 - a) # -0.1057
Numerical gradient checking
Derivations have bugs. The cure is to compare against a finite difference:
the central difference, whose error is (the one-sided version is only ). Use in double precision and compare with the relative error : below is a pass, above is a bug. It costs two function evaluations per parameter, so it is a test, not a training method — but it is the test every autograd engine is validated against (4.4 Backpropagation, fully derived, 4.8 Training dynamics and debugging).
From gradients to learning
Put the pieces together: a model , a loss averaged over data, the gradient computed by the chain rule on the model's computation graph, and the update . Repeat. That loop, with better step-size rules (1.10 Optimization: from gradient descent to Adam) and better architectures, is all of deep learning. The next lesson extends the calculus to vectors and matrices so that the chain rule can be written for whole layers at once.
Where this shows up later
- Jacobians, Hessians and matrix calculus — 1.5 Multivariable and matrix calculus; gradient descent and its variants — 1.10 Optimization: from gradient descent to Adam.
- Autograd in PyTorch — 2.4 PyTorch I: tensors, autograd and the training loop; backpropagation fully derived — 4.4 Backpropagation, fully derived; gradient checks as a debugging tool — 4.8 Training dynamics and debugging.
- Saturating derivatives cause vanishing gradients — 4.2 Activation functions, 6.4 Recurrent neural networks and BPTT.
Common pitfalls (4)
- Forgetting the inner derivative when applying the chain rule (differentiating σ(wx + b) as σ′ only, without the factor x).
- Using one-sided finite differences, whose O(ε) error can hide a real bug — use the central difference.
- Treating the gradient as pointing 'toward the minimum'; it points in the direction of steepest local increase, which is not the same thing in a curved valley.
- Picking ε too small in gradient checks so that floating-point cancellation dominates (1e−10 in float32 is noise).
Recap: all 7 key points
- f(x + h) ≈ f(x) + f′(x)h: the derivative is the slope of the best local linear approximation.
- σ′ = σ(1 − σ) with maximum 1/4; tanh′ = 1 − tanh²; ReLU′ is 0 or 1 — the derivatives that govern vanishing gradients.
- The gradient ∇f collects all partial derivatives; D_u f = ∇f · u, so ∇f is the direction of steepest ascent and is perpendicular to level sets.
- Chain rule: multiply derivatives along a path, add over paths — on a computation graph this is backpropagation.
- Worked neuron: ∂L/∂w = 2(a − y) · a(1 − a) · x; a gradient step lowered the loss from 0.0723 to 0.0669.
- Check analytic gradients with the central difference (f(x+ε) − f(x−ε))/2ε, ε ≈ 1e−5; relative error below 1e−7 passes.
- Learning = model + loss + gradient (chain rule) + update θ ← θ − η∇L, repeated.
Watch
Why calculus is about tiny nudges; watch before any formula.
Read
Exercises
- By hand: a tanh neuron
Repeat the worked example with instead of the sigmoid, for , , , : compute the forward values, , , and the loss after one step with . Expected: the loss decreases; your derivative uses .
- NumPy: a gradient checker
Write
numgrad(f, x, eps=1e-5)that returns the central-difference gradient of a scalar function of a vector. Test it on (gradient ) and on the neuron loss with parameters . Expected: relative errors below for both. - Experiment: gradients are perpendicular to contours
For , draw the level curves with
plt.contourand the gradient field withplt.quiveron a grid. Then run 20 gradient-descent steps from with and overlay the path. Expected: arrows cross contours at right angles, and the path zigzags in the steep direction before sliding along — the conditioning effect you will study in 1.10.
Quiz
Flashcards
These cards are now in your spaced-repetition queue. Review all due cards →