Exponentials and logarithms
Use $e^x$ and $\log x$, rules like log(ab) = log a + log b, and why logs turn tiny products into sums.
What the animation says
Underflow: the silent zero
Score a thousand-word sentence by multiplying a thousand small probabilities, each about one in a hundred. Each factor moves the decimal point two places left. After a thousand of them, you have ten to the minus two thousand. But an ordinary double can't go below about ten to the minus three hundred eight. So the product silently becomes exactly zero. No error, no warning. This is called underflow. Here's a second sentence whose factors are about one in fifty. Which sentence scores higher?
Logs count exponents
Both tie at zero. The fix: store the exponent instead of the number. A logarithm is that exponent, the power you raise the base to. Log base ten of a thousand is three. One hundredth is ten to the minus two, so its log is minus two. And the log of one is zero. Multiplying powers adds exponents. Ten squared times ten cubed is ten to the fifth. So ten to the minus two thousand is stored as plain minus two thousand. What is log base ten of one thousandth?
e and the natural log
One thousandth is ten to the minus three, so its log is minus three. The standard base is e, about two point seven one eight. Plain log means the power you raise e to. e to any power is positive, so log only accepts positive numbers. Log and e to the x undo each other. The key rule: the log of a product is the sum of the logs. Dividing subtracts, and a power multiplies. Other bases just rescale. Now tap the expression that has no log.
Multiply by adding
Let's multiply by adding. A half, times a quarter, times a tenth, is zero point zero one two five. Take the natural log of each factor. All three are negative, because each factor is below one. Add them and you get about minus four point three eight two. e to that power gives back zero point zero one two five. Now the thousand factors of one hundredth. Each log is about minus four point six, so the sum is about minus four thousand six hundred five. No underflow. One sentence has a log of minus four thousand six hundred five, another minus five thousand. Which product is bigger?
Remember this
Minus four thousand six hundred five is bigger, so that product wins. Log keeps the order. The log of x is the power you raise e to, to get x. And e to the x undoes it. Because the log of a product is the sum of the logs, many tiny factors become a sum of modest negative numbers. That sum is safe to store, add up and compare. That's why language models work in log probabilities.
Logs count exponents
$e^{x}$ and log x, precisely
Multiply by adding
Remember this
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses