Logarithms for Machine Learning
A logarithm asks which power turns the base into the number, compressing thousand-fold ranges into small readable steps. Entropy and log-loss both run on it.
Why Does This Exist?
Some quantities refuse to sit on a straight axis. A probability of and a learning rate of differ by seven orders of magnitude, and a linear plot puts one of them at zero. Logarithms compress multiplication into addition, so thousand-fold gaps become readable steps, loss curves straighten, and products of tiny probabilities become sums a float can actually hold.
That last one is load-bearing. Multiplying a thousand probabilities underflows to zero; adding their logs stays in range. Entropy is defined as expected log-surprise, and cross-entropy is the loss that results, so this page is the prerequisite both of them assume.
The one idea you need first. An exponent counts repeated multiplication: . The logarithm asks the question backwards: 10 is the power that turns 2 into 1024. If exponents and roots feel shaky, read that page first and this one becomes vocabulary.
Think of It Like This
Counting digits instead of the number
Nobody reads 1,000,000 by counting zeros one at a time; you notice it has 7 digits. The digit count is a base-10 logarithm in disguise: 10 has 2 digits, 100 has 3, each extra digit is one more multiplication by 10.
That is what a log axis does to a plot. The marks 1, 10, 100, 1000 sit evenly spaced because each step multiplies by 10, and a curve climbing them steadily is growing exponentially. You read the growth in the spacing instead of the raw heights.
Where it stops: digit counts are whole numbers, while logarithms are continuous, . The analogy shows the compression; the arithmetic below shows the smooth version ML actually uses.
How It Actually Works
The definition: exactly when , with and base , . In plain words: the log is the power the base needs. because . because . Three bases dominate: base 2 for bits, base 10 for human scales, and base for calculus, written . Change between them with .
Three rules do all the work, and each mirrors an exponent law. Products split into sums: . Quotients split into differences: . Powers drop their exponent in front: . That last rule is the underflow fix: a product of a thousand probabilities becomes a sum of a thousand logs.
Worked example: bits, digits and one natural log
Bits: , since . A fair coin flip carries bit of surprise, because .
Digits: , the three multiplications hiding in those zeros.
Natural: by the power rule, since . And a product check: , which matches . The rules agree with direct computation.
Code
import math
print(math.log2(1024)) # -> 10.0print(math.log10(1000)) # -> 3.0print((1e-300) ** 2) # -> 0.0print(round(math.log(1e-300) * 2, 2)) # -> -1381.55The third line is the catastrophe: squaring a tiny probability underflows to exactly zero, and every log after that is negative infinity. The fourth line does the same work in log space, where the answer fits comfortably. Multiply probabilities, add log-probabilities.
Watch Out For
Taking the log of zero or a negative
is not zero, it is negative infinity, and the log of a negative is undefined over the reals. A softmax output that underflows, or a count matrix with empty cells, hands exactly these inputs to the loss. NumPy returns -inf with a warning and keeps going, so the NaN surfaces ten steps later in the gradients.
Add a small epsilon inside the log: math.log(p + 1e-12). Clamp model outputs away from exact 0 and 1 before any log touches them.
Mixing bases without converting
while ; same input, different answer, and the ratio is always . An entropy reported in nats against a baseline in bits looks like a different model, and a hand-rolled learning-rate schedule in the wrong base decays 2.3 times too fast.
Name the base in code (math.log2, math.log10, math.log for natural) and in prose. When comparing two numbers, convert first with the change-of-base rule.
The Quick Version
- is the power the base needs: , defined only for positive .
- Products become sums, quotients become differences, powers drop their exponent in front.
- Base 2 measures bits, base 10 measures human scale, base is the calculus default.
- Add log-probabilities instead of multiplying probabilities; products of tiny values underflow to zero.
- Logs of zero or negatives are undefined: clamp with epsilon before the log, not after the NaN.