M4 · Derivatives, partial derivatives and the gradient
From the slope of a curve to the direction of steepest ascent of a multivariable function, and to the gradient of a loss function.
Mastering AI · Required Maths · Week 2 (calculus) · Part 4
Goal of this step
Training a model means looking for the parameters that minimize the loss. To know which way to move the parameters, we need one thing: the gradient. This calculus week starts with it.
By the end of this article you’ll be able to:
- read a derivative as a local slope and compute it numerically;
- compute partial derivatives and assemble a gradient;
- explain why the gradient points in the direction of steepest ascent;
- derive by hand the gradient of the MSE and the log-loss, and check it numerically.
Key concepts
1. The derivative: a local slope
Intuition. Zoom in far enough on a smooth curve and it starts looking like a straight line. The slope of that line is the derivative. It says: “if I nudge a tiny bit, how much does move?”.
It is the limit of the slopes of secant lines:
In the figure, the secant between and has slope 4, the one between and slope 2.25. As goes to 0, we get the tangent with slope .
Rules you use all the time:
| Function | Derivative |
|---|---|
| (chain rule, see M5) |
Numerically, we approximate the derivative with finite differences. The centered version is far more accurate than the forward one:
The forward version’s error is proportional to , the centered one’s to (we’ll prove it with Taylor in M6). But beware: if is too small, floating-point rounding errors take over.
2. Partial derivatives and the gradient
Intuition. A loss depends on many parameters . The partial derivative measures the effect of a small change in alone, all others frozen. The gradient stacks all partial derivatives into a vector:
Example: for , we get , so .
3. Why the gradient points uphill fastest
Intuition. Standing on a hill, which way climbs fastest? The gradient answers.
Taking a small step in a unit direction changes the function by about:
That’s a dot product (M1): it’s largest when is aligned with , where it equals . It’s zero when is perpendicular to the gradient: you stay at the same height. Three consequences:
- gives the direction of steepest ascent, and the slope in that direction;
- gives the direction of steepest descent: the whole idea of gradient descent (M10);
- the gradient is perpendicular to level sets (the curves where is constant).
In the figure, the ellipses are level sets of . The red arrows (the gradient) cross each ellipse at a right angle. The blue path applies the rule : it heads to the minimum, quickly in the vertical direction (where is steeper), then more slowly horizontally.
4. The gradients of the losses we use
MSE of linear regression. With :
Setting it to zero gives : exactly the normal equations we found geometrically in M2.
Log-loss of logistic regression. With and , we use a remarkable property of the sigmoid:
Differentiating , almost everything cancels:
The gradient is just the sum of the examples, weighted by the prediction error . With regularization , add . That’s the formula the logistic regression of part 16 uses.
Code example
Finite differences: forward vs centered
import numpy as np
def f(x):
return x**3 - 2 * x
def f_prime(x):
return 3 * x**2 - 2
x0 = 1.0
print("exact derivative:", f_prime(x0))
for h in (1e-1, 1e-3, 1e-5, 1e-8, 1e-12):
forward = (f(x0 + h) - f(x0)) / h
centered = (f(x0 + h) - f(x0 - h)) / (2 * h)
print(f"h={h:.0e} forward: error={abs(forward - f_prime(x0)):.2e} centered: error={abs(centered - f_prime(x0)):.2e}")
| Forward error | Centered error | |
|---|---|---|
The forward version gains a factor of 100 when is divided by 100 (error in ). The centered one gains a factor of 10,000 (error in ). Below , rounding errors take over again: at , both are poor.
Numerical gradient and direction of steepest slope
import numpy as np
def g(w):
return w[0] ** 2 / 4 + w[1] ** 2
def grad_g(w):
return np.array([w[0] / 2, 2 * w[1]])
def numerical_grad(F, w, h=1e-5):
gr = np.zeros_like(w)
for i in range(len(w)):
e = np.zeros_like(w)
e[i] = h
gr[i] = (F(w + e) - F(w - e)) / (2 * h)
return gr
w = np.array([2.0, 1.0])
print("exact gradient :", grad_g(w))
print("numerical gradient:", numerical_grad(g, w).round(6))
# try 360 unit directions: which one makes g increase fastest?
angles = np.linspace(0, 2 * np.pi, 360, endpoint=False)
dirs = np.stack([np.cos(angles), np.sin(angles)], axis=1)
slopes = dirs @ grad_g(w)
best = dirs[np.argmax(slopes)]
print("steepest direction:", best.round(3),
"| normalized gradient:", (grad_g(w) / np.linalg.norm(grad_g(w))).round(3),
"| max slope:", slopes.max().round(3), "= ||grad||:", np.linalg.norm(grad_g(w)).round(3))
The numerical gradient is exactly . Among 360 directions, the best is , less than a degree from the normalized gradient , and the maximum slope is .
Log-loss gradient: analytic vs numerical
We check the formula on simulated data, then follow and watch the loss fall.
import numpy as np
rng = np.random.default_rng(0)
n, d = 500, 3
X = rng.normal(size=(n, d))
w_true = np.array([1.5, -2.0, 0.5])
def sigmoid(z):
return 1 / (1 + np.exp(-z))
y = (rng.random(n) < sigmoid(X @ w_true)).astype(float)
def log_loss(w, lam=0.0):
p = sigmoid(X @ w)
eps = 1e-12
return -np.mean(y * np.log(p + eps) + (1 - y) * np.log(1 - p + eps)) + lam / 2 * w @ w
def grad_log_loss(w, lam=0.0):
return X.T @ (sigmoid(X @ w) - y) / n + lam * w
def numerical_grad(F, w, h=1e-5):
gr = np.zeros_like(w)
for i in range(len(w)):
e = np.zeros_like(w)
e[i] = h
gr[i] = (F(w + e) - F(w - e)) / (2 * h)
return gr
w_test = rng.normal(size=d)
for lam in (0.0, 0.1):
ga = grad_log_loss(w_test, lam)
gn = numerical_grad(lambda v: log_loss(v, lam), w_test)
print(f"lambda={lam}: max analytic/numerical gap = {np.abs(ga - gn).max():.1e}")
# follow -gradient: the loss goes down
w_k = np.zeros(d)
for k in range(201):
if k in (0, 1, 5, 20, 50, 200):
print(f"iteration {k:>3}: log-loss = {log_loss(w_k):.4f}, w = {w_k.round(3)}")
w_k = w_k - 1.0 * grad_log_loss(w_k)
The gap between analytic and numerical gradients is around : the formula is right. Descent takes the log-loss from ( , the loss of a model that always says 50%) to in 200 iterations, with . The signs and proportions of are recovered; the remaining gap comes from sampling noise on 500 examples.
This finite-difference check (the gradient check) is a habit to keep: whenever you write a gradient by hand, compare it with its numerical version.
Hands-on exercise
- By hand. Compute for and (the softplus function). Check with the centered difference. What do you notice about softplus’s derivative?
- The MSE gradient. Write
grad_mse(w)for a linear regression, check it withnumerical_grad, then solve withnp.linalg.solveand compare with M2’s solution. - Level sets. For
, compute
. Plot a few level sets with
plt.contourand overlay gradient arrows withplt.quiver. Check the orthogonality visually. - The step-size trap. In gradient descent on the log-loss, try a step of 10 then 50 instead of 1. What happens? (We’ll explain it in M6 and M10 with curvature.)
- Running project. For a logistic model on the 9 features of part 14, what is the gradient’s dimension? How many operations does computing it cost for examples? Compare with the cost of a prediction.
Hint for question 1: softplus’s derivative is a function you already know very well.
Further resources
- 3Blue1Brown, Essence of Calculus, episodes 1–4: the derivative as a slope, animated.
- Khan Academy, Multivariable calculus, chapter Gradient and directional derivatives.
- Deisenroth et al., Mathematics for Machine Learning, chapter 5 (vector calculus), sections 5.1–5.3.
- CS231n (Stanford), notes on gradient checking.
What you should be able to do before moving on
- Explain a derivative as a local slope, with a drawing
- Compute a derivative numerically and pick a sensible
- Compute a gradient by hand for a multivariable function
- Explain with the dot product why following lowers
- Derive as the gradient of the log-loss
- Run a gradient check systematically
Next part: M5 · The chain rule and backpropagation. Differentiating a composition of functions is exactly what a neural network does during training.