Summary and Revision Notes
Key Ideas
| Idea | Meaning |
|---|---|
| floating point | finite approximation to real numbers |
| rounding error | difference between exact and stored result |
| overflow | value too large to represent |
| underflow | tiny value lost near zero |
| conditioning | sensitivity of output to input changes |
| stable softmax | softmax computed after subtracting max logit |
| log-sum-exp | stable form of log(sum(exp(x))) |
| epsilon | small constant used as a numerical guardrail |
| exploding quantity | repeated products push a value to large scale |
| vanishing quantity | repeated products push a value close to zero |
| gradient check | comparison between backprop and finite difference |
Formulas to Remember
Stable softmax:
Log-sum-exp:
Centered finite difference:
How To Read Stable Code
When implementation code differs from the formula in a textbook, ask what intermediate value became dangerous.
exp(large number)suggests overflow.- products of many tiny probabilities suggest underflow.
- subtracting nearly equal large values suggests cancellation.
- division by zero or
log(0)suggests an epsilon guardrail. - suspicious gradients suggest a finite-difference check on a small example.
Stable rewrites usually preserve the mathematical quantity while changing the route taken through machine arithmetic.
Common ML Patterns
| Pattern | Numerical reason |
|---|---|
| subtract max before softmax | keep largest shifted exponential at e^0 = 1 |
| use log-sum-exp | avoid direct exponentials at unsafe scale |
| add epsilon in denominators | avoid division by zero or square root of zero |
| avoid long probability products | prevent underflow toward zero |
| clip gradients | limit extreme gradients after they appear |
| run gradient checks | debug a gradient implementation on a small case |
Common Traps
- Assuming computer arithmetic is exact real arithmetic.
- Computing softmax by exponentiating large logits directly.
- Ignoring underflow in long products of probabilities.
- Adding epsilon without understanding what it changes.
- Treating ill-conditioning as always being a code bug.
- Forgetting to add the max back in log-sum-exp.
- Assuming stable softmax changes the model's probabilities.
- Trusting gradients without testing a small case.
- Comparing floating-point results with exact equality when a tolerance is more appropriate.
- Adding epsilon by habit without identifying the numerical danger.
- Treating gradient clipping as a full fix rather than a guardrail.
Mental Model
Stable numerical code preserves the mathematical idea while avoiding avoidable machine failures.