Functions and change
- Lesson 10
- 40 min
- Module 4: How models learn
Lesson 10 of 18 · Essential Mathematics and Statistics for AIFunctions and change
1. Numbers, notation and data summaries
2. Vectors and matrices
3. Probability and conditional reasoning
4. How models learn
5. Sampling and statistical evidence
6. Statistics for model decisions
What you will be able to do
Read a model as a function, and measure how fast its output changes when its input changes. This rate of change is the idea that every training method uses to improve a model.
NotebookOptional: check the numbers in a notebook
The notebook is optional: this lesson works with a calculator. It writes the ticket model and the learning curve as Python functions. You measure slopes and make the gap smaller until the slope becomes the derivative. The guided practice and the independent variation have checks. It uses only Python’s standard library, so it needs no install.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- How to read a unit such as “minutes per hundred words”, from Mathematical reading skills: Units. Every slope in this lesson has a unit of this kind.
- How to read decimals such as 0.01 and 0.375, from Mathematical reading skills: Fractions, decimals and percentages.
What you need to have ready (the notebook needs none of it)
- A calculator. A phone calculator is enough. The independent variation divides 24 by 8.01.
- Your
c03-notes.mdfile. You made it in the “Before you start” box of Mathematical reading skills. Add a heading now:## How models learn. If you cannot find the file, make a new empty file with that name: this module does not use answers from earlier modules.
The notebook was tested in Jupyter with Python 3.14.6 (standard library only).
Larkfield so far
Section titled “Larkfield so far”Larkfield is a fictional online shop for home and garden products. Customer messages become tickets, about 1,200 a week. The team builds AI that suggests a team and a priority for each new ticket.
In this module, the question is simpler. Grace, the support lead, plans the work for each day. She asks: “When a ticket arrives, how many minutes will it take to handle?” Tomás, the AI engineer, starts with the smallest possible model. It uses one input, the length of the ticket. Priya, the data scientist, gives him four handled tickets to learn from:
| Ticket | Length (hundreds of words) | Handling minutes |
|---|---|---|
| T-3101 | 1 | 7 |
| T-3102 | 2 | 13 |
| T-3103 | 3 | 17 |
| T-3104 | 4 | 26 |
Over the three lessons of this module, you watch this model learn. In this lesson, you learn to read it.
Where you meet this in AI
Section titled “Where you meet this in AI”- Every model is a function. A spam filter takes an email and gives a score. A ticket model takes a ticket and gives minutes. You can test, plot and compare models because each one turns inputs into outputs by a fixed rule.
- Training curves. A training dashboard plots the loss (how wrong the model is) after each step. A steep curve means the model still improves quickly. A flat curve means it has stopped improving. You read the slope, not the formula.
- Learning curves. A team plots model accuracy against the amount of labelled data. The slope answers a money question: how many accuracy points does the next 1,000 labelled examples buy?
- Sensitivity. “If the ticket is one hundred words longer, how much does the prediction change?” That is a slope, and it has units.
A model is a function
Section titled “A model is a function”A function is a rule that gives exactly one output for each input. Tomás’s model is this rule:
Read it as “predicted minutes equal w times the length”. The input is the length of the ticket, in hundreds of words. The output is the predicted handling time, in minutes. The number w is a parameter: a number inside the model that training chooses. Training means finding a good value for w.
Tomás tries w = 6. Then a ticket of 300 words (length 3) gets a prediction of 6 × 3 = 18 minutes.
| Ticket | Length (input) | Prediction with w = 6 | Actual minutes |
|---|---|---|---|
| T-3101 | 1 | 6 | 7 |
| T-3102 | 2 | 12 | 13 |
| T-3103 | 3 | 18 | 17 |
| T-3104 | 4 | 24 | 26 |
Text description of the chart
Four dots: T-3101 at length 1 and 7 minutes, T-3102 at 2 and 13, T-3103 at 3 and 17, T-3104 at 4 and 26. A straight line starts at 0 and rises to 30 minutes at length 5. It is the model minutes = 6 × length. The dots are close to the line. T-3103 is 1 minute below the line, and T-3104 is 2 minutes above it.
To read a graph of a function, find the input on the horizontal axis. Go up to the line, and read the output on the vertical axis. For length 2.5, the line is at 15 minutes. The model can answer for any length, also lengths that it never saw.
Slope: change in output per unit of input
Section titled “Slope: change in output per unit of input”Move along the line from length 2 to length 4. The input changes by 2 (hundreds of words). The prediction changes from 12 to 24 minutes, by 12 minutes. So the output changes by 12 ÷ 2 = 6 minutes per hundred words.
This number is the slope: the change in output for each one unit of change in input. It always has units: output units per input unit. Here, the slope says that each extra hundred words adds 6 minutes of work.
Read it as “slope equals the change in y divided by the change in x”. The symbol Δ (the Greek letter delta) means “change in”.
On a straight line, the slope is the same everywhere. Try length 1 to 3: the prediction goes from 6 to 18, so the slope is 12 ÷ 2 = 6 again. For this model, the parameter w is the slope. Changing w makes the line steeper or flatter:
- w = 4
- w = 6
- w = 8
Text description of the chart
Three straight lines start at 0. With w = 4, the line reaches 20 minutes at length 5. With w = 6, it reaches 30. With w = 8, it reaches 40. Each line rises by w minutes for each hundred words.
Which line is best? The dots of the four tickets are closest to the line with w = 6, but “closest” is not a number yet. The next lesson turns it into one.
Curves: the slope changes
Section titled “Curves: the slope changes”Many functions in AI work are not straight lines. Omar, the product manager, wants a better ticket classifier. Agents can label more past tickets to train it, but labelling costs agent time. Priya shows him a learning curve: the accuracy of the classifier after training on more and more labelled tickets.
The numbers below are illustrative, not measured. They follow the rule accuracy = 94 − 24 ÷ x, where x is thousands of labelled tickets. You do not need the rule, only the values:
| Labelled tickets (thousands) | 1 | 2 | 4 | 8 |
|---|---|---|---|---|
| Accuracy (%) | 70 | 82 | 88 | 91 |
From 1,000 to 2,000 tickets, accuracy rises by 12 points: 12 points per thousand. From 4,000 to 8,000, it rises by 3 points over 4 thousand: 0.75 points per thousand. The curve gets flatter. The slope depends on where you measure it.
It also depends on how far you look. From 2,000 to 4,000 tickets, the slope is (88 − 82) ÷ 2 = 3 points per thousand. From 2,000 to 3,000, it is (86 − 82) ÷ 1 = 4. Which is the slope “at 2,000”?
The slope at one point: the derivative
Section titled “The slope at one point: the derivative”Take the point at 2,000 tickets. Draw a straight line through it and a second point a small gap to the right. Such a line through two points of a curve is a secant. Now make the gap smaller and smaller:
| Gap (thousands) | Second point | Accuracy at the second point | Rise | Slope = rise ÷ gap |
|---|---|---|---|---|
| 2 | 4 | 88 | 6 | 3 |
| 1 | 3 | 86 | 4 | 4 |
| 0.5 | 2.5 | 84.4 | 2.4 | 4.8 |
| 0.1 | 2.1 | 82.571 | 0.571 | 5.714 |
| 0.01 | 2.01 | 82.060 | 0.060 | 5.970 |
The slopes come closer and closer to one number: 6. At that limit, the line only touches the curve at the point. That line is the tangent.
The derivative is the slope of the tangent: how fast the output changes per unit of input, for a very small change at one point. Here, the derivative at 2,000 tickets is 6 accuracy points per thousand labelled tickets. Mathematicians write it like this:
Read it as “the derivative of f at x is about the rise from x to x plus h, divided by h”. The gap h must be very small. You never need to compute this limit by hand in this course. Software computes derivatives for you. Your job is to read them.
Explore
Shrink the gap until the line becomes the tangent
Predict first: You make the gap smaller, step by step. Does the slope of the dashed line go up, go down, or stay the same? Where does it stop?
- Point x
- 2
- Output y
- 82
- Gap h
- 2
- Rise (change in y)
- 6
- Slope of the line = rise ÷ gap
- 3
- Tangent slope
- 6
With a gap of 2, the line through the two points has slope 3 (points per thousand tickets). The tangent has slope 6. Make the gap smaller.
Text description of the explorer
The chart shows the learning curve, accuracy = 94 − 24 ÷ x. The axes go from 60% to 100% accuracy and from 0 to 10 thousand labelled tickets. At the start, the point is at 2 thousand tickets and 82% accuracy. The second point is at 4 thousand and 88%. The dashed line through both points has slope 3 points per thousand. The tangent at the point has slope 6. The first slider moves the point between 1 and 8 thousand tickets. You can also drag the point, or focus it and press the arrow keys. The second slider sets the gap: 2, 1, 0.5, 0.2, 0.1 or 0.01. As the gap shrinks, the dashed line turns towards the tangent and its slope rises from 3 to 5.97. At 4 thousand tickets, the tangent slope is 1.5. At 8 thousand, it is 0.375.
Worked example: is more labelling worth it?
Section titled “Worked example: is more labelling worth it?”Omar asks Priya: “Each 1,000 labelled tickets costs about 50 hours of agent time. Where does it stop being worth it?” Priya reads the derivative at three points from the explorer:
| Labelled tickets now | Accuracy | Derivative (points per extra thousand) | What it means |
|---|---|---|---|
| 2,000 | 82% | 6 | The next thousand tickets add about 6 points. Worth it. |
| 4,000 | 88% | 1.5 | About 1.5 points for 50 hours. Maybe. |
| 8,000 | 91% | 0.375 | Less than half a point. Probably not worth it. |
Priya adds one warning. The derivative describes a small change near the point. At 2,000 tickets, the derivative is 6, but the real gain from 2,000 to 3,000 is only 4 points, because the curve bends. For a larger change, measure the curve again at the new point.
Omar decides to label up to about 4,000 tickets, and then spend the agent time on other work.
Guided practice: read a slope at 4,000 tickets
Section titled “Guided practice: read a slope at 4,000 tickets”Use the explorer above. Write each answer in c03-notes.md.
- Make your prediction, then press I have made my prediction.
- Move the point to 4.
- Set the gap to 2.
- Write the rise and the slope of the dashed line. Expected: the second point is at 6 thousand tickets, 90%. The rise is 2, and the slope is 2 ÷ 2 = 1.
- Set the gap to 0.01.
- Write the slope of the dashed line and the tangent slope. Expected: about 1.496, and 1.5.
- Write one sentence that explains the tangent slope, with its units.
Show an example answer
At 4,000 labelled tickets, accuracy is 88%. With a gap of 2, the line goes to 6,000 tickets and 90%. That is a rise of 2 points over 2 thousand: a slope of 1 point per thousand. With a gap of 0.01, the slope is 1.496. The tangent slope is 1.5.
Sentence: “At 4,000 labelled tickets, accuracy rises by about 1.5 points for each extra thousand labelled tickets, for small changes.”
Check: your sentence has a unit (“points per thousand tickets”). You cannot compare a slope without a unit with a cost.
Independent variation: the slope at 8,000 tickets
Section titled “Independent variation: the slope at 8,000 tickets”Now work with the calculator first, and use the explorer only to check. The accuracy at x thousand tickets is 94 − 24 ÷ x.
- Calculate the slope between 8 and 10 thousand tickets.
- Calculate the slope between 8 and 8.01 thousand tickets.
- Explain to Omar, in one sentence, what the second number means for his labelling decision.
Show an example answer
- Accuracy at 8 is 94 − 3 = 91. At 10, it is 94 − 2.4 = 91.6. The rise is 0.6 over a gap of 2, so the slope is 0.3 points per thousand.
- Accuracy at 8.01 is 94 − 24 ÷ 8.01 = 91.003745. The rise is 0.003745 over a gap of 0.01, so the slope is about 0.3745. The tangent slope in the explorer is 0.375.
- “At 8,000 labelled tickets, another 1,000 tickets add less than half an accuracy point, so 50 hours of agent time buy very little.”
Run Python in your browser
Optional: shrink the gap in Python
This box repeats the secant table for the point at 2,000 tickets. Change point to 4 or 8 and check your answers above. Professionals let a library such as PyTorch or JAX compute derivatives automatically.
Tab adds four spaces. Press Esc, then Tab, to leave the box. Ctrl+Enter runs the code.
Common mistake
Using a slope far from where you measured it
Symptom: a plan says: “At 2,000 labelled tickets, the slope is 6 points per thousand. So 5,000 more tickets will add 30 points and give 112% accuracy.” The number is impossible, but the plan goes to a budget meeting.
Cause: a derivative describes the change near one point. The curve bends, so the slope at 2,000 tickets says nothing reliable about 7,000 tickets.
Fix: use a derivative only for small changes. For a large change, take a small step, measure the slope again at the new point, and repeat. Training methods follow exactly this rule. You see why in the next two lessons.
Check your understanding
Tomás changes the model to minutes = 6.5 × length. What is the slope, with its units? How many more minutes does the model predict for a 400-word ticket than for a 200-word ticket?
Show the answer
The slope is the parameter: 6.5 minutes per hundred words. From 200 words (length 2) to 400 words (length 4), the input changes by 2. The prediction changes by 6.5 × 2 = 13 minutes. Check: 6.5 × 4 = 26 and 6.5 × 2 = 13, and 26 − 13 = 13.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
On a curve, the line through x = 3 and x = 4 has slope 2. The tangent at x = 3 has slope 2.67. You draw a new line from x = 3 with a gap of 0.1. Predict its slope: closer to 2 or closer to 2.67? Why?
Show the answer
Closer to 2.67. A smaller gap puts the second point closer to the first one, so the line through the two points comes closer to the tangent. (These are the learning-curve values: with a gap of 0.1, the slope is about 2.58.)
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
A training dashboard shows the loss after each step. Over the last 2,000 steps, the curve is almost flat. What is its slope, and what does that tell the team?
Show the answer
The slope is about 0: the loss changes very little per step. More training steps of the same kind will probably not make the model much better. The team can stop the run, or change something, for example the data or the settings. A steep downward slope would mean the opposite: the model still improves quickly.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- A model is a function: a rule that turns each input (ticket length) into one output (minutes). Its parameters, such as
w, are numbers that training chooses. - The slope is the change in output per unit of input, with units: 6 minutes per hundred words.
- On a straight line, the slope is the same everywhere. On a curve, it changes from point to point.
- The derivative is the slope of the tangent at one point. A line through two points turns into the tangent when the gap shrinks.
- A derivative describes small changes only. For a large change, step and measure again.
Next, you measure how wrong the model is with one number, the loss, and use its slope to improve w.
Next: Loss and gradients