Foundations to Frontier
Tests
A. Mathematical Foundations › A1 Prerequisites and Mathematical Thinking

Exponentials, Logarithms and Log-Sum-Exp

Repeated doubling, the "how many doublings?" question that undoes it, and the numerically safe tricks every neural network relies on.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Doubling, again and again

One bacterium splits into 2. Each of those splits, making 4. Then 8, 16, 32… After just 20 splits there are more than a million. Growing by multiplying again and again is called exponential growth, and it gets huge very fast.

Fold a piece of paper in half: 2 layers. Fold again: 4 layers. Again: 8. Each fold doubles the layers.

Going backwards: “how many doublings?”

Now ask the opposite question. You see 32 bacteria. How many times did they double, starting from 1? Count: 1 → 2 → 4 → 8 → 16 → 32. That’s 5 doublings. Answering “how many times did I multiply?” is what a logarithm does. It’s like a counter of doublings.

Why AI cares

Logarithms turn huge numbers into small, friendly ones. The number 1,000,000 is just “6 zeros”, and that 6 is a logarithm. AI systems use logarithms all the time so their numbers don’t get too big or too small.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A plant doubles its leaves every week. It starts with 1 leaf. How many after 4 weeks?
  2. 2. How many times must you double 1 to reach 64?
  3. 3. Which grows faster in the long run: adding 100 each day, or doubling each day (starting at 1)?

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Powers and their rules

ana^n means aa multiplied by itself nn times. aa is the base, and nn is the exponent.

Rule Example
am⋅an=am+na^m \cdot a^n = a^{m+n} 23⋅22=25=322^3 \cdot 2^2 = 2^5 = 32
am/an=am−na^m / a^n = a^{m-n} 105/102=10310^5 / 10^2 = 10^3
(am)n=amn(a^m)^n = a^{mn} (32)3=36=729(3^2)^3 = 3^6 = 729
a0=1a^0 = 1 70=17^0 = 1
a−n=1/ana^{-n} = 1/a^n 2−3=1/82^{-3} = 1/8
a1/n=ana^{1/n} = \sqrt[n]{a} 91/2=39^{1/2} = 3

Logarithms undo powers

log⁡b(x)=y⟺by=x\log_b(x) = y \quad\Longleftrightarrow\quad b^y = x

log⁡232=5\log_2 32 = 5 because 25=322^5 = 32. log⁡101000=3\log_{10} 1000 = 3 because 103=100010^3 = 1000. Three bases matter most:

Base Written Used for
10 log⁡10\log_{10} Orders of magnitude, decibels, pH
2 log⁡2\log_2 Bits, binary search, information theory
e≈2.71828e \approx 2.71828 ln⁡\ln Calculus, probability, ML (the “natural” log)

Where does e come from?

Put ₹1 in a bank paying 100% interest a year. Paid once, you end with ₹2. Paid twice (50% each half-year), you get 1.52=2.251.5^2 = 2.25. Paid monthly, (1+1/12)12≈2.613(1 + 1/12)^{12} \approx 2.613. As the payments get more frequent, the total approaches a special number:

e=lim⁡n→∞(1+1n)n≈2.718281828e = \lim_{n\to\infty}\left(1 + \frac1n\right)^n \approx 2.718281828

The log rules

Rule Why
log⁡(xy)=log⁡x+log⁡y\log(xy) = \log x + \log y multiplying adds exponents
log⁡(x/y)=log⁡x−log⁡y\log(x/y) = \log x - \log y dividing subtracts exponents
log⁡(xk)=klog⁡x\log(x^k) = k\log x a power multiplies the exponent
log⁡bb=1, log⁡b1=0\log_b b = 1, \ \log_b 1 = 0 b1=b, b0=1b^1 = b, \ b^0 = 1

Drag the slider to change the base aa. The exponential and the log are mirror images across the line y=xy = x:

📈 aˣ and logₐ(x) are inverses
-4-3-2-10123456-4-3-2-10123456
y = aˣy = logₐ(x)y = x (mirror line)
Every exponential passes through (0, 1), and every log passes through (1, 0).
📝 Quick check (Level 1)
0/3 answered
  1. 1. What is log₂(64)?
  2. 2. Simplify log(5) + log(20) in base 10.
  3. 3. What is 2⁻²?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

The functions eˣ and ln x

exp⁡(x)=ex\exp(x) = e^x is defined for all real xx, is always positive, and is strictly increasing. Its inverse ln⁡x\ln x is defined only for x>0x > 0. Their special properties in calculus (A3) are:

ddxex=ex,ddxln⁡x=1x\frac{d}{dx} e^x = e^x, \qquad \frac{d}{dx}\ln x = \frac{1}{x}

Change of base. Any log can be computed with ln⁡\ln:

log⁡bx=ln⁡xln⁡b,ax=exln⁡a\log_b x = \frac{\ln x}{\ln b}, \qquad a^x = e^{x \ln a}

Solving for an exponent. 3⋅2x=963 \cdot 2^x = 96 gives 2x=322^x = 32, so x=log⁡232=5x = \log_2 32 = 5.

The sigmoid and softmax

The sigmoid squashes any real number into (0,1)(0,1). It turns a score into a probability for binary classification:

σ(z)=11+e−z,σ(−z)=1−σ(z)\sigma(z) = \frac{1}{1 + e^{-z}}, \qquad \sigma(-z) = 1 - \sigma(z)

Softmax does the same for many classes. It turns a vector of scores (logits) into probabilities that are positive and sum to 1:

softmax⁡(z)i=ezi∑j=1Kezj\operatorname{softmax}(\mathbf z)_i = \frac{e^{z_i}}{\sum_{j=1}^K e^{z_j}}
Softmax explorer: scores in, probabilities out
🐱 cat2.0
63.8%
🐶 dog1.0
23.5%
🐦 bird0.1
9.5%
🚗 car-1.0
3.2%
max(z/T) = 2.000 · log-sum-exp(z/T) = 2.449 · upper bound max + ln 4 = 3.386
Low T makes softmax pick the winner (close to argmax). High T spreads probability evenly. Log-sum-exp is always between the max and max + ln 4.

Log-sum-exp and numerical stability

The denominator’s log is the log-sum-exp function:

LSE⁡(z)=ln⁡∑jezj\operatorname{LSE}(\mathbf z) = \ln \sum_{j} e^{z_j}

A computer can’t evaluate e1000e^{1000}: float64 overflows above about e709.78e^{709.78}. The fix is to subtract the maximum m=max⁡jzjm = \max_j z_j first, which doesn’t change the result:

LSE⁡(z)=m+ln⁡∑jezj−m\operatorname{LSE}(\mathbf z) = m + \ln \sum_j e^{z_j - m}

Now every exponent is ≤0\le 0, so nothing overflows, and at least one term equals e0=1e^0 = 1, so the sum doesn’t underflow to 0. Softmax is shift-invariant for the same reason: softmax⁡(z−c)=softmax⁡(z)\operatorname{softmax}(\mathbf z - c) = \operatorname{softmax}(\mathbf z).

🐍 Exercise 1: stable softmax and log-sum-exp
The naive softmax crashes on large scores. Write logsumexp with the max trick, then softmax using it.

Logs everywhere in ML and data science

Use Formula
Cross-entropy loss −ln⁡pcorrect-\ln p_{\text{correct}}. Confident and wrong is punished heavily
Log-likelihood ∑iln⁡p(xi)\sum_i \ln p(x_i), a product turned into a sum (A1-3)
Log-transformed features ln⁡(1+x)\ln(1 + x) for skewed data such as income or population
Log scales on plots Exponential growth looks like a straight line
Learning rates Searched on a log grid, such as 10−4,10−3,10−210^{-4}, 10^{-3}, 10^{-2}
Decibels 10log⁡10(P/P0)10\log_{10}(P/P_0) for signal power in sensors
📝 Quick check (Level 2)
0/3 answered
  1. 1. softmax([3, 3, 3]) is…
  2. 2. Why subtract max(z) before computing softmax?
  3. 3. σ(z) = 0.5 when z equals…

Level 3: Advanced

★★★★☆ Advanced⏱ 45 minGraduate level, with full derivations and proofs.

Defining eˣ properly

Three equivalent definitions:

ex=lim⁡n→∞(1+xn)n=∑k=0∞xkk!,and y=ex is the unique solution of y′=y, y(0)=1e^x = \lim_{n\to\infty}\Big(1 + \frac{x}{n}\Big)^n = \sum_{k=0}^{\infty}\frac{x^k}{k!}, \qquad \text{and } y = e^x \text{ is the unique solution of } y' = y,\ y(0)=1

From the series: e=1+1+12+16+124+…e = 1 + 1 + \tfrac12 + \tfrac16 + \tfrac1{24} + \dots. The law of exponents ex+y=exeye^{x+y} = e^x e^y follows by multiplying the two series and grouping terms with the binomial theorem. The log rules then follow from it, because ln⁡\ln is the inverse of exp⁡\exp.

Two inequalities you’ll use constantly

Theorem

For all real xx: ex≥1+xe^x \ge 1 + x. For all x>0x > 0: ln⁡x≤x−1\ln x \le x - 1. Equality holds only at x=0x = 0 and x=1x = 1 respectively.

Proof. Let g(x)=ex−1−xg(x) = e^x - 1 - x. Then g′(x)=ex−1g'(x) = e^x - 1, which is negative for x<0x < 0 and positive for x>0x > 0. So gg has its minimum at 0, where g(0)=0g(0) = 0. Hence g≥0g \ge 0. For the second, put x=ln⁡tx = \ln t into the first to get t≥1+ln⁡tt \ge 1 + \ln t. ∎

These give (1−p)m≤e−pm(1 - p)^m \le e^{-pm}, the step used in the PAC bound (A1-1), and they’re behind the proof that KL divergence is never negative (A7).

Log-sum-exp is a smooth maximum

Theorem

For z∈Rn\mathbf z \in \mathbb{R}^n: max⁡izi≤LSE⁡(z)≤max⁡izi+ln⁡n\max_i z_i \le \operatorname{LSE}(\mathbf z) \le \max_i z_i + \ln n. With temperature T>0T > 0: T LSE⁡(z/T)→max⁡iziT\,\operatorname{LSE}(\mathbf z / T) \to \max_i z_i as T→0T \to 0.

Proof. Let m=max⁡izim = \max_i z_i. The sum ∑jezj\sum_j e^{z_j} contains the term eme^m and has nn terms, each ≤em\le e^m, so em≤∑jezj≤neme^m \le \sum_j e^{z_j} \le n e^m. Take logs. For the temperature version, apply the same bounds to z/T\mathbf z / T and multiply by TT: m≤T LSE⁡(z/T)≤m+Tln⁡n→mm \le T\,\operatorname{LSE}(\mathbf z/T) \le m + T\ln n \to m. ∎

Theorem

∇LSE⁡(z)=softmax⁡(z)\nabla \operatorname{LSE}(\mathbf z) = \operatorname{softmax}(\mathbf z), and LSE is convex.

Proof. ∂ziln⁡∑jezj=ezi/∑jezj\partial_{z_i} \ln \sum_j e^{z_j} = e^{z_i} / \sum_j e^{z_j}. The Hessian is diag⁡(p)−pp⊤\operatorname{diag}(\mathbf p) - \mathbf p \mathbf p^\top with p=softmax⁡(z)\mathbf p = \operatorname{softmax}(\mathbf z). For any v\mathbf v, v⊤Hv=∑ipivi2−(∑ipivi)2\mathbf v^\top H \mathbf v = \sum_i p_i v_i^2 - (\sum_i p_i v_i)^2, which is the variance of vv under p\mathbf p, so it’s ≥0\ge 0. ∎ (Convexity is covered in A6.)

A useful consequence: the cross-entropy loss with softmax is ℓ=LSE⁡(z)−zy\ell = \operatorname{LSE}(\mathbf z) - z_y, and its gradient is the beautifully simple ∇zℓ=p−ey\nabla_{\mathbf z}\ell = \mathbf p - \mathbf e_y, “prediction minus truth”.

Exercise 3.1: Prove that σ’(z) = σ(z)(1 − σ(z))

σ(z)=(1+e−z)−1\sigma(z) = (1 + e^{-z})^{-1}, so σ′(z)=e−z(1+e−z)−2=σ(z)⋅e−z1+e−z=σ(z)(1−σ(z))\sigma'(z) = e^{-z}(1+e^{-z})^{-2} = \sigma(z)\cdot\frac{e^{-z}}{1+e^{-z}} = \sigma(z)(1 - \sigma(z)). ∎

Exercise 3.2: Show that the sigmoid is a 2-class softmax

softmax⁡(z,0)1=ezez+e0=11+e−z=σ(z)\operatorname{softmax}(z, 0)_1 = \frac{e^z}{e^z + e^0} = \frac{1}{1 + e^{-z}} = \sigma(z). Logistic regression is the 2-class case of softmax regression.

📝 Quick check (Level 3)
0/3 answered
  1. 1. LSE([5, 5, 5, 5]) equals…
  2. 2. Which is the gradient of cross-entropy with softmax, with respect to the logits?
  3. 3. As the temperature T → 0, softmax(z/T) tends to…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Where softmax came from

The softmax form pi∝e−Ei/kTp_i \propto e^{-E_i / kT} is the Boltzmann distribution of statistical physics (Boltzmann, 1868): states with lower energy EiE_i are more probable, and the temperature TT controls how sharp the preference is. Bridle (1990) gave the name “softmax” to its use as the output layer of neural networks. The physics language survives in ML: “temperature” in LLM sampling, “energy-based models” (H6), and −LSE⁡-\operatorname{LSE} as a free energy.

Numerics: the details that matter in practice

  • float64 overflows at e709.78e^{709.78}. float32 overflows at e88.7e^{88.7}, which a single bad logit can reach, so mixed-precision training keeps softmax and loss computations in float32.
  • For small xx, ln⁡(1+x)\ln(1+x) and ex−1e^x - 1 lose accuracy. Use log1p and expm1.
  • Blanchard, Higham and Higham (2021) analysed exactly how accurate the shifted LSE and softmax algorithms are. They’re stable, but the details of the algorithm matter.
  • Log-domain algorithms. Hidden Markov models, CTC speech recognition (Graves et al., 2006), and particle filters in robot localization (T1) multiply long chains of probabilities. They work entirely with logs and combine them with logaddexp.

The Gumbel-max trick: sampling as an argmax

If gig_i are independent samples from the Gumbel distribution, g=−ln⁡(−ln⁡u)g = -\ln(-\ln u) with u∼Uniform(0,1)u \sim \text{Uniform}(0,1), then

arg⁡max⁡i (zi+gi) ∼ softmax⁡(z).\arg\max_i\,(z_i + g_i) \ \sim\ \operatorname{softmax}(\mathbf z).

This result comes from extreme-value theory (Gumbel, 1954). Replacing the argmax with a softmax at temperature τ\tau gives the Gumbel-softmax (Concrete) relaxation (Jang et al., 2017; Maddison et al., 2017). It lets gradients flow through discrete choices, which is used in discrete latent-variable models and some robot-learning policies.

🐍 Exercise 2 (research): verify the Gumbel-max trick
Implement gumbel_max_sample, then run 40,000 samples and compare the frequencies to softmax.

Temperature as a research tool

  • Knowledge distillation (Hinton, Vinyals and Dean, 2015) trains a small model on a large model’s softmax outputs at a high temperature, so the “dark knowledge” in the small probabilities is visible.
  • LLM sampling uses temperature to trade off diversity against accuracy.
  • Contrastive learning (InfoNCE, E16) uses a temperature in the softmax over similarities, and the choice strongly affects the learned representations.

Exponentials in robotics

Rotations in 3D are produced by a matrix exponential: a rotation by angle θ\theta about the unit axis ω\boldsymbol\omega is R=exp⁡(θ[ω]×)R = \exp(\theta[\boldsymbol\omega]_\times). The “log map” goes back from a rotation to an axis and angle. These maps, on Lie groups, are the foundation of modern state estimation and SLAM (R2, T3).

References

  1. J. Napier (1614). Mirifici Logarithmorum Canonis Descriptio. (The invention of logarithms.)
  2. L. Euler (1748). Introductio in analysin infinitorum. (e, the exponential series, and e^x as a limit.)
  3. L. Boltzmann (1868). “Studien über das Gleichgewicht der lebendigen Kraft zwischen bewegten materiellen Punkten.” Wiener Berichte, 58.
  4. E. Gumbel (1954). Statistical Theory of Extreme Values and Some Practical Applications. US National Bureau of Standards, Applied Mathematics Series 33.
  5. J. Bridle (1990). “Probabilistic interpretation of feedforward classification network outputs, with relationships to statistical pattern recognition.” In Neurocomputing, Springer.
  6. A. Graves, S. Fernández, F. Gomez, J. Schmidhuber (2006). “Connectionist temporal classification.” ICML.
  7. G. Hinton, O. Vinyals, J. Dean (2015). “Distilling the knowledge in a neural network.” arXiv:1503.02531.
  8. E. Jang, S. Gu, B. Poole (2017). “Categorical reparameterization with Gumbel-softmax.” ICLR.
  9. C. Maddison, A. Mnih, Y. W. Teh (2017). “The Concrete distribution: a continuous relaxation of discrete random variables.” ICLR.
  10. P. Blanchard, D. Higham, N. Higham (2021). “Accurately computing the log-sum-exp and softmax functions.” IMA Journal of Numerical Analysis, 41(4).