Maths toolkit1 / 17

Lesson 1 · Warm-up

Warm-up: functions and graphs

Skip this chapter if you can do these three things

  1. Read a value off a graph.
  2. Say what the slope of y=2x+1y = 2x + 1 means.
  3. Say what log⁡28\log_2 8 is.

If all three feel easy, go on to vectors.

DigressionSelf-check answersOpen this after you've tried the three questions. · 1 min
  1. Find the input on the x-axis. Go up to the curve, then across to the y-axis. That number is the output.
  2. Each time xx goes up by 1, yy goes up by 2. The +1+1 is the value of yy at x=0x = 0.
  3. log⁡28=3\log_2 8 = 3, because 23=2×2×2=82^3 = 2 \times 2 \times 2 = 8.

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 5+4×3=175 + 4 \times 3 = 17 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.

? kmmachine f5 + 4 × km? min
024681001020304050distance (km)time (minutes)

Press play to feed distances into the machine.

Pairs so far: none

Step 0 of 12
Each input gives one output, and each pair becomes one dot. The last step fills in every distance between the dots.

We name the machine ff and write f(x)=5+4xf(x) = 5 + 4x. So f(3)=17f(3) = 17 ("f of 3") and f(10)=45f(10) = 45. We often call the output yy and write y=f(x)y = f(x).

The graph of ff is every point (x,f(x))(x, f(x)). 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:

f(x)=wx+bf(x) = wx + b
  • ww is the slope: how much the output goes up when xx goes up by 1. Here it's minutes per extra km.
  • bb is the intercept: the output at x=0x = 0, where the line meets the y-axis. Here it's the prep time.

Later in the book, ww and bb become a weight and a bias, the numbers a model learns. Move the sliders, and drag the point along the line.

0246810020406080100distance (km)time (minutes)+45 min at 0 km

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.

The slope sets how steep the line is. The intercept sets where it meets the y-axis. The small triangle shows what one more km adds.

On a straight line, the slope is the same everywhere. Going from 1 km to 2 km adds ww 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 wx+bwx + b affine and save "linear" for lines through the origin (b=0b = 0). ML usually calls wx+bwx + b 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 g(x)=5+4x+0.5x2g(x) = 5 + 4x + 0.5x^2. The 0.5x20.5x^2 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?

0246810020406080100distance (km)time (minutes)lightheavy

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².

On the line, the next km always adds 4 minutes. On the curve, the next km adds more the further out you go.

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.

024681002004006008001000dayorders1

Day 0: 1 = 1 orders

Doublings counted: 0, so log₂ 1 = 0

Day 0 of 10
Plotted as orders, the bars stay tiny for days, then shoot up. Plotted as doublings (log₂ of the orders), they climb one even step per day.

After xx days there are 2x2^x orders. In 2x2^x, 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. 20.52^{0.5} is the number that gives 2 when you multiply it by itself, 2≈1.414\sqrt{2} \approx 1.414. So 2x2^x makes sense for any xx, 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: 1→2→4→81 \to 2 \to 4 \to 8. That count is a log.

The log (logarithm) to base bb undoes bxb^x. log⁡by\log_b y is the power pp that makes bp=yb^p = y:

log⁡28=3because23=8\log_2 8 = 3 \quad \text{because} \quad 2^3 = 8

The doublings tab in the figure above used base 2. Each bar's height was log⁡2\log_2 of the orders, so it climbed by 1 per day.

Predict first

Roughly what is log₂ 1000?

A few facts follow from "counting doublings":

  • log⁡1=0\log 1 = 0: you get 1 with no doublings.
  • Between 0 and 1 the log is negative. Halving is a doubling backwards, so log⁡20.5=−1\log_2 0.5 = -1.
  • log⁡0\log 0 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:

8×4=(2×2×2)×(2×2)=25=328 \times 4 = (2 \times 2 \times 2) \times (2 \times 2) = 2^5 = 32

So log⁡232=3+2\log_2 32 = 3 + 2. Set any two numbers below and watch their doublings join end to end.

8 × 4 = 32

log₂ of each: 3 + 2 = 5 = log₂ 32

Multiplying two numbers puts their doublings end to end. So the log of the product is the sum of the logs.

In general, for any base:

log⁡(a×b)=log⁡a+log⁡b\log(a \times b) = \log a + \log b

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 log⁡2(3×5)\log_2 (3 \times 5)? Every positive number is 2 to some power, even if the power isn't whole. To three decimals, 3=21.5853 = 2^{1.585} and 5=22.3225 = 2^{2.322}.

Powers add when you multiply: 2p×2q=2p+q2^p \times 2^q = 2^{p + q}, for any pp and qq. So:

3×5=21.585×22.322=23.9073 \times 5 = 2^{1.585} \times 2^{2.322} = 2^{3.907}

Check: 23.907≈15.02^{3.907} \approx 15.0. So log⁡215=log⁡23+log⁡25\log_2 15 = \log_2 3 + \log_2 5.

Changing the base only scales every log by the same number: log⁡2x=ln⁡x/ln⁡2\log_2 x = \ln x / \ln 2, where ln⁡2≈0.693\ln 2 \approx 0.693. So the rule holds in every base.

The number e

One base turns up again and again: the number e≈2.718e \approx 2.718. The exponential function is exp⁡(x)=ex\exp(x) = e^x:

  • adding 1 to xx multiplies the output by ee
  • e0=1e^0 = 1, e1≈2.718e^1 \approx 2.718 and e2≈7.389e^2 \approx 7.389
  • the output is always positive, even for negative xx: e−1≈0.368e^{-1} \approx 0.368

The log to base ee is the natural log, written ln⁡\ln. In ML papers and in ML code, a plain log⁡\log means ln⁡\ln. For example, ln⁡e2=2\ln e^2 = 2.

Why ee? Look at the slope of bxb^x. For every base, the slope at a point is the height times a fixed number, ln⁡b\ln b. For base 2 that number is ln⁡2≈0.693\ln 2 \approx 0.693. For base ee it's exactly 1, so the slope of exe^x 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.

−3−2−1012345−3−2−1012345y = xy = 2ˣy = log₂ x

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.

The exponential (blue) and the log (orange) are mirror images across the dashed line y = x. On the Base e tab, the tangent's slope always equals the height.

Later, softmax uses exe^x 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 1.5×1.5=2.251.5 \times 1.5 = 2.25.

With nn payments a year, each one multiplies your money by 1+1n1 + \frac{1}{n}:

Payments a year, nnMoney after a year, (1+1n)n(1 + \frac{1}{n})^n
12
22.25
12 (monthly)2.613
365 (daily)2.715
1,000,0002.718

The amount creeps up to e=2.71828…e = 2.71828\ldots and never passes it. So ee 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

"f(x)f(x) means ff times xx." It doesn't. f(x)f(x) is the output of ff for the input xx.

"Twice the slope means twice the output." Only if the intercept is 0.

"x2x^2 and 2x2^x are the same kind of thing." In x2x^2 the input is the base, so it squares. In 2x2^x the input is the power, so it doubles with every step. At x=10x = 10: 102=10010^2 = 100, but 210=10242^{10} = 1024.

"log means base 10." On many calculators it does. In ML, log⁡\log means base ee.

Recap

  • A function turns each input into exactly one output, f(x)f(x). Its graph is every point (x,f(x))(x, f(x)).
  • A linear function wx+bwx + b has a straight graph: slope ww, intercept bb. A nonlinear function's slope changes.
  • bxb^x multiplies by the base bb at every step. The log log⁡by\log_b y counts the steps, and it turns products into sums.
  • exe^x is the exponential whose slope equals its height. Its log is ln⁡\ln, the log that ML means by a plain log⁡\log.

Section 2 shows how to compute these in PyTorch.