Functions and graphs
Last updated
A model is a function: numbers go in, a number comes out. This stage teaches the maths the rest of the course is written in, and it starts here, with what a function is and the four you'll meet most: straight lines, the exponential, the logarithm and the sigmoid. Each one gets a graph you can change, then a sentence, then the symbols, then NumPy.
By the end of this lesson you can
- Read f(x) notation and work out a function's value by hand and in NumPy
- Say what the slope and intercept of a line do to its graph
- Describe the shapes of eˣ, ln x and the sigmoid, and where each is used
- Explain why adding logs is safer than multiplying many small probabilities
What a function is
A function takes an input and gives back one output, by a fixed rule. In maths it's written , said "f of x": is the rule's name and is the input. For example:
In words: double the input, then add 1. So . In Python it's a def, and with NumPy the same function works on a whole array of inputs at once (lesson 0.4):
Every model in this course is a function like this, just with more inputs and a more complicated rule. A penguin's measurements go in, a probability of "Gentoo" comes out. Training is choosing the numbers inside the rule (here, the 2 and the 1) so the outputs come out right.
Lines: y = mx + b
The simplest useful function is a straight line:
In words: multiply the input by , then add . is the slope: how much goes up for each step of 1 in . is the intercept: the value of where the line crosses the y-axis, at . In NumPy: y = m * x + b.
Change them and watch:
At x = 1, the line is 1. Each step of 1 to the right changes y by the slope, 1.
A negative slope makes the line fall; a slope of 0 makes it flat. Changing slides the line up and down without turning it.
Stage 3's first model is exactly this line, fitted through data: and are found by training. With many inputs it becomes , the weighted sum from lesson 0.6.
Powers, eˣ and ln x, and the sigmoid
Three more functions turn up all through the course. Use the buttons on the plotter above to see each, then run these:
- The exponential, , is the number raised to the power . It's always above 0, it's 1 at , and it grows faster and faster. Softmax (Stage 3), which turns scores into probabilities, is built from it.
- The natural logarithm, (NumPy's
np.log), undoes the exponential: is the power you'd raise to, to get . So , , and because . It only works for . Every loss for classifiers in this course is built from it. - The sigmoid, written (the Greek letter sigma), squeezes any number into the range 0 to 1:
In words: take to the power minus , add 1, and divide 1 by that. In NumPy: 1 / (1 + np.exp(-z)). A big positive gives nearly 1, a big negative one nearly 0, and gives exactly 0.5. That's why it turns a score into a probability: logistic regression (Stage 3) and the first neural networks use it.
Here are all three drawn with matplotlib:
Why ln turns products into sums
The log has one property the course depends on: the log of a product is the sum of the logs.
In words: multiplying two numbers and then taking the log gives the same answer as taking each log and adding. In NumPy: np.log(a * b) equals np.log(a) + np.log(b). Run it:
Why it matters: training a model often means multiplying many probabilities together, one for each example (lesson 1.10). Each is below 1, so the product shrinks fast.
- 1,000 probabilities of 0.5 multiply to about (the
e-302means "times 10 to the −302"). - 2,000 of them give exactly
0.0. The true answer isn't 0, but it's smaller than the smallest number afloat64can hold, so it underflows to 0. Every model would look equally bad. - Adding the logs instead gives −1386.29, an ordinary number with no trouble at all.
So the course almost never multiplies probabilities. It adds their logs. You'll see "log-likelihood" and "log-probability" everywhere from Stage 3 on, and this is the reason.
Wrong way: the log of 0
has no value at : no power of gives 0. Here's what happens if you ask anyway:
- NumPy's
np.log(0.0)gives-inf, minus infinity, and carries on. On your own computer it also prints aRuntimeWarning: divide by zero encountered in log, which is easy to miss in a long notebook. - Python's own
math.log(0)stops with aValueError.
NumPy's quiet -inf is the dangerous one. It spreads: -inf plus any number is -inf, and 0 * -inf is nan (not a number). One probability of exactly 0 in a model's output can turn the whole loss into nan, and training stops making sense. Lesson 1.11 shows the standard fix.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 3: Probability and Information Theory: the logistic sigmoid and its shape (section 3.10)
- Goodfellow, Bengio and Courville, Deep Learning, chapter 4: Numerical Computation: underflow, and why computations are done with logs
- NumPy: numpy.exp: the exponential, element by element
- NumPy: numpy.log: the natural log, and log(0) = -inf
- The Python Standard Library: math: math.log raises ValueError for 0