Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsPre-calculus Refresher

Exponents and logarithms

P1.A235 min●○○ BeginnerReviewed Nov 30, 2026

Why this matters for machine learning

Logarithms turn the products and huge ranges of probabilities into sums and small numbers, and exponentials turn scores into probabilities: together they sit inside most ML losses.

  • The log-likelihood turns a product of many probabilities into a sum, which a computer can handle and which has the same best parameters. (P1.D10: Maximum Likelihood Estimation)
  • Cross-entropy loss is the negative log of the probability the model gave to the right answer. (P1.E3: Cross-entropy)
  • The sigmoid, which squashes any number into a probability between 0 and 1, is built from e to a negative power. (P1.A5: The sigmoid, tanh and other squashing functions)
  • Softmax regression uses exponentials to turn a model's raw scores into class probabilities. (P3.A8: Softmax regression, coming soon)
  • Log-sum-exp is the trick that keeps softmax and log-probabilities from overflowing or underflowing. (P1.G2: Numerical stability, coming soon)
  • Learning rates are tried on a log scale (1e-4, 1e-3, 1e-2), and loss curves and scaling laws are read on log axes. (P4.14: Learning-rate schedules, coming soon)

Before this: Functions and graphs

After this lesson you can

  • Use the exponent rules (product, quotient, power, zero, negative and fractional exponents) and check them with small numbers
  • Describe exponential growth and decay, and say what the number e is and why it is used
  • Explain a logarithm as "the power that gives this number", and give its domain, shape and the bases 2, 10 and e
  • Use the log rules and change of base, and avoid the mistake log(a + b) = log a + log b
  • Read a log-scale axis, and explain why machine learning uses logs to turn products into sums and to avoid underflow

Here is a puzzle. A model gives each of 1000 training examples a probability of 0.1. Multiply all 1000 probabilities together in Python and you get exactly 0.0. Python prints zero, although the true answer is a tiny positive number. Add the logarithms of the same 1000 probabilities and you get a perfectly fine number, about −2302.585-2302.585. Logarithms are the cure, and by the end of this lesson you will know why it works.

You met exponential functions in P1.A1 (2x2^x multiplies by 2 for every +1 in xx). Here we go deeper: the rules for exponents, the logarithm that undoes them, and why machine learning uses them everywhere.

Exponents: repeated multiplication

An exponent (or power) says how many times to multiply a number by itself. In bnb^n the number bb is the base and nn is the exponent:

23=2×2×2=8104=10×10×10×10=10000\begin{gathered} 2^3 = 2 \times 2 \times 2 = 8 \\ 10^4 = 10 \times 10 \times 10 \times 10 = 10000 \end{gathered}

Read 232^3 as “2 to the power 3”. Now we build the rules from small examples. Each rule is checked with numbers, so you never have to memorize it blindly.

Rule 1: multiplying powers of the same base adds the exponents

23×22=(2×2×2)×(2×2)=25check: 8×4=32=25\begin{gathered} 2^3 \times 2^2 = (2\times2\times2)\times(2\times2) = 2^5 \\ \text{check: } 8 \times 4 = 32 = 2^5 \end{gathered}

Five 2s multiplied together: three from the first power and two from the second. In general bm×bn=bm+nb^m \times b^n = b^{m+n}.

Rule 2: dividing powers of the same base subtracts the exponents

2522=2×2×2×2×22×2=23check: 32/4=8=23\begin{gathered} \frac{2^5}{2^2} = \frac{2\times2\times2\times2\times2}{2\times2} = 2^3 \\ \text{check: } 32 / 4 = 8 = 2^3 \end{gathered}

Two 2s cancel. In general bm/bn=bm−nb^m / b^n = b^{m-n}.

Rule 3: a power of a power multiplies the exponents

(23)2=23×23=26check: 82=64=26\begin{gathered} (2^3)^2 = 2^3 \times 2^3 = 2^6 \\ \text{check: } 8^2 = 64 = 2^6 \end{gathered}

In general (bm)n=bmn(b^m)^n = b^{mn}.

Rule 4: the exponent zero

Look at the pattern as the exponent goes down by one: 23=82^3 = 8, 22=42^2 = 4, 21=22^1 = 2. Each step divides by 2. So the next one has to be 20=2÷2=12^0 = 2 \div 2 = 1. This also follows from Rule 2: 23/23=202^3/2^3 = 2^0, and anything divided by itself is 1. So b0=1b^0 = 1 for every b≠0b \ne 0.

Rule 5: negative exponents mean “one over”

Keep going down the same pattern: 20=12^0 = 1, then 2−1=1÷2=122^{-1} = 1 \div 2 = \tfrac12, 2−2=142^{-2} = \tfrac14, 2−3=18=0.1252^{-3} = \tfrac18 = 0.125. So

b−n=1bnb^{-n} = \frac{1}{b^n}

A negative exponent does not make the number negative: 2−3=0.1252^{-3} = 0.125 is positive. It means “divide” instead of “multiply”.

Rule 6: fractional exponents are roots

What should 91/29^{1/2} be? By Rule 1, 91/2×91/2=91=99^{1/2} \times 9^{1/2} = 9^{1} = 9, so 91/29^{1/2} is a number that times itself gives 9: the (positive) square root, 33. In the same way

b1/2=bb1/3=b3bm/n=(bn)m\begin{gathered} b^{1/2} = \sqrt{b} \\ b^{1/3} = \sqrt[3]{b} \\ b^{m/n} = \bigl(\sqrt[n]{b}\bigr)^m \end{gathered}

Checks: 81/3=28^{1/3} = 2 (because 23=82^3 = 8), and 43/2=(4)3=23=84^{3/2} = (\sqrt{4})^3 = 2^3 = 8. For fractional exponents we use a positive base, so the answer is a real number.

Rule 7: powers of a product

(ab)n=anbn(ab)^n = a^n b^n. Check: (2×3)2=36=4×9(2 \times 3)^2 = 36 = 4 \times 9.

RuleFormulaSmall check
Productbmbn=bm+nb^m b^n = b^{m+n}23⋅22=32=252^3 \cdot 2^2 = 32 = 2^5
Quotientbm/bn=bm−nb^m / b^n = b^{m-n}25/22=8=232^5 / 2^2 = 8 = 2^3
Power of a power(bm)n=bmn(b^m)^n = b^{mn}(23)2=64=26(2^3)^2 = 64 = 2^6
Zerob0=1b^0 = 120=12^0 = 1
Negativeb−n=1/bnb^{-n} = 1/b^n2−3=0.1252^{-3} = 0.125
Fractionalb1/n=bnb^{1/n} = \sqrt[n]{b}81/3=28^{1/3} = 2
Product base(ab)n=anbn(ab)^n = a^n b^n(2⋅3)2=36=4⋅9(2 \cdot 3)^2 = 36 = 4 \cdot 9

Exponential growth and decay

In P1.A1 you saw that a⋅bxa \cdot b^x multiplies by bb for every +1 in xx. That one idea covers many real processes:

  • Doubling. 100 bacteria that double every hour: N(t)=100⋅2tN(t) = 100 \cdot 2^t. After 3 hours there are 800, after 10 hours there are 102,400.
  • Halving (decay). A quantity that halves each step: A(t)=A0⋅(1/2)tA(t) = A_0 \cdot (1/2)^t. After 10 steps only 0.510=1/10240.5^{10} = 1/1024 of it is left.
  • Percentage growth. Growing 5% a year multiplies by 1.051.05 each year: 1000⋅1.05t1000 \cdot 1.05^t. After 100 years that is about 131,501. (Money doubles in about ln⁡2/ln⁡1.05≈14.2\ln 2 / \ln 1.05 \approx 14.2 years at 5%; see the log rules below.)

A line adds the same amount each step; an exponential multiplies by the same factor each step. That is why the exponential eventually beats any line.

The number e

One base is special: the number

e≈2.71828e \approx 2.71828

It is an irrational number (its digits never end or repeat), like π\pi. Why do we love it? The function exe^x is the one exponential whose growth rate at every point equals its own value: when exe^x is 5, it is growing at 5 per unit of xx; when it is 20, it is growing at 20 per unit. (Slopes of curves are the subject of P1.C1; for now, remember that this makes exe^x the cleanest exponential to do calculus with.) No other base has this property exactly. A different base bb has growth rate ln⁡b\ln b times its value, and ln⁡e=1\ln e = 1.

You will see exe^x constantly in ML: the sigmoid 1/(1+e−x)1/(1 + e^{-x}) (P1.A5), softmax (P3.A8), and Gaussian bell curves e−x2/2e^{-x^2/2} (P1.D3). In NumPy it is np.exp(x). Some values: e0=1e^0 = 1, e1=2.71828e^1 = 2.71828, e−1≈0.368e^{-1} \approx 0.368, e10≈22026e^{10} \approx 22026.

Go deeper Where e comes from: compound interest

Put 1 unit of money in a bank paying 100% interest a year. If the interest is paid once at the end of the year, you have (1+1)1=2(1 + 1)^1 = 2. Paid twice (50% each half year): (1+1/2)2=2.25(1 + 1/2)^2 = 2.25. Paid monthly: (1+1/12)12≈2.613(1 + 1/12)^{12} \approx 2.613. Daily: (1+1/365)365≈2.7146(1 + 1/365)^{365} \approx 2.7146. A million times a year: about 2.718282.71828. As the payments become more and more frequent, the result settles toward a limit. That limit is ee:

e=lim⁡n→∞(1+1n)ne = \lim_{n \to \infty}\left(1 + \frac{1}{n}\right)^n

So ee is what you get from growth that compounds continuously. This is why continuous growth and decay, such as radioactive decay and the Gaussian, use exe^x.

Logarithms: the undo button for powers

Powers answer: “2 to the power 3 is what?” (8). A logarithm asks the question the other way round:

log⁡by\log_b y is the power you must raise bb to in order to get yy.

log⁡28=3  because  23=8log⁡101000=3  because  103=1000\begin{gathered} \log_2 8 = 3 \;\text{because}\; 2^3 = 8 \\ \log_{10} 1000 = 3 \;\text{because}\; 10^3 = 1000 \end{gathered}

The two statements be=yb^e = y and log⁡by=e\log_b y = e say the same fact in two ways. A logarithm is the inverse function of the exponential (the “undo” idea from P1.A1): the exponential turns an exponent into a number, the logarithm turns the number back into the exponent.

log⁡b(bx)=xblog⁡by=y(y>0)\begin{gathered} \log_b\bigl(b^x\bigr) = x \\ b^{\log_b y} = y \quad (y \gt 0) \end{gathered}

Log is the undo button for powers

23 = 8

log₂(8) = 3

Both lines say the same thing: the power of 2 that gives 8 is 3.

−22468−22468xy

2ˣ (power)   log₂(x) (its inverse)   dashed: the mirror line y = x. Swapping the two coordinates of a point on one curve gives a point on the other.

Pick a base and move the exponent slider. The two text lines say the same fact: b to the power e gives y, and log base b of y gives e. On the graph, the blue curve is the power and the pink curve is its inverse, the log. They are mirror images in the dashed line y = x, because swapping the two numbers of a point (x, y) on one curve gives a point on the other.

The three bases you will meet

  • log⁡10\log_{10} (common log): counts digits and powers of ten. log⁡101000=3\log_{10} 1000 = 3.
  • log⁡2\log_2 (binary log): counts doublings, and bits in information theory (P1.E1). log⁡21024=10\log_2 1024 = 10.
  • ln⁡\ln (natural log) =log⁡e= \log_e: the inverse of exe^x. ln⁡(e)=1\ln(e) = 1.

Watch out for the notation. Many ML books and all of NumPy write log for the natural log. In NumPy, np.log is ln⁡\ln; np.log10 and np.log2 are the others. In this course we write the base when it matters and write ln⁡\ln for the natural log. When a formula in ML has “log” with no base, the base only changes the answer by a constant factor (see change of base below), so the ideas do not depend on it.

Easy values to remember

yylog⁡10y\log_{10} ylog⁡2y\log_2 y
110000
220.3010.30111
880.9030.90333
1010113.323.32
10001000339.979.97
0.10.1−1-1−3.32-3.32
0.50.5−0.301-0.301−1-1

For any base bb: log⁡b1=0\log_b 1 = 0 (because b0=1b^0 = 1) and log⁡bb=1\log_b b = 1 (because b1=bb^1 = b).

Domain and shape

  • The input must be positive. A power bxb^x with b>0b \gt 0 is always positive, so there is no power that gives 0 or a negative number. So log⁡by\log_b y is defined only for y>0y \gt 0. In code, np.log(0) gives -inf and math.log(-1) raises an error.
  • The output can be any real number. Range: all real numbers.
  • For any base b>1b \gt 1, it is increasing, but slowly: log⁡10\log_{10} climbs only from 0 to 6 as yy goes from 1 to a million. Each time yy is multiplied by 10, log⁡10y\log_{10} y goes up by only 1. (The base must be positive and not 1; this course almost always uses bases 2, 10 and ee.)
  • Below 1 it is negative. log⁡by<0\log_b y \lt 0 when 0<y<10 \lt y \lt 1 (for b>1b \gt 1).
  • Near 0 it plunges. As yy gets closer to 0 from above (y→0+y \to 0^+), log⁡by→−∞\log_b y \to -\infty. For example log⁡100.001=−3\log_{10} 0.001 = -3, log⁡100.000001=−6\log_{10} 0.000001 = -6, and so on without end. It never reaches a value for y=0y = 0.

The log rules

Logarithms turn multiplication into addition. Why? Because exponents add when you multiply powers (Rule 1). Take log⁡2\log_2 of 32=4×832 = 4 \times 8: log⁡232=5\log_2 32 = 5 and log⁡24+log⁡28=2+3=5\log_2 4 + \log_2 8 = 2 + 3 = 5. The same thing, in general, for any base:

RuleFormulaSmall check
Product becomes sumlog⁡(xy)=log⁡x+log⁡y\log(xy) = \log x + \log ylog⁡106=0.778=0.301+0.477\log_{10} 6 = 0.778 = 0.301 + 0.477
Quotient becomes differencelog⁡(x/y)=log⁡x−log⁡y\log(x/y) = \log x - \log ylog⁡2(8/2)=2=3−1\log_2 (8/2) = 2 = 3 - 1
Reciprocallog⁡(1/x)=−log⁡x\log(1/x) = -\log xlog⁡2(1/8)=−3\log_2 (1/8) = -3
Power becomes multiplicationlog⁡(xk)=klog⁡x\log(x^k) = k \log xln⁡(37)=7.690=7ln⁡3\ln(3^7) = 7.690 = 7 \ln 3
Change of baselog⁡bx=ln⁡xln⁡b\log_b x = \dfrac{\ln x}{\ln b}log⁡210=ln⁡10ln⁡2=3.32\log_2 10 = \dfrac{\ln 10}{\ln 2} = 3.32

Why each rule is true. Write x=bmx = b^m and y=bny = b^n, so m=log⁡bxm = \log_b x and n=log⁡byn = \log_b y. Then xy=bmbn=bm+nxy = b^m b^n = b^{m+n} by Rule 1, so log⁡b(xy)=m+n\log_b(xy) = m + n. Rule 2 gives the quotient in the same way, and Rule 3 gives xk=(bm)k=bmkx^k = (b^m)^k = b^{mk}, so log⁡b(xk)=km\log_b(x^k) = km. The reciprocal rule is the quotient rule with x=1x = 1 (because log⁡1=0\log 1 = 0).

Change of base lets you compute any log with just ln⁡\ln: log⁡28=ln⁡8/ln⁡2=2.0794/0.6931=3\log_2 8 = \ln 8 / \ln 2 = 2.0794/0.6931 = 3. It also shows that logs of different bases differ only by a constant factor: log⁡2x=ln⁡x/ln⁡2≈1.4427ln⁡x\log_2 x = \ln x / \ln 2 \approx 1.4427 \ln x.

A worked use. How long does money take to double at 5% a year? We need 1.05t=21.05^t = 2. Take logs of both sides and use the power rule: tln⁡1.05=ln⁡2t \ln 1.05 = \ln 2, so t=ln⁡2/ln⁡1.05=0.6931/0.04879≈14.2t = \ln 2 / \ln 1.05 = 0.6931/0.04879 \approx 14.2 years. Taking the log brought the unknown tt down from the exponent.

Log scales: reading numbers that span many sizes

Some quantities range from tiny to huge: a training loss that falls from 5 to 0.001, a population that grows from 100 to 100 million, a learning rate between 10−510^{-5} and 11. On an ordinary linear axis the biggest values flatten everything else against the bottom.

A log axis fixes this by plotting log⁡10y\log_{10} y instead of yy. Equal steps up the axis now mean equal multiplications (here, ×10\times 10) instead of equal additions. The tick labels are written as powers of 10: 10210^2, 10310^3, 10410^4. (We plot log⁡10\log_{10} of each value and label the tick at height kk as 10k10^k.)

  • A quantity that grows or shrinks by the same factor each step (an exponential) becomes a straight line. The slope of that line is log⁡10\log_{10} of the factor: for doubling, log⁡102=0.301\log_{10} 2 = 0.301 per step.
  • Each order of magnitude (a factor of 10) gets the same height, so you can see tiny and huge values in one picture.

Linear axis or log axis?

05101520050 M100 Mhours

At hours 10: y = 102,400, and log₁₀ y = 5.01 (so y is about 10⁵ to the nearest power of 10).

Linear axis: each gridline adds the same amount. Small values are squashed against the bottom.

N = 100 · 2^t. Each hour multiplies the count by 2, so log₁₀ N rises by log₁₀ 2 ≈ 0.301 per hour: a straight line.

The same data on two y-axes. Pick a data set, then switch between Linear y-axis and Log y-axis. Use the slider to move the pink dot and read y and log10 y. On the log axis the doubling bacteria and the 5% money growth become straight lines, and the made-up loss curve shows all its detail from 5 down to 0.001; on the linear axis most of it is squashed along the bottom.

In ML you will meet log scales again and again:

  • Learning rates are searched on a log scale: try 10−5,10−4,10−3,10−210^{-5}, 10^{-4}, 10^{-3}, 10^{-2}, not 0.001,0.002,0.0030.001, 0.002, 0.003. A factor of 10 matters more than a difference of 0.001 (P4.14).
  • Loss curves that fall over several orders of magnitude are drawn with a log y-axis.
  • A power law y=axky = a x^k becomes a straight line when both axes are log, because log⁡y=log⁡a+klog⁡x\log y = \log a + k \log x. Many scaling-law plots are drawn this way (P10.B3).

Why machine learning loves logs

1. Products become sums

Probabilities of independent things multiply. If a model gives probability pip_i to each of NN independent training examples, the probability of the whole data set is the product p1p2⋯pNp_1 p_2 \cdots p_N (P1.D10 explains when and why). Taking the log:

ln⁡(p1p2⋯pN)=ln⁡p1+ln⁡p2+⋯+ln⁡pN\begin{gathered} \ln(p_1 p_2 \cdots p_N) \\ \quad= \ln p_1 + \ln p_2 + \cdots + \ln p_N \end{gathered}

A product of NN numbers has become a sum of NN numbers. Sums are easier for everything: they are numerically safe (next), their gradients are simple (a sum of small pieces, one per example), and they average nicely.

2. Underflow: why the product breaks

A computer stores a normal Python number as a 64-bit float. The smallest positive number a float can hold is about 5×10−3245 \times 10^{-324}, and anything much smaller is rounded to exactly 0.0. (Just above that, floats also lose precision, which is one more reason to avoid tiny numbers.) This is called underflow. Multiply 1000 probabilities of 0.1 and the true answer is 10−100010^{-1000}, far below that limit. The sum of logs, 1000×ln⁡0.1=−2302.5851000 \times \ln 0.1 = -2302.585, is an ordinary number.

Multiply probabilities, or add their logs

Multiply: p × p × … × p (100 times)

1.000 × 10⁻¹⁰⁰

A 64-bit float can still hold this number.

Add logs: ln p + … + ln p

−230.26

Always a comfortable size. It equals 100 × ln 0.10.

True value of the product: about 1.00 × 10⁻¹⁰⁰ (that is 100 × log₁₀ 0.10 = −100.0 as a power of 10). The smallest positive 64-bit float is about 5 × 10⁻³²⁴.

Choose a probability p and how many of them (n). The left card multiplies one at a time exactly as a Python float would; the right card adds their natural logs. Slide n up until the left card turns to 0.0. With p = 0.1 that happens at n = 324.

3. Log is increasing, so the best stays the best

The log function is increasing: a bigger input always gives a bigger output. So if one number is larger than another, its log is also larger. This means that the input that maximizes the likelihood also maximizes the log-likelihood, so we may optimize whichever is easier. Example: a coin shows heads, heads, tails. If the chance of heads is θ\theta, the likelihood of that data is θ2(1−θ)\theta^2(1 - \theta):

θ\theta0.50.52/32/30.80.8
likelihood θ2(1−θ)\theta^2(1-\theta)0.1250.1250.14810.14810.1280.128
log-likelihood−2.0794-2.0794−1.9095-1.9095−2.0557-2.0557

Both rows rank the three candidates the same way, and both pick θ=2/3\theta = 2/3 (the best of the three; in P1.D10 you will prove it is the best of all). Remember the effect on direction: a negative log makes a bigger probability a smaller loss, which is why we minimize the negative log-likelihood.

4. Log loss: surprise at the wrong answer

If a model gives probability pp to the correct answer, its log loss (the cross-entropy loss of P1.E3 for one example) is −ln⁡p-\ln p:

pp given to the right answer0.90.90.50.50.10.10.010.01→0\to 0
loss −ln⁡p-\ln p0.1050.1050.6930.6932.3032.3034.6054.605→∞\to \infty

A confident correct answer costs almost nothing; a confident wrong answer (pp near 0) costs a lot, without limit. That is exactly the shape of the log near 0 that you saw above. The base only sets the units: ln⁡\ln gives “nats”, log⁡2\log_2 gives “bits” (P1.E1).

5. Logs squeeze huge ranges

Raw numbers in ML vary enormously: word counts from 1 to millions, learning rates over four powers of ten, probabilities near zero. log⁡\log compresses a factor of a million into a step of 6 (base 10), making these quantities easier to plot, compare and feed to models.

Go deeper The mirror problem: overflow, and a preview of log-sum-exp

Underflow has a twin, overflow: numbers too big. A 64-bit float tops out near 1.8×103081.8 \times 10^{308}, and e1000e^{1000} is far above it (Python’s math.exp(1000) raises an error, and NumPy gives inf). Softmax divides ezie^{z_i} by a sum of such terms, so a raw implementation breaks for large scores ziz_i. The cure combines logs and a shift of the scores; it is called log-sum-exp and is the subject of P1.G2. The same two ideas, “work with logs” and “never let a number leave the safe range”, solve both problems.

Intuition

An exponent counts how many times you multiplied. A logarithm counts them back: “how many times did I multiply by 10 to get from 1 to this?” That is why log⁡10\log_{10} of a number is roughly “the number of digits minus one”, and why logs turn multiplication into addition: multiplying numbers just adds up the counts of multiplications. Where the picture breaks: this “counting” is whole numbers only for neat cases like 8 or 1000. For other numbers the log is a fraction (log⁡102=0.301\log_{10} 2 = 0.301), but the rules are exactly the same.

Math

For b>0b \gt 0, b≠1b \ne 1 and x,y>0x, y \gt 0:

log⁡b(bx)=x,blog⁡by=y,log⁡b(xy)=log⁡bx+log⁡by,log⁡bxy=log⁡bx−log⁡by,\begin{aligned} \log_b(b^x) &= x, \\ b^{\log_b y} &= y, \\ \log_b(xy) &= \log_b x + \log_b y, \\ \log_b\frac{x}{y} &= \log_b x - \log_b y, \end{aligned}log⁡b(xk)=klog⁡bx,log⁡bx=ln⁡xln⁡b,log⁡b1=0,log⁡bb=1.\begin{aligned} \log_b(x^k) &= k\log_b x, \\ \log_b x &= \frac{\ln x}{\ln b}, \\ \log_b 1 &= 0, \\ \log_b b &= 1. \end{aligned}

For b>1b \gt 1, log⁡b\log_b is increasing; for 0<b<10 \lt b \lt 1 it is decreasing, but ML uses b>1b \gt 1. Domain (0,∞)(0, \infty), range all real numbers. Exponent rules: bmbn=bm+nb^{m}b^{n} = b^{m+n}, (bm)n=bmn(b^m)^n = b^{mn}, b0=1b^0 = 1, b−n=1/bnb^{-n} = 1/b^n, b1/n=bnb^{1/n} = \sqrt[n]{b}.

Code

import numpy as np

np.log(np.e)        # 1.0   (natural log)
np.log10(1000)      # 3.0
np.log2(8)          # 3.0
np.exp(0)           # 1.0
np.log(20 * 3)      # = np.log(20) + np.log(3)
np.sum(np.log(np.full(1000, 0.1)))   # about -2302.585, fine
np.prod(np.full(1000, 0.1))          # 0.0, underflow

The cell below runs all of this and more.

Try it in Python

Run the cell, then change the numbers. Try different aa and bb, and see which of the “log rules” still hold when you break them.

Try it: exponents, logs and underflow

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Challenge: find how many 0.5 probabilities you can multiply before the product becomes 0.0 (use a loop). Then check that the sum of their logs is still fine. (It happens at 1075 of them.)

Putting it together

Exponents multiply; logarithms undo them and so turn multiplying into adding. Exponentials give growth, decay and the clean base ee. Logs give slow growth, a domain of positive numbers, and a way to read numbers of every size. In ML, that means log-likelihoods instead of products, log loss for classification, and log axes for learning rates and loss curves.

Check your understanding

  1. What is 2−32^{-3}?

  2. What is log⁡232\log_2 32?

  3. Which statement is correct for all positive aa and bb?

  4. A program multiplies 1000 probabilities that are each 0.1 and prints 0.0. What happened, and what is the standard fix?

  5. A plot has a log10 y-axis, and a curve rises as a straight line. What does that tell you about the data?

Exercises

  1. Compute 32⋅343^2 \cdot 3^4, 5654\dfrac{5^6}{5^4} and (22)3(2^2)^3 as single powers, and give their values.
    Show answer32⋅34=36=7293^2 \cdot 3^4 = 3^6 = 729; 56/54=52=255^6/5^4 = 5^2 = 25; (22)3=26=64(2^2)^3 = 2^6 = 64.
  2. Evaluate 10−210^{-2}, 161/216^{1/2}, 271/327^{1/3} and 909^0.
    Show answer10−2=1/100=0.0110^{-2} = 1/100 = 0.01; 161/2=416^{1/2} = 4; 271/3=327^{1/3} = 3 (because 33=273^3 = 27); 90=19^0 = 1.
  3. A population of 50 doubles every day. Write its size after tt days and compute it for t=6t = 6.
    Show answerN(t)=50⋅2tN(t) = 50 \cdot 2^t. At t=6t = 6: 50×64=320050 \times 64 = 3200.
  4. Find log⁡1010000\log_{10} 10000, log⁡214\log_2 \tfrac{1}{4} and log⁡51\log_5 1.
    Show answerlog⁡1010000=4\log_{10} 10000 = 4 (since 104=1000010^4 = 10000); log⁡214=−2\log_2 \tfrac14 = -2 (since 2−2=1/42^{-2} = 1/4); log⁡51=0\log_5 1 = 0 (since 50=15^0 = 1).
  5. Why are log⁡10(−5)\log_{10}(-5) and log⁡10(0)\log_{10}(0) undefined?
    Show answer10x10^x is positive for every xx, so no power of 10 gives −5-5 or 00. The log is defined only for positive inputs.
  6. Given log⁡102≈0.301\log_{10} 2 \approx 0.301 and log⁡103≈0.477\log_{10} 3 \approx 0.477, use the log rules to estimate log⁡106\log_{10} 6 and log⁡108\log_{10} 8 and log⁡100.5\log_{10} 0.5.
    Show answerlog⁡106=log⁡102+log⁡103≈0.778\log_{10} 6 = \log_{10} 2 + \log_{10} 3 \approx 0.778. log⁡108=3log⁡102≈0.903\log_{10} 8 = 3 \log_{10} 2 \approx 0.903. log⁡100.5=log⁡10(1/2)=−log⁡102≈−0.301\log_{10} 0.5 = \log_{10}(1/2) = -\log_{10} 2 \approx -0.301.
  7. Is ln⁡(4)+ln⁡(5)\ln(4) + \ln(5) equal to ln⁡(9)\ln(9)? Say what it equals instead.
    Show answerNo. ln⁡4+ln⁡5=ln⁡(4×5)=ln⁡20≈2.996\ln 4 + \ln 5 = \ln(4 \times 5) = \ln 20 \approx 2.996, while ln⁡9≈2.197\ln 9 \approx 2.197.
  8. Use change of base to compute log⁡210\log_2 10 from ln⁡\ln values.
    Show answerln⁡10/ln⁡2=2.3026/0.6931≈3.32\ln 10 / \ln 2 = 2.3026 / 0.6931 \approx 3.32.
  9. A model gives probability 0.25 to the correct class. What is its log loss −ln⁡p-\ln p? What would it be at p=0.5p = 0.5?
    Show answer−ln⁡0.25=ln⁡4≈1.386-\ln 0.25 = \ln 4 \approx 1.386. At p=0.5p = 0.5: −ln⁡0.5=ln⁡2≈0.693-\ln 0.5 = \ln 2 \approx 0.693. Making the model twice as confident in the right answer lowers the loss by ln⁡2\ln 2.
  10. You multiply 400 probabilities, each 0.1. Does a Python float hold the product? Use the widget or compute with logs.
    Show answerNo. 0.1400=10−4000.1^{400} = 10^{-400}, below the float limit of about 5×10−3245 \times 10^{-324}, so Python gives 0.0. The sum of logs, 400ln⁡0.1≈−921.03400 \ln 0.1 \approx -921.03, is fine.
  11. On a log10 y-axis, a curve is a straight line that rises by 1 gridline (one power of 10) every 5 steps. By what factor does the quantity multiply every step?
    Show answerOver 5 steps it multiplies by 10, so each step multiplies by 101/5=100.2≈1.58510^{1/5} = 10^{0.2} \approx 1.585.
  12. In the log-inverse widget, set the base to 2 and the exponent to 3. Read the two lines, then do it again with base 10 and exponent 0.9.
    Show answerBase 2: 23=82^3 = 8 and log⁡28=3\log_2 8 = 3. Base 10, exponent 0.9: 100.9≈7.9410^{0.9} \approx 7.94 (the widget shows 7.94; more precisely 7.943) and log⁡107.94≈0.9\log_{10} 7.94 \approx 0.9.

Key takeaways

  • bnb^n means multiply bb by itself nn times. Adding exponents multiplies powers (bmbn=bm+nb^m b^n = b^{m+n}), multiplying exponents raises a power to a power ((bm)n=bmn(b^m)^n = b^{mn}), b0=1b^0 = 1, b−n=1/bnb^{-n} = 1/b^n and b1/2b^{1/2} is the square root.
  • Exponential growth multiplies by the same factor every step (doubling, halving, +5% a year). The number e≈2.71828e \approx 2.71828 is the base whose growth rate at every point equals its own value, which makes it the natural base for calculus and for ML.
  • log⁡by\log_b y is the power you must raise bb to in order to get yy. It is the inverse of bxb^x, defined only for y>0y \gt 0, equal to 0 at y=1y = 1, negative for 0<y<10 \lt y \lt 1, and it grows slowly. ln⁡\ln is log⁡e\log_e, and NumPy's np.log is ln⁡\ln.
  • Log rules turn products into sums: log⁡(xy)=log⁡x+log⁡y\log(xy) = \log x + \log y, log⁡(x/y)=log⁡x−log⁡y\log(x/y) = \log x - \log y, log⁡(xk)=klog⁡x\log(x^k) = k\log x. But log⁡(x+y)\log(x + y) does not split.
  • ML uses logs because products of many small probabilities underflow to 0.0 while sums of logs are fine, because log is increasing so maximizing the log-likelihood finds the same answer as maximizing the likelihood, and because log scales show quantities that span many orders of magnitude.
Found a mistake? Tell us