When the letter you want is a power, as in money growing at 5% a year for n years, no root can reach it. Logarithms can: a log turns a power into a multiplier, and transposing with the four moves takes over. And when the letter is inside a log, raising to the matching power frees it. This pair of opposites runs through growth and decay, half-lives, learning-rate schedules, and the logit that undoes the sigmoid at the end of every classifier.
What you'll be able to do
Free a letter that is a power by taking logarithms of both sides, so the power comes down as a multiplier
Free a letter inside a logarithm by raising the log's base to both sides
Tell the two cases apart in A = P(1 + r)ⁿ: the letter in the power needs logs, the letter in the base needs a root
State the conditions: logs only of numbers above 0, and a base above 0 and not 1
Turn growth, decay, learning-rate schedules, the sigmoid and perplexity around for the letter you need
Quick check
Have a guess before you start. $100 doubles every year. After how many years is it $1,600?
4
1,500
8
16
Show the answer
4
100 × 2ⁿ = 1600, so 2ⁿ = 16, and 2 × 2 × 2 × 2 = 16: n = 4. Asking "to what power?" is exactly what a logarithm does: log₂ 16 = 4.
The math: logs bring a power down
Purpose. Free a letter that sits in a power, or inside a log.
Done to the letter
Undone by
10 to the power x
log₁₀
e to the power x
ln (the log with base e)
b to the power x
log with base b, or any log, divided by log b
log or ln of x
raise the base to that power: 10ʸ or eʸ
The key law is log(aⁿ) = n·log a: a log brings a power down to an ordinary multiplier. So in y = a·bˣ, divide by a, giving bˣ = y/a, then take logs of both sides: x·log b = log(y/a). Divide: x = log(y/a)/log b. Any base of log works, as long as you use the same one on top and bottom.
Conditions. Logs exist only for numbers above 0, so y/a must be positive. The base b must be above 0 and not 1, since a base of 1 stays 1 whatever the power.
Quick check
From an earlier lesson: what is log₂ 32?
2.5
64
16
5
Show the answer
5
log₂ 32 asks "2 to what power is 32?" 2⁵ = 32, so the answer is 5.
Watch a population that grows as N = 200e^(0.05t), built from t = 10 and then peeled back. "e to the power" is undone by ln.
Try it now
Change t and press Use these numbers: ln always undoes the e to the power step.
Symbols and formula. Compound growth has two different letters to free, and they need different tools:
A=P(1+r)n⇒n=ln(1+r)ln(A/P),r=(PA)1/n−1
In plain words: if the letter is the power, take logs. If the letter is in the base being raised to a known power, take the matching root (the move from the powers and roots lesson). If the letter is inside a log, raise the base to both sides.
A letter inside a log. Loudness in decibels is L = 10·log₁₀(I/I₀). Divide by 10: L/10 = log₁₀(I/I₀). Raise 10 to both sides: 10^(L/10) = I/I₀. So I = I₀·10^(L/10): every 10 decibels multiplies the intensity by 10.
Named mistakes. log(a + b) is not log a + log b: the log of a sum doesn't split. (It's the product that splits: log(ab) = log a + log b.) And ln(y/a) is not ln(y)/a.
Quick check
A = P(1 + r)ⁿ. Make n the subject.
n = ln A/ln(P(1 + r))
n = ln(A − P)/ln(1 + r)
n = (A/P)^(1/(1 + r))
n = ln(A/P)/ln(1 + r)
Show the answer
n = ln(A/P)/ln(1 + r)
Divide by P: (1 + r)ⁿ = A/P. Take logs: n·ln(1 + r) = ln(A/P). Divide: n = ln(A/P)/ln(1 + r).
In your own words
In A = P(1 + r)ⁿ, why does finding n need logs but finding r needs a root?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: logs, exps, and rounding up
Purpose. Python's math module has every log and its opposite. Planning questions usually want a whole number of steps, so they round up.
It runs right here in your browser, with nothing to install.
math.log(x) is ln, math.log10 is log₁₀, and math.log(x, base) takes any base.
math.exp undoes math.log: e to the power ln 7 gives back 7.
math.ceil rounds up. After 8.31 years the money hasn't arrived yet, so the answer is 9, and the check on the first line shows 9 years really does pass $1,500.
Worked example
Worked example: how long until a sample decays?
A sample halves every h years: N = N₀(1/2)^(t/h). Rearranged, t = h × log₂(N₀/N). The condition: both amounts above 0.
import math
deftime_to_fall(n0, n, half_life):
if n <= 0or n0 <= 0:
returnNonereturn half_life * math.log(n0 / n, 2)
print(time_to_fall(800, 100, 5), time_to_fall(800, 800, 5), time_to_fall(800, 0, 5))
In the lesson you can change this and run it.
800 grams falling to 100 is three halvings (800 → 400 → 200 → 100), so 3 × 5 = 15.0 years. Falling to 800, where it already is, takes 0.0 years. Falling to 0 never happens: each halving leaves half, however long you wait, and log of 800/0 doesn't exist. The function says so with None.
ln(10) ≈ 2.3026. ln 2 + ln 8 = ln 16 ≈ 2.7726, the same as ln(2 × 8). The log of a product splits into a sum; the log of a sum doesn't split.
Quick check
Which call stops the program with an error?
math.log(1)
math.log(100)
math.log(0)
math.log(0.5)
Show the answer
math.log(0)
No power of e is 0, so ln 0 doesn't exist and math.log raises a ValueError. ln 1 = 0, and ln 0.5 is negative, which is fine.
Which log to use? To free a letter in a power, any base works, as long as the top and the bottom use the same one: ln 16 ÷ ln 2 and log₁₀ 16 ÷ log₁₀ 2 both give 4. When a formula names its base, like decibels (base 10) or bits (base 2), use math.log10 or math.log2 directly. math.log(1000, 10) divides two natural logs and comes out as 2.9999999999999996, while math.log10(1000) gives exactly 3.0.
Drill
Fix the growth questions
years_to_reach takes the log of a difference, and rate_needed takes a log where it needs a root. Fix both.
In the lesson, your code is checked as you write it.
In your own words
Why does a planning answer like 8.31 years usually get rounded up, not to the nearest whole number?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: schedules, the logit and perplexity
Purpose. Training and evaluation are full of exponentials, and each one turned around answers a real question.
A decaying learning rate. Many runs shrink the learning rate by a factor γ each step: lr = lr₀·γᵗ. How many steps until it falls to a target? Take logs: t = ln(lr/lr₀)/ln γ. Both logs are negative, since both ratios are below 1, so t comes out positive.
The logit undoes the sigmoid. A classifier turns a score z into a probability with the sigmoid, p = 1/(1 + e^(−z)). That's four layers on z: times −1, e to the power, add 1, then 1 divided by it. Peel them in reverse: 1/p, take 1 away, ln, times −1. The result is the logit, z = ln(p/(1 − p)): the score that gives probability p.
Perplexity and loss. A language model's cross-entropy loss on a word is L = −ln p, where p is the probability it gave the right word. Turned around, p = e^(−L): a loss of 2.3 means it gave the right word about a 10% chance. Perplexity is e^H for an average loss H, and H = ln(perplexity) undoes it.
0.75/0.25 = 3 and ln 3 ≈ 1.0986. For 0.25 the ratio is 1/3, and ln(1/3) = −ln 3: probabilities the same distance from 0.5 give scores of opposite sign.
Drill
Undo the sigmoid and the schedule
Write logit(p) (returning None unless p is strictly between 0 and 1) and steps_until(target, lr0, gamma), the first whole step at which lr₀·γᵗ has fallen to the target.
31Formulas with fractions: clear them firstFreeClearing denominators, cross-multiplying, a letter in the bottom, and reciprocal formulas like 1/R = 1/R₁ + 1/R₂.35 min
37Inverse functions, lines and scalingFreeUndoing a function by making x the subject, domains that allow it, line forms, and unscaling predictions.40 min
38Sequences and sums: find n, a, d and rFreeEvery letter of the arithmetic and geometric formulas as the subject, and an average's window from its decay.40 min
40Review: rearrange anything (algebra)FreeBroken rearrangements to fix, mixed practice from the whole algebra block, a strategy chart, and its formula sheet.35 min
41Coordinates and distanceFreePoints as (x, y), Pythagoras, distance in any number of coordinates, and nearest neighbors.42 min
42Angles, degrees and radiansFreeMeasuring a turn, converting units, arcs, and the angle between two arrows.36 min
43Sine and cosineFreeAcross and up: right triangles, the unit circle, and directions and rotations.42 min
44Waves: height, speed and shiftFreeAmplitude, period and phase, sampling a wave, and position codes in transformers.37 min
68Vector equations: rearranging with arrowsFreeSolving for a vector or a scale, recovering a gradient from two weight snapshots, and an angle from a dot product.40 min
87Counting: choices, orders and groupsFreeThe counting principle, factorials, ordered picks and groups, and why attention's cost grows with the square of the length.43 min
88Probability: from counts to chancesFreeEqually likely outcomes, the complement, distributions, seeded simulation, and a next-word model.43 min
89Combining events: and, or, independentFreeThe or rule, independence, probability trees, drawing with and without replacement, and chains of steps.42 min
100Maximum likelihoodFreeLikelihoods as products, log-likelihoods as sums, grid search, underflow, and the negative log-likelihood loss.45 min
101Linear regression: the best lineFreeResiduals, least squares by formula, three roads to one line, the matrix form, and train versus test error.45 min
109One neuron, start to finishFreeWeighted sum, activation, loss, slopes and update, by hand and in code, and what one straight line can and can't split.45 min
111Training a small networkFreeThe backward pass in matrix form, gradient checks, a full training loop, and reading loss curves.45 min
112The same network in PyTorchFreenn.Sequential with the NumPy weights loaded, the same loss and slopes, then SGD and mini-batches.45 min
113Attention as matrix mathFreeScaled dot-product scores, the causal mask, a row-by-row softmax, and blending value vectors.45 min
114Residual connections and layer normFreeAdding the input back and why its slope keeps learning alive, layer norm by hand, and add and norm.40 min
115A transformer block, start to finishFreeA sentence through embeddings, attention, add and norm, a small network and next-word scores, every shape named.45 min
117Final test: transpose any formulaFreeSlips to fix from every topic, mixed practice of every kind of rearranging, the strategy chart and the full sheet.45 min
Learn the math behind AI from arithmetic up: numbers, algebra, vectors and matrices, calculus, probability and statistics, each shown as numbers moving and checked in Python, ending with a network you build and train from the formulas.
Learn to make any letter of any formula the subject: layers undone in reverse order, fractions, a letter that appears twice, powers, roots, quadratics, logs, inequalities, trigonometry, vectors, matrices, calculus, probability, statistics and the formulas inside AI models, each checked in Python.
See the course →
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write