Topic 4 of 5 · 5 min read
Derivatives, gradients and composed functions
Goal: Compute the gradient of a small two-variable function and say which direction lowers its value.
A derivative says how much a function's output changes when you nudge an input a tiny bit, and training a model means nudging every weight in the direction that lowers the loss. That one idea runs all of deep learning.
Check yourself · 1 of 3
What is the derivative of f(x) = x^2 at x = 5?
Why it matters
When a training run diverges, a loss turns to NaN or a fine-tune does not move, the cause is often in gradients: too large, too small or missing. You need the vocabulary to read the error and the learning-rate advice that follows.
How to do it
- Slope as sensitivity. For f(x) = x^2, the derivative is 2x. At x = 3 the slope is 6: a nudge of 0.01 in x moves f by about 0.06.
- Partial derivatives. With two inputs, hold one fixed and vary the other. For f(x, y) = x^2 + 3y, df/dx = 2x and df/dy = 3.
- Gradient. Stack the partial derivatives into a vector. At (2, 1) the gradient of f is (4, 3). It points in the direction of steepest increase.
- Gradient descent. To lower f, step against the gradient: new point = old point - learning rate * gradient. With a learning rate of 0.1 from (2, 1) the new point is (1.6, 0.7).
- Chain rule. For a function inside a function, multiply the derivatives. If f(x) = (3x + 1)^2, the inner part 3x + 1 has derivative 3, the outer square has derivative 2 * (3x + 1), and so df/dx = 2(3x + 1) * 3. At x = 1 that is 24. Backpropagation applies this rule layer by layer through a network.
Before and after
Weak: "The loss is 0.8. Increase the weights to lower it." A loss value does not give a direction.
Fixed: compute the gradient of the loss with respect to each weight. A positive gradient means increasing that weight raises the loss, so decrease it. A negative one means increase it.
w, lr = 2.0, 0.1
grad = 2 * w # d(w^2)/dw
w = w - lr * grad # 1.6, closer to the minimum at 0
What goes wrong
- Learning rate too large. With f(w) = w^2, a rate of 1.1 updates w to w - 1.1 * 2w = -1.2w, so w = 2 becomes -2.4, then 2.88, and the loss grows every step.
- Vanishing gradients. Multiplying many derivatives below 1 along a deep chain leaves almost nothing for early layers.
- Sign errors. Adding the gradient climbs the loss instead of lowering it.
Examples
Check a gradient numerically
def f(x, y): return x**2 + 3*y
eps = 1e-5
dfdx = (f(2 + eps, 1) - f(2 - eps, 1)) / (2 * eps)
dfdy = (f(2, 1 + eps) - f(2, 1 - eps)) / (2 * eps)
print(round(dfdx, 3), round(dfdy, 3)) # 4.0 3.0
The numerical result matches the analytic gradient (4, 3). Frameworks use this trick in tests to catch wrong backward code.
A chain of two steps
x = 1.0
u = 3 * x + 1 # inner: u = 4, du/dx = 3
f = u ** 2 # outer: f = 16, df/du = 2u = 8
df_dx = 8 * 3 # chain rule: 24
Each layer only needs its own local derivative; the chain rule multiplies them.
Key points
- A derivative measures how much the output changes per small nudge of an input.
- The gradient stacks all partial derivatives into one vector that points toward the steepest increase.
- Gradient descent moves against the gradient, scaled by the learning rate.
- The chain rule multiplies derivatives along nested functions, and backpropagation is that rule applied through a network.
- Too large a learning rate overshoots and can diverge, while long chains of small derivatives cause vanishing gradients.
Common mistakes
- Moving along the gradient instead of against it when minimising a loss.
- Treating the gradient as a step size rather than a direction scaled by the learning rate.
- Forgetting the inner derivative when applying the chain rule.
- Raising the learning rate to fix slow training without watching for divergence.
Check yourself · 2 of 3
The gradient of a loss with respect to a weight is +0.8. To lower the loss, what should the update do?
Check yourself · 3 of 3
For f(x, y) = x^2 + 3y, what is the gradient at (2, 1)?
Practice items, not counted towards your mastery.
Start here
- Mathematics for Machine Learning (Deisenroth, Faisal, Ong) (opens in a new tab)BookFree
The free book that covers linear algebra, calculus and probability in the order machine learning needs them.
- 3Blue1Brown: Essence of linear algebra (opens in a new tab)VideoFree
Vectors, dot products and matrix multiplication drawn as geometry, the fastest way to build intuition.
- 3Blue1Brown: Essence of calculus (opens in a new tab)VideoFree
Derivatives and the chain rule as pictures of change, without heavy algebra.
More for this topic
- CS231n: Backpropagation, Intuitions (opens in a new tab)CourseFree
Gradients and the chain rule worked through small computation graphs, then the same in code.