The Spell That Saved AI From Itself
The most important log identity for AI:
Read it again. Multiplication on the left, addition on the right. Logs let you trade hard operations (multiplying many tiny floats) for easy ones (adding their logs). This is not a notational convenience — it's an engineering miracle.
Numerical Underflow: The Bug This Identity Solves
Float32 represents numbers down to about . Multiply 100 probabilities of value 0.001 each:
The result is mathematically tiny but not zero. Float32 rounds it to zero anyway. Now your loss is and your training crashes.
Take logs first: . A perfectly normal float. Sum the log-probabilities instead of multiplying probabilities. Done.
Log-Likelihood Everywhere
This is why almost every probabilistic ML loss involves logs:
- Cross-entropy loss: — sums log-probabilities of true classes.
- Maximum likelihood estimation: maximize , not .
- Language model training: minimize negative log-likelihood of the next token, summed over millions of tokens.
import numpy as np
방법 1
x = np.prod(np.full(1000, 0.5)) print(x)
방법 2
y = np.exp(1000 * np.log(0.5)) print(y)
비교
print(x == y)
첫번째 방법에서는 확률을 계속 곱하기 때문에 언더플로우가 나버려서 0이되어버림