Lesson 1 · Warm-up
Warm-up: functions and graphs
Skip this chapter if you can do these three things
- Read a value off a graph.
- Say what the slope of means.
- Say what is.
If all three feel easy, go on to vectors.
DigressionSelf-check answersOpen this after you've tried the three questions. · 1 min
- Find the input on the x-axis. Go up to the curve, then across to the y-axis. That number is the output.
- Each time goes up by 1, goes up by 2. The is the value of at .
- , because .
The problem
You run a pizza shop. Making a pizza takes 5 minutes, and the driver needs about 4 minutes per km. So a 3 km delivery takes minutes. You now have a rule: give it a distance, and it gives back a time.
Functions and their graphs
A function is a rule that turns each input into exactly one output. Think of a machine: a distance goes in, a time comes out. Press play below.
Press play to feed distances into the machine.
Pairs so far: none
We name the machine and write . So ("f of 3") and . We often call the output and write .
The graph of is every point . The dots in the figure are a few of them. You can read a graph both ways: up from an input to the output, or across from an output down to the input.
Predict first
Using f(x) = 5 + 4x, what is the furthest you can deliver within 25 minutes?
Straight lines
A function whose graph is a straight line is a linear function:
- is the slope: how much the output goes up when goes up by 1. Here it's minutes per extra km.
- is the intercept: the output at , where the line meets the y-axis. Here it's the prep time.
Later in the book, and become a weight and a bias, the numbers a model learns. Move the sliders, and drag the point along the line.
At 3 km: 5 + 4 × 3 = 17 min
Slope 4: each extra km adds 4 min. Intercept 5: a 0 km order still takes 5 min.
On a straight line, the slope is the same everywhere. Going from 1 km to 2 km adds minutes, and so does going from 8 km to 9 km.
Predict first
The driver gets twice as slow: the slope goes from 4 to 8. Does a 4 km delivery take twice as long?
Two meanings of “linear”
Strictly, mathematicians call affine and save "linear" for lines through the origin (). ML usually calls linear, and so does this book. The chapter on matrices as transformations uses the strict meaning.
Curves
In heavy traffic, each extra km is slower than the one before. Say the time is . The part adds 2 minutes at 2 km, but 32 minutes at 8 km.
Predict first
In heavy traffic, does going from 2 km to 3 km add the same time as going from 7 km to 8 km?
From 2 km to 3 km:
light traffic adds 4 min · heavy traffic adds 6.5 min
Light: 5 + 4 × km. Heavy: 5 + 4 × km + 0.5 × km².
A function whose graph is not a straight line is a nonlinear function. A model built only from linear functions can only draw straight lines, so models need nonlinear pieces to fit curves.
On a curve the slope keeps changing. The slope at one point is called the derivative, coming later in this section.
Repeated doubling
Some things grow by multiplying, not adding. Say a post about your pizza goes viral, and orders double every day: 1, 2, 4, 8, 16, …
Predict first
Orders double every day. After 10 days there are about 1,000 times as many. After 20 days, how many times as many?
Step through the days below. First plot the orders, then switch to the doublings tab.
Day 0: 1 = 1 orders
Doublings counted: 0, so log₂ 1 = 0
After days there are orders. In , the 2 is called the base. Any positive number can be the base. This is exponential growth: each step multiplies by the base, so the steps keep getting bigger.
The power doesn't have to be whole. is the number that gives 2 when you multiply it by itself, . So makes sense for any , and its graph is a smooth curve.
Logs: counting doublings
Run the doubling backwards. You have 8 orders. How many doublings got you here? Three: . That count is a log.
The log (logarithm) to base undoes . is the power that makes :
The doublings tab in the figure above used base 2. Each bar's height was of the orders, so it climbed by 1 per day.
Predict first
Roughly what is log₂ 1000?
A few facts follow from "counting doublings":
- : you get 1 with no doublings.
- Between 0 and 1 the log is negative. Halving is a doubling backwards, so .
- and the log of a negative number have no answer. No power of 2 gives 0 or less.
Logs turn multiplying into adding
Multiply 8 by 4. That's 3 doublings followed by 2 more doublings:
So . Set any two numbers below and watch their doublings join end to end.
8 × 4 = 32
log₂ of each: 3 + 2 = 5 = log₂ 32
In general, for any base:
ML relies on this. A model often multiplies many small probabilities together. The product can get so small that the computer rounds it to 0. Adding their logs avoids that, which is why models work with log-probabilities.
DeeperWhy the rule holds when the logs aren't whole numbersEvery positive number is some number of doublings, even a fractional one. · 3 min
What about ? Every positive number is 2 to some power, even if the power isn't whole. To three decimals, and .
Powers add when you multiply: , for any and . So:
Check: . So .
Changing the base only scales every log by the same number: , where . So the rule holds in every base.
The number e
One base turns up again and again: the number . The exponential function is :
- adding 1 to multiplies the output by
- , and
- the output is always positive, even for negative :
The log to base is the natural log, written . In ML papers and in ML code, a plain means . For example, .
Why ? Look at the slope of . For every base, the slope at a point is the height times a fixed number, . For base 2 that number is . For base it's exactly 1, so the slope of equals its height.
That makes the calculus in later chapters as tidy as it can be. You'll see why the slope works out like this in the derivatives chapter.
The figure below has a Base 2 tab and a Base e tab. Drag the point on the blue curve. Its mirror image on the orange curve is the log, and the purple line touching the blue curve shows the slope there.
21 = 2 · log₂ 2 = 1
Slope of the tangent: 1.39. Height: 2. Slope = 0.69 × height.
The log hands back the power you started with. Swap x and y, and one curve becomes the other.
Later, softmax uses to turn any score into a positive number.
DigressionWhy e? Compound interestPay interest more and more often, and the growth creeps up to 2.718, not more. · 3 min
A bank offers 100% interest a year. Put in £1, and a year later you have £2.
Now say the bank pays half the interest every six months. After six months you have £1.50. The second half then earns interest on £1.50, not £1. So you end with £2.25, because .
With payments a year, each one multiplies your money by :
| Payments a year, | Money after a year, |
|---|---|
| 1 | 2 |
| 2 | 2.25 |
| 12 (monthly) | 2.613 |
| 365 (daily) | 2.715 |
| 1,000,000 | 2.718 |
The amount creeps up to and never passes it. So is the result of growth that happens continuously, at a rate of 100%. That's why it turns up wherever things grow or shrink smoothly.
Common misconceptions
" means times ." It doesn't. is the output of for the input .
"Twice the slope means twice the output." Only if the intercept is 0.
" and are the same kind of thing." In the input is the base, so it squares. In the input is the power, so it doubles with every step. At : , but .
"log means base 10." On many calculators it does. In ML, means base .
Recap
- A function turns each input into exactly one output, . Its graph is every point .
- A linear function has a straight graph: slope , intercept . A nonlinear function's slope changes.
- multiplies by the base at every step. The log counts the steps, and it turns products into sums.
- is the exponential whose slope equals its height. Its log is , the log that ML means by a plain .
Section 2 shows how to compute these in PyTorch.