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
| Idea | Meaning | Example |
|---|---|---|
| Function | A rule that turns inputs into an output | error = f(weights) |
| Derivative | The slope at a point: how fast the output changes | For x², the slope is 2x; at x = 3 it's 6 |
| Partial derivative | The slope for one input while the others stay fixed | f(x, y) = x² + 3y → ∂f/∂x = 2x, ∂f/∂y = 3 |
| Gradient | All the partial derivatives together; it points uphill | ∇f = [2x, 3]; step the opposite way |
| Chain rule | For a function inside a function, multiply the slopes along the chain | Exactly what backpropagation does, layer by layer |
| Minimum | Where the slope is 0 at the bottom of a valley | The best weights sit at the bottom of the error curve |
| Integral | The area under a curve; adding up lots of small pieces | Probabilities under a distribution curve |
Gradient descent, step by step
new weight = old weight − learning rate × gradientTake 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).
| Step | Weight w | Slope 2(w − 3) | Error (w − 3)² |
|---|---|---|---|
| 0 | 0 | −6 | 9.00 |
| 1 | 0 − 0.1 × (−6) = 0.60 | −4.8 | 5.76 |
| 2 | 0.6 + 0.48 = 1.08 | −3.84 | 3.69 |
| 3 | 1.08 + 0.384 = 1.46 | −3.07 | 2.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 rate | What happens |
|---|---|
| Too small | Tiny steps; training takes forever |
| Just right | Steady progress to the bottom |
| Too big | Jumps over the valley, bounces around, or blows up |
Types of gradient descent
| Type | Data used per step | Trade-off |
|---|---|---|
| Batch | All the training data | Smooth, but slow and memory-hungry on big data |
| Stochastic (SGD) | One example | Fast and noisy; the noise can help escape bad spots |
| Mini-batch | A small group, e.g. 32 or 64 | The usual choice: a balance of both |
Smarter optimisers
| Optimiser | Idea |
|---|---|
| Momentum | Keeps rolling in a direction that keeps working, like a ball gathering speed |
| RMSProp | Gives each weight its own step size, smaller where slopes are steep |
| Adam | Combines 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 minRun gradient descent in a spreadsheet
- In A1 type 0 (the starting weight).
- In A2 type
=A1-0.1*2*(A1-3)and fill it down to row 30. - 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.