Module 0 · optional · Lesson 0.4

Softmax and numerical stability

Read stable softmax as a row operation. By the end, you should identify the two indices and the row maximum, compute one probability, and explain exact equivalence versus finite-precision overflow.

Prerequisite: sums, maxima and free indices from 0.2; basic fractions.

0.4.1 Exponentiate, then normalize

Let z be one row of scores, zⱼ its j-th entry, and aⱼ the resulting weight. The exponential exp(x), also written e to the power x, is positive and grows rapidly as its input grows. Softmax divides each exponential by the sum of all exponentials in that row:

a_j = exp(z_j) divided by the sum over k of exp(z_k)

j is the free index naming the requested output. k is a bound reduction index visiting every candidate in the denominator. The denominator includes j too; excluding it would break normalization. In exact arithmetic all finite inputs produce positive weights summing to one.

0.4.2 Subtract one shared maximum

m is the maximum score in the row

Multiplying the original numerator and denominator by the same nonzero factor exp(−m) leaves the ratio unchanged. This identity uses exact real-number arithmetic. In a floating-point implementation, unshifted exponentials can overflow; the shifted inputs are at most zero, so their exponentials are at most one. Very negative differences can still underflow to zero. Subtracting a different amount from each entry would generally change the distribution.

0.4.3 Worked example: predict the maximum first

Use the invented score row [4,8,2,6]. Predict m and write the shifted row before opening the worked calculation.

Reveal maximum and worked probabilities

The maximum m is 8; the shifted row is [−4,0,−6,−2]. The exponentials are approximately [0.018316,1,0.002479,0.135335], whose sum is approximately 1.156130.

weights approximately [0.015842,0.864955,0.002144,0.117059]

The weight for the second position is approximately 1 / 1.156130 = 0.864955. These displayed numbers are rounded; the computation uses unrounded values. Add 1000 to every score to obtain [1004,1008,1002,1006]. The maximum becomes 1008, and subtraction recovers exactly the same shifted row. Direct JavaScript exponentials overflow on these large inputs, while the stable implementation returns finite probabilities.

Common mistake: calling tiny valid weights “masked zeros.” A causal mask deliberately excludes positions. After applying that exclusion, masked positions have exactly zero probability if at least one legal position remains. A row with every position excluded has no valid probability distribution; report it rather than silently returning NaN.

0.4.4 Compare moderate and large inputs

Make a prediction before opening hints. Try the task again without help after checking the reasoning.

0.4.5 Explain the equation without help

Given p_r = exp(u_r minus c) divided by sum over s exp(u_s minus c), c is max u, name the free index, bound index, row scalar and denominator. For u=[−1,1], write the shifted inputs and explain why the maximum entry has the larger probability.

Check the reference explanation

r names one output; s runs through candidates in each reduction. c is the shared row maximum, 1. The shifted row is [−2,0]. Its exponentials are [exp(−2),1], so the second entry gets the greater weight. The denominator sums both exponentials. Subtracting c preserves the exact ratio; it changes the numerical route used to compute it.

Return to softmax in the attention mechanism.

Return to the attention equation