Module 6 — Derivatives & Gradient Descent
You now understand that a loss function is a scalar quantifying a model's error. This module addresses the central question of machine learning: how is that quantity minimized? The answer is gradient descent — iteratively following the slope toward lower values. The first prerequisite is the slope itself, which is measured by the derivative.
The derivative: the slope of a function at a point
The derivative of a function is its slope — the rate at which the output changes with respect to the input. Geometrically, it is the slope of the line tangent to the curve at a given point. A steep increasing curve has a large positive derivative; a steep decreasing curve a large negative derivative; a flat region a derivative of zero.
Manual computation of derivatives is not required here; what matters is the single principle underlying training: the sign of the derivative indicates the direction in which the function increases. Where the derivative is positive, the output increases as the input increases; where it is negative, the output decreases. The derivative \( f'(w) \) therefore points in the direction of steepest increase, and its negative, \( -f'(w) \), points in the direction of steepest decrease — the direction in which gradient descent steps to reduce the loss.
Minima: points at which the derivative is zero
A minimum of a function is a point of locally lowest value, at which the slope is zero. The minimum of a loss function corresponds to the best achievable model. Gradient descent is a procedure for reaching such a minimum without prior knowledge of its location, using only the local slope at the current point.
The activity below illustrates this local-information constraint: only the region of the curve at the current point is visible. The global shape is unknown; only the slope at the current position is available. Stepping in the downhill direction repeatedly converges to the minimum.
This activity needs JavaScript. Gradient descent never sees the whole loss surface — it reads the local slope and steps in the opposite (downhill) direction, repeating until the slope is 0.
Move the point along the curve below and observe the tangent line. The slope of the tangent is the derivative at that point; at the minimum of the curve the tangent is horizontal and the slope is \( 0 \).
This activity needs JavaScript. At a minimum the tangent line is horizontal, so the derivative (slope) is \( 0 \) — which is why gradient descent comes to rest there.
Gradient descent: the iterative update rule
From any point on the curve, evaluate the slope and take a small step in the direction of decreasing value, then repeat. This update rule is the basis of essentially all model training:
You will also see this written compactly with an update arrow: \( w \leftarrow w - \eta \cdot \text{slope} \). The arrow \( \leftarrow \) means update — "replace \( w \) with the value on the right." It is an instruction, not an equation (just as \( w = w + 1 \) in code increases \( w \) rather than claiming the two are equal).
Here \( \eta \) (eta) is the learning rate, which determines the step size. If \( \eta \) is too small, convergence is slow; if it is too large, the updates overshoot the minimum and may diverge. The activity below allows you to set the initial point on a loss curve and adjust \( \eta \). Identify the value that reaches the minimum most rapidly without diverging.
This activity needs JavaScript. The lesson below still covers everything.
The gradient: the vector of partial derivatives
Real models have millions of parameters rather than one. The gradient is the collection of partial derivatives — one per parameter — assembled into a vector that points in the direction of steepest increase. Gradient descent steps in the opposite direction. The one-dimensional case demonstrated here generalizes directly: a real model performs gradient descent in a space of millions of dimensions.
Check your understanding
Predict the behavior of gradient descent under various conditions.
This activity needs JavaScript.