Calculus and optimisation for AI: how a model improves

Training a model means finding the weights that make its error as small as possible. Calculus tells the model which way is "downhill": a derivative measures how much the error changes when one weight changes a little, and the gradient collects these for every weight. Gradient descent then takes small steps downhill, again and again.

Everyday analogy

Walking down a hill in thick fog. You can't see the bottom, but you can feel the slope under your feet. Step in the steepest downward direction, feel again, step again. How big each step is: that's the learning rate.

Example

A pricing model is off by AED 400 on average. The gradient says: "raise the age weight slightly, lower the region weight slightly". After thousands of tiny steps the average error falls to around AED 60. (Illustrative numbers.)

The key ideas

IdeaMeaningExample
FunctionA rule that turns inputs into an outputerror = f(weights)
DerivativeThe slope at a point: how fast the output changesFor x², the slope is 2x; at x = 3 it's 6
Partial derivativeThe slope for one input while the others stay fixedf(x, y) = x² + 3y → ∂f/∂x = 2x, ∂f/∂y = 3
GradientAll the partial derivatives together; it points uphill∇f = [2x, 3]; step the opposite way
Chain ruleFor a function inside a function, multiply the slopes along the chainExactly what backpropagation does, layer by layer
MinimumWhere the slope is 0 at the bottom of a valleyThe best weights sit at the bottom of the error curve
IntegralThe area under a curve; adding up lots of small piecesProbabilities under a distribution curve

Gradient descent, step by step

The update rule
new weight = old weight − learning rate × gradient

Take a tiny model whose error is E(w) = (w − 3)². The best weight is clearly 3. Start at w = 0 with a learning rate of 0.1. The slope is 2 × (w − 3).

StepWeight wSlope 2(w − 3)Error (w − 3)²
00−69.00
10 − 0.1 × (−6) = 0.60−4.85.76
20.6 + 0.48 = 1.08−3.843.69
31.08 + 0.384 = 1.46−3.072.36
…creeps towards 3→ 0→ 0

Each step moves the weight a little closer to 3 and the error shrinks. Real models do this for millions or billions of weights at once, using the chain rule to get every slope.

Choosing the learning rate

Learning rateWhat happens
Too smallTiny steps; training takes forever
Just rightSteady progress to the bottom
Too bigJumps over the valley, bounces around, or blows up

Types of gradient descent

TypeData used per stepTrade-off
BatchAll the training dataSmooth, but slow and memory-hungry on big data
Stochastic (SGD)One exampleFast and noisy; the noise can help escape bad spots
Mini-batchA small group, e.g. 32 or 64The usual choice: a balance of both

Smarter optimisers

OptimiserIdea
MomentumKeeps rolling in a direction that keeps working, like a ball gathering speed
RMSPropGives each weight its own step size, smaller where slopes are steep
AdamCombines momentum with per-weight step sizes; the default for most deep learning

Bumps in the landscape

Real error surfaces aren't one smooth valley. There are local minima (small dips that aren't the lowest point), saddle points (flat in one direction, sloping in another) and plateaus. In practice, big networks trained with mini-batches and optimisers like Adam usually find solutions that are good enough. See how this fits into training in How a neural network is trained.

Try it yourself

About 10 min

Run gradient descent in a spreadsheet

  1. In A1 type 0 (the starting weight).
  2. In A2 type =A1-0.1*2*(A1-3) and fill it down to row 30.
  3. Change 0.1 to 1.1 and watch what happens.

What you should see: With 0.1 the numbers creep up to 3, the best weight. With 1.1 they swing further and further away: a learning rate that's too big.

Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot) where needed. Never paste private or confidential information.

Test what you learned

Take a quick quiz, or look up any word in the glossary.