Math Toolkit 1: Functions, Exponents & Logs
After this lesson, you will be able to:
- Read f(x) notation, draw a function as a set of (x, f(x)) pairs, and evaluate a composition f(g(x)) from the inside out
- Evaluate powers with positive, negative and fractional exponents, and say what the number e is and why growth and decay use it
- Use a logarithm as the undo button for an exponent, and apply the three log rules: product to sum, power to multiplier, change of base
- Explain why ML libraries work with log-probabilities instead of raw products, and show it with a number you compute yourself
- Check your own algebra with six quick questions, so you know whether you are ready for the rest
#Why a Maths Toolkit First
Open any ML paper or tutorial and you will see things like exp, log, a tall sigma, a product symbol, and little letters attached above and below. Most courses use these symbols on page one and never explain them.
These two toolkit lessons explain them on purpose. They are not about getting good at algebra. They are about being able to look at a formula and know what to do with each piece.
Before the main ideas there is one short warm-up: a six-question algebra self-check, so you can find out whether you are ready.
The toolkit is split in two. This first part has four small ideas, in an order where each one uses the one before:
- Functions, the machines that turn inputs into outputs.
- Exponents and the special number e.
- Logarithms, the undo button for exponents.
- Why ML lives on logs.
Part 2, Math Toolkit 2: Sums, Symbols & Reading Formulas, picks up set symbols, sigma and pi, reading whole formulas word by word, sigmoid and softmax, and a first look at slopes.
Every Python cell on this page runs in your browser. Run them. Reading about a number and seeing your own machine print it are different experiences.
#Algebra Self-Check
Try these six questions on paper before you read the answers. They use only school arithmetic, and they are the skills the rest of this lesson leans on.
- Solve 4x = 20. What is x?
- What is 1/2 + 1/3?
- What are 3 squared and 10 cubed?
- What are -5 + 8 and (-3) x (-2)?
- What is 2 + 3 x 4? What is (2 + 3) x 4?
- Solve 3x - 4 = 11. What is x?
| Question | Answer | Working |
|---|---|---|
| 1 | x = 5 | Divide both sides by 4: 20 / 4 = 5. |
| 2 | 5/6 | Common bottom of 6: 3/6 + 2/6 = 5/6. |
| 3 | 9 and 1000 | 3 x 3 = 9, and 10 x 10 x 10 = 1000. |
| 4 | 3 and 6 | Start at -5 and move 8 steps up to land on 3. Two negatives multiplied give a positive: 3 x 2 = 6. |
| 5 | 14 and 20 | Multiplication goes before addition: 3 x 4 = 12, then 2 + 12 = 14. Brackets go first: 5 x 4 = 20. |
| 6 | x = 5 | Add 4 to both sides to get 3x = 15, then divide by 3. |
If you got at least five of the six, you are ready. If you missed several, do not stop. Read this lesson slowly, run every Python cell, and redo any question you missed when its idea appears below: powers return in the exponents section, and negatives, brackets and fractions return all through the lesson.
#Functions: Machines That Turn Inputs into Outputs
Take the rule "double it and add one". In symbols:
Feed in a few inputs and write down what comes out:
| Input x | Working | Output f(x) |
|---|---|---|
| -2 | 2 x (-2) + 1 | -3 |
| 0 | 2 x 0 + 1 | 1 |
| 3 | 2 x 3 + 1 | 7 |
| 10 | 2 x 10 + 1 | 21 |
#A graph is every (x, f(x)) pair
A neural network is also a function. It takes an input (the pixels of a photo) and returns an output (the probability of "cat"). The only difference is that its rule has millions of numbers inside it, and training is the process of choosing those numbers.
#Composition: one machine feeding another
Work it for x = 3, from the inside out:
- g(3) = 3 squared = 9.
- f(9) = 2 x 9 + 1 = 19.
So f(g(3)) = 19. Order matters. Reverse it: f(3) = 7, then g(7) = 49. So g(f(3)) = 49, a different answer.
A neural network is a long composition. Layer 1 feeds layer 2, which feeds layer 3, so the whole model is f3(f2(f1(x))). Later, when you learn how a small change in the input travels through a stack of layers, you will use exactly this idea of nesting. That rule is called the chain rule, and composition is its raw material.
#Exponents and the Number e
Doubling is the easiest way to feel how fast this grows. After 10 doublings you have 2 to the 10 = 1024. After 20 doublings you have 1,048,576. A power is shorthand for growth that compounds.
#Zero and negative exponents
Two extensions look odd at first, but each follows from one idea: an exponent counts how many times you multiply, and subtracting one from the exponent means dividing by the base once.
| Expression | Meaning | Value |
|---|---|---|
| 2 to the 3 | 2 x 2 x 2 | 8 |
| 2 to the 2 | 2 x 2 | 4 |
| 2 to the 1 | 2 | 2 |
| 2 to the 0 | divide 2 by 2 once more | 1 |
| 2 to the -1 | divide again, giving 1 over 2 | 0.5 |
| 2 to the -3 | 1 over (2 x 2 x 2) | 0.125 |
Read the pattern going down: every step down in the exponent halves the value. A negative exponent means "one over". Any number to the power 0 is 1.
A note on negatives, so that 2 to the -3 is not confusing. The minus sign in an exponent is not a negative number: 2 to the -3 is 0.125, which is positive. Separately, multiplying two negative numbers gives a positive one. You can see why from a pattern: 3 x (-3) = -9, 2 x (-3) = -6, 1 x (-3) = -3, 0 x (-3) = 0. Each step down in the first number adds 3, so the next row must be (-1) x (-3) = 3. That is why (-2) x (-3) = 6.
#Two rules for powers, and fractional exponents
Two handy rules follow from "an exponent counts copies". Multiplying powers of the same base adds the exponents. Count the copies: 2 to the 3 is three 2s, 2 to the 2 is two 2s, and together there are five 2s. In numbers, 8 x 4 = 32, which is 2 to the 5.
Raising a power to a power multiplies the exponents. Take (2 to the 3) to the 2. That means two copies of 2 to the 3 multiplied together, which is (2 x 2 x 2) x (2 x 2 x 2), six 2s in all. In numbers, 8 x 8 = 64, which is 2 to the 6, and 3 x 2 = 6.
Both rules can be derived by counting copies. Open them if you want every line.
| Expression | Meaning | Value |
|---|---|---|
| 9 to the 1/2 | the number that squares to 9 | 3 |
| 8 to the 1/3 | the number that cubes to 8 | 2 |
#The number e
Suppose a bank pays 100% interest per year on 1 unit of money. Paid once at year end, you hold 2. Paid twice, as 50% every half-year, you hold (1 + 1/2) squared = 2.25. Split into more and more slices, the total creeps up but never explodes:
| Slices n | (1 + 1/n) to the n |
|---|---|
| 1 | 2.0000 |
| 10 | 2.5937 |
| 100 | 2.7048 |
| 1,000 | 2.7169 |
| 1,000,000 | 2.7183 |
For exp, the steepness at any spot equals the height of the curve there. At x = 2 the height is 7.389 and the steepness is also about 7.39 (between x = 2 and x = 2.001 the rise over run is 7.3928). At x = 3 both are about 20.09. No other base does this, and that is why e is the natural base. The last section of this lesson takes slopes further, and the derivatives lesson explains why exp behaves this way.
Why ML cares: exp turns any real number, positive or negative, into a positive one. Probabilities must be positive, so exp is how a model turns raw scores into valid ones. You will see that in Math Toolkit 2, where softmax is built from exp.
#Logarithms: The Undo Button for Exponents
Here is the picture. Start at 1 and keep doubling. Each doubling is one step up a staircase, and the log base 2 of a number is which step you are standing on.
| Step | Value after that many doublings | log2 of the value |
|---|---|---|
| 0 | 1 | 0 |
| 1 | 2 | 1 |
| 2 | 4 | 2 |
| 3 | 8 | 3 |
| 4 | 16 | 4 |
| 5 | 32 | 5 |
Reading the table sideways: to get from the value 32 back to the step number, take log2, and you get 5. The exponent walks up the stairs and the log walks back down.
| Name | Written | Meaning | Example |
|---|---|---|---|
| Log base 2 | log2 | how many doublings | log2(8) = 3 |
| Common log | log10 | how many factors of 10, roughly "number of digits minus 1" | log10(1000) = 3 |
| Natural log | ln | log with base e | ln(e squared) = 2 |
log almost always means the natural log ln. np.log and math.log both do. When a paper says log without a base, assume ln unless told otherwise.Because a log is the undo of an exponent, they cancel in either order: ln(exp(x)) = x and exp(ln(x)) = x. Feed a number in and then undo it, and you are back where you started. Also note that logs only exist for positive numbers. There is no power of 2 that equals 0 or a negative number.
The log of a number below 1 is negative. Check it with base 2: log2(0.5) = -1, because 0.5 is 1 over 2, and "one over" is what a negative exponent means, so 2 to the -1 = 0.5. The natural log works the same way: ln(0.1) = -2.3026, because 0.1 is 1 over 10, so the power of e that gives 0.1 must be negative (e to the -2.3026 is 0.1). Remember this, because every probability sits between 0 and 1, so the log of a probability is always negative or zero.
#The three rules
These three rules carry almost everything. Each one is just an exponent rule turned inside out.
| Rule | Statement | Worked number |
|---|---|---|
| Product becomes a sum | log(a x b) = log(a) + log(b) | log2(2 x 8) = log2(16) = 4, and log2(2) + log2(8) = 1 + 3 = 4 |
| Power becomes a multiplier | log(a to the b) = b x log(a) | log2(8 squared) = log2(64) = 6, and 2 x log2(8) = 2 x 3 = 6 |
| Change of base | log_b(x) = ln(x) / ln(b) | log2(10) = ln(10) / ln(2) = 2.3026 / 0.6931 = 3.3219 |
The first rule is the one to remember. It comes straight from the exponent rule you already saw: multiplying powers adds their exponents. A log reads off exponents, so multiplying the numbers adds their logs.
The third rule has a reason too. Let x be log2(10), which means 2 to the x is 10. Take ln of both sides, move the exponent to the front with the power rule, and divide by ln(2): x = ln(10) / ln(2) = 2.3026 / 0.6931 = 3.3219. The line-by-line steps are in the third derivation below. As a check, 2 to the 3.3219 is 9.9998, which is 10 up to rounding.
The three rules each have a short derivation. They use only the definition of a logarithm (it is the exponent that gives the number) and the exponent rules from the section above.
What is log2(32)?
#Why Machine Learning Lives on Logs
Here is the practical reason logs matter so much, as a short story.
Three heads in a row: 0.5 x 0.5 x 0.5 = 0.125. Ten heads in a row: 0.5 multiplied ten times is 0.000977. A hundred heads in a row is 7.9e-31, where "e-31" means the decimal point moves 31 places to the left. Four hundred heads in a row is 3.9e-121. Every extra event multiplies in another number below 1, so the total keeps shrinking toward zero.
Make it concrete with a smaller probability. Say 400 independent events each have probability 0.1. The likelihood of all 400 is 0.1 multiplied by itself 400 times, which mathematically is 10 to the power -400. Computers write that as 1e-400, meaning 1 divided by a 1 followed by 400 zeros.
In Python, you start with total = 1.0 and multiply it by 0.1 four hundred times in a loop. What does the program print for total at the end?
A 64-bit float holds numbers down to about 1e-308 at full precision and, with reduced precision, down to about 5e-324. Below that it silently rounds to zero. 0.1 multiplied by itself 100 times is 1e-100, which still fits. At 300 copies you reach 1e-300, still fine. At 320 copies the value is about 1e-320 and has lost most of its precision. At 400 copies it is gone, and the program says 0.0.
Real models are far worse than this. A language model scoring a long sentence, or a classifier scoring thousands of training rows, multiplies far more than 400 factors.
Even with probability 0.5, which is far larger than 0.1, the plain product underflows to 0.0 after 1075 events. Python gives no warning at all.
Now use the product rule. The log of a product is the sum of the logs, and adding numbers does not shrink anything:
The answer, -921.03, loses nothing. You can still compare two models by it: the one with the larger (less negative) total log-likelihood explained the data better. Since log is increasing, "larger likelihood" and "larger log-likelihood" always pick the same winner.
Run it yourself. The cell prints the naive product, the sum of logs, and where each one breaks.
Next, try the explorer below. It draws exp and log as mirror images and shows the product-to-sum rule on numbers you choose. Its third panel repeats the underflow demo from the cell above as a second look, so spend your time on the first two.
Move the sliders and watch the exp and log curves reflect across the diagonal, then enter two numbers in the product panel and compare the log of their product with the sum of their logs.
#Cross-entropy is the same idea
A loss is a score for how badly the model did, where lower is better. Training means nudging the model's numbers until the average loss on the training examples falls. In plain words, the loss is how surprised the model was by the truth. Two examples where the model gave the right answer 0.7 and 0.5 cost 0.357 and 0.693, so the average loss is 0.525.
#Try It Yourself
Tests: Verify that log2(a x b) and log2(a) + log2(b) are both 8.0, that the power rule gives 9.0 on both sides, that change of base gives 2.0, that the naive product of 1100 factors of 0.5 is exactly 0.0, that the sum of their logs is about -762.46, and that the product first becomes 0.0 at the 1075th factor.
When you run the solution you should see 8.0 for both the log of the product and the sum of the logs, 9.0 for both sides of the power rule, and 2.0 for the change of base. The naive product of 1100 factors of 0.5 prints 0.0, the sum of their logs is -762.46, and the product first hits exactly 0.0 at the 1075th factor. Even a probability as large as 0.5 collapses a 64-bit float.
#Key Takeaways
- A function is a rule with one output per input, and f(g(x)) means do g first, then f. A neural network is a long composition of simple functions.
- Exponents count repeated multiplication. A negative exponent means one over, a fractional exponent means a root, and e = 2.71828 is the base where exp(x) has a slope equal to its own value.
- A logarithm is the undo of an exponent. Its three rules are product to sum, power to multiplier, and change of base.
- Multiplying 400 probabilities of 0.1 gives exactly 0.0 in 64-bit floats, while the sum of their logs is a normal number, -921.03. This is why ML works with log-probabilities.
#Quick Check
With f(x) = x + 3 and g(x) = x squared, what is f(g(2))?