Optimization and Calculus for Machine Learning

Gradient Descent

Gradient descent as a local movement rule for minimizing loss functions.

Lesson
26

Goal

Understand gradient descent as a local movement rule for minimizing a loss function: look at the current slope of the loss landscape, then move parameters in the opposite direction.

By the end of this lesson, you should be able to explain the update rule

theta_next = theta_current - learning_rate * gradient

and connect each part of that rule to geometry, loss functions, and machine learning training loops.

Gradient descent follows local downhill steps toward lower loss.A loss curve with gradient descent steps moving toward a minimum, and a contour map showing a path through parameter space.One-dimensional lossthetalossminimumsteps shrink as slope shrinksTwo-parameter pathwbpath depends on geometry
Gradient descent uses only local slope information. The loss defines the surface; the optimizer defines the path.

Motivation

The previous lessons built a sequence of questions:

Gradient descent answers the next question:

How do the parameters actually move?

Once we have a loss function, the problem becomes an optimization problem. We have a parameter vector θ\theta, a scalar loss L(θ)L(\theta), and a desire to find parameter values that make the loss small. Gradient descent is the simplest reusable movement rule:

  1. Stand at the current parameter value.
  2. Measure the local uphill direction.
  3. Step downhill.
  4. Repeat.

That is the whole idea. The power comes from the fact that the same rule works for a one-dimensional quadratic, linear regression, logistic regression, and large neural networks. The details become harder, but the invariant remains stable:

A loss function defines the landscape. An optimizer defines the path through it.

Prerequisites

Notation / Symbol Table

SymbolPlain-English nameMeaning
θ\thetaparametersThe values the model can adjust during training.
θt\theta_tcurrent parametersThe parameter vector at optimization step tt.
θt+1\theta_{t+1}next parametersThe parameter vector after one update.
L(θ)L(\theta)loss functionA scalar function measuring how bad the current parameters are.
L(θt)\nabla L(\theta_t)gradient at the current pointThe direction of steepest local increase of the loss.
L(θt)-\nabla L(\theta_t)descent directionThe direction of steepest local decrease of the loss.
η\etalearning rateA positive scalar controlling step size.
Δθ\Delta \thetaparameter updateThe change added to the current parameters.
L(θt)\lVert \nabla L(\theta_t) \rVertgradient magnitudeHow steep the loss is near the current parameters.

The basic gradient descent update is:

θt+1=θtηL(θt).\theta_{t+1} = \theta_t - \eta \nabla L(\theta_t).

Read this as:

The next parameters equal the current parameters minus a scaled version of the current uphill direction.

Core Ideas

The Loss Landscape

A loss function turns parameter choices into a number:

L:ΘR.L : \Theta \to \mathbb{R}.

For each possible parameter vector θ\theta, the loss L(θ)L(\theta) tells us how bad that choice is. Low loss is good. High loss is bad.

Geometrically, you can imagine the loss as a landscape over parameter space:

The optimizer does not need to see the whole landscape at once. Gradient descent is local. It asks:

At my current location, which direction increases loss fastest?

Then it moves the other way.

The Gradient Points Uphill

The gradient L(θ)\nabla L(\theta) points in the direction of steepest local increase of LL.

For a small movement Δθ\Delta \theta, the local linear approximation is:

L(θ+Δθ)L(θ)+L(θ)TΔθ.L(\theta + \Delta \theta) \approx L(\theta) + \nabla L(\theta)^T \Delta \theta.

The term

L(θ)TΔθ\nabla L(\theta)^T \Delta \theta

is a dot product. It measures how aligned your movement is with the gradient.

This is the first important geometric compression:

Gradient descent is dot-product geometry applied to a loss landscape.

The Update Rule

Since the gradient points uphill, gradient descent moves downhill:

Δθ=ηL(θt).\Delta \theta = -\eta \nabla L(\theta_t).

Then:

θt+1=θt+Δθ.\theta_{t+1} = \theta_t + \Delta \theta.

Substituting the update gives:

θt+1=θtηL(θt).\theta_{t+1} = \theta_t - \eta \nabla L(\theta_t).

The minus sign is not decorative. It is the whole descent part of gradient descent.

The Learning Rate Is A Trust Scale

The learning rate η\eta controls how much we trust the local gradient.

A gradient is local information. It says what the loss does near the current point. It does not promise that the same direction remains good forever.

So the learning rate asks:

How far should I move before checking the slope again?

If η\eta is too small, training is stable but slow. If η\eta is too large, training can overshoot, oscillate, or diverge.

This connects directly to a point from Lesson 17. If we multiply the loss by 100:

L(θ)=100L(θ),L'(\theta) = 100L(\theta),

then:

L(θ)=100L(θ).\nabla L'(\theta) = 100\nabla L(\theta).

The minimizer has not moved, but gradient descent steps become 100 times larger unless the learning rate is adjusted.

That distinction matters:

The optimum depends on the loss landscape. The path depends on both the landscape and the optimizer settings.

Interactive check

Learning Rate Changes The Path

Move the learning rate. The minimum stays at (w, b) = (2, -1), but the optimizer path can become slow, smooth, oscillatory, or unstable.

Status
Converging cleanly
Final loss
0.0189
Gradient descent path under the selected learning rate.wbminimum

Convergence Intuition

Gradient descent is usually trying to reach a point where the gradient is zero or close to zero:

L(θ)0.\nabla L(\theta) \approx 0.

For a simple convex bowl, this means we are near the global minimum. For a nonconvex neural network landscape, it can mean a local minimum, a saddle point, or a flat region.

This is why optimization vocabulary is careful:

Gradient descent does not magically know the global answer. It follows local information repeatedly.

Geometry And Conditioning

Some loss landscapes are shaped like round bowls. Others are long, narrow valleys.

In a round bowl, the same learning rate works similarly in every direction. In a narrow valley, one direction may be steep while another is shallow. A learning rate that is safe for the steep direction may be painfully slow for the shallow direction.

This should feel connected to covariance geometry:

This is one reason whitening, normalization, and adaptive optimizers appear so often in machine learning. They try, in different ways, to make the geometry easier to move through.

Objective Versus Optimizer

Keep these separate:

ConceptQuestion answered
Loss functionWhat are we trying to minimize?
RegularizationWhat parameter choices should be discouraged?
Gradient descentHow do we move through parameter space?
Learning rateHow large is each movement?
Early stoppingWhich point along the path should we keep?
DropoutHow do we perturb training so representations become less brittle?

This separation prevents a common confusion. Regularization, dropout, early stopping, and gradient descent can all affect training, but they are not the same kind of object.

Worked Examples

Example 1: A One-Dimensional Quadratic

Let:

L(θ)=(θ3)2.L(\theta) = (\theta - 3)^2.

This loss is minimized at θ=3\theta = 3.

The gradient in one dimension is just the derivative:

dLdθ=2(θ3).\frac{dL}{d\theta} = 2(\theta - 3).

Start at:

θ0=0\theta_0 = 0

and choose:

η=0.1.\eta = 0.1.

Step 1:

L(θ0)=2(03)=6.\nabla L(\theta_0) = 2(0 - 3) = -6. θ1=00.1(6)=0.6.\theta_1 = 0 - 0.1(-6) = 0.6.

The gradient is negative, so the downhill step moves to the right.

Step 2:

L(θ1)=2(0.63)=4.8.\nabla L(\theta_1) = 2(0.6 - 3) = -4.8. θ2=0.60.1(4.8)=1.08.\theta_2 = 0.6 - 0.1(-4.8) = 1.08.

Step 3:

L(θ2)=2(1.083)=3.84.\nabla L(\theta_2) = 2(1.08 - 3) = -3.84. θ3=1.080.1(3.84)=1.464.\theta_3 = 1.08 - 0.1(-3.84) = 1.464.

The steps get smaller as θ\theta approaches 3 because the slope gets smaller.

Example 2: A Two-Parameter Bowl

Let:

L(w,b)=(w2)2+4(b+1)2.L(w, b) = (w - 2)^2 + 4(b + 1)^2.

The minimum is at:

(w,b)=(2,1).(w, b) = (2, -1).

The gradient is:

L(w,b)=[2(w2)8(b+1)].\nabla L(w, b) = \begin{bmatrix} 2(w - 2) \\ 8(b + 1) \end{bmatrix}.

Start at:

(w0,b0)=(0,0)(w_0, b_0) = (0, 0)

with:

η=0.1.\eta = 0.1.

Compute the gradient:

L(0,0)=[48].\nabla L(0, 0) = \begin{bmatrix} -4 \\ 8 \end{bmatrix}.

The update is:

[w1b1]=[00]0.1[48]=[0.40.8].\begin{bmatrix} w_1 \\ b_1 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \end{bmatrix} - 0.1 \begin{bmatrix} -4 \\ 8 \end{bmatrix} = \begin{bmatrix} 0.4 \\ -0.8 \end{bmatrix}.

The bb-direction moved more because the loss is steeper in that direction. The factor 4 in the original loss created a larger gradient component for bb.

This is the beginning of conditioning intuition. Directional scale affects how gradient descent moves.

Why This Shows Up In ML

Machine learning training is usually:

  1. Choose a model with parameters θ\theta.
  2. Choose a loss L(θ)L(\theta).
  3. Compute or estimate L(θ)\nabla L(\theta).
  4. Update θ\theta.
  5. Repeat until some stopping rule says enough.

For linear regression, the loss may be squared error. For probabilistic models, it may be negative log-likelihood. For MAP estimation or regularized learning, the loss may include a penalty.

Gradient descent is the movement rule that turns those objectives into training procedures.

In code, the training loop often looks conceptually like this:

theta = theta.map((value, index) => value - learningRate * gradient[index]);

That single line hides the central geometry:

The next lessons add realism. Stochastic gradient descent estimates the gradient from a mini-batch instead of the whole dataset. Momentum remembers part of the previous movement. Adaptive optimizers change the effective learning rate by parameter.

Common Confusions

Exercises

Use Exercise Set 26 - Gradient Descent.

The most important practice is to keep the symbol meanings visible while computing updates. Do not rush to the formula before naming the objects:

Implementation Exercise

Implement a first-principles TypeScript gradient descent helper for a vector-valued parameter:

type GradientDescentOptions = {
  initialTheta: number[];
  gradient: (theta: number[]) => number[];
  learningRate: number;
  steps: number;
};

The helper should return the full parameter history, not only the final parameter vector. That makes the optimization path inspectable.

Use a quadratic objective first:

L(w,b)=(w2)2+4(b+1)2.L(w, b) = (w - 2)^2 + 4(b + 1)^2.

Then check that the path moves toward (2,1)(2, -1).

Reflection Questions

Next Lesson

Next: Stochastic Gradient Descent.

The bridge is simple: full gradient descent uses the whole dataset to compute the gradient, while stochastic gradient descent uses one example or a mini-batch to estimate the gradient more cheaply and more noisily.