Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Logit
- A model’s raw score for one option; any real number, with larger meaning more likely.
- Softmax
- e^{zᵢ} / Σⱼ e^{zⱼ}: turns logits into positive probabilities that add up to 1.
- Temperature
- Dividing logits by T before softmax: below 1 sharpens the distribution, above 1 flattens it.
- Log-sum-exp
- ln Σ e^{xⱼ}, computed as m + ln Σ e^{xⱼ − m} with m the maximum so nothing overflows.
- Ghost text
- An inline completion an editor draws in grey for the user to accept with Tab.
Why this matters
Many language models, classifiers and recommenders use softmax, and products put thresholds on its output: show the suggestion, auto-label the ticket, send the alert. Temperature is a dial on the same output, so changing it moves every threshold. And the exponential grows so fast that a straightforward implementation overflows exactly when the model is most certain.
This lesson assumes exponentials, logarithms and ratios.
What softmax normalises
A logit is a model score before normalisation. Softmax converts a list of logits into a probability distribution by exponentiating each score and dividing by the sum of those exponential weights. The resulting probabilities sum to one in the mathematical model.
Temperature is a positive divisor applied to every logit before softmax. It changes the gaps between the scores that enter the exponentials. A sampling setting and a display threshold therefore both depend on how the scores become probabilities; those probabilities still need evaluation against real use.
01From scores to probabilities
From scores to probabilities
A code editor shows an inline completion as grey ghost text only when the model’s top suggestion is likely enough. The team changed the sampling temperature to make suggestions more varied, and the ghost text all but vanished. When someone tried a near-greedy temperature instead, the confidence readout in the logs became NaN.
After print(user.name the model scores four completions: ) 4.2, ); 3.9, a comma 1.1 and a space 0.3. Softmax exponentiates each score and divides by the total: 0.5537, 0.4102, 0.0249, 0.0112. A gap of 1 in score is a factor of e ≈ 2.72 in probability.
const logits = [4.2, 3.9, 1.1, 0.3]; // ")", ");", ",", " " after print(user.name
const softmax = (z: number[]) => {
const total = z.reduce((sum, v) => sum + Math.exp(v), 0);
return z.map((v) => Math.exp(v) / total);
};
for (const t of [0.7, 1, 1.3])
console.log(t, softmax(logits.map((z) => z / t))[0]!.toFixed(4)); // top probability
const near = [8.0, 7.99, 7.97].map((z) => z / 0.01); // T = 0.01
console.log(near.reduce((sum, v) => sum + Math.exp(v), 0)); // the naive denominator
const shift = Math.max(...near);
console.log((1 / near.reduce((sum, v) => sum + Math.exp(v - shift), 0)).toFixed(4)); // the top probability, stably
02Temperature reshapes, it does not reorder
Temperature reshapes, it does not reorder
Dividing every score by T before softmax multiplies the gaps by 1/T. Below 1 the leader pulls ahead; above 1 the field closes up. The order of the completions never changes, so the same completion is on top at every temperature; only the model’s stated confidence moves. The first two lab levels use direct exponentials only within a bounded numerical domain: a nonempty candidate list, positive T, finite scaled logits, finite exponential weights and a finite, strictly positive sum. A zero sum from all-underflow or an infinite sum is outside those P/G requests. The later L request addresses a broader range of finite scaled logits.
| T | Probability | Gate 0.53 | Gate 0.5 |
|---|---|---|---|
| 0.7 | 0.5998 | show | show |
| 1.0 | 0.5537 | show | show |
| 1.3 | 0.5166 | hide | show |
03A gate on a moving number
A gate on a moving number
A gate of 0.53 shows this same completion at T = 1 and hides it at T = 1.3: the ranking did not change, but the product decision did. The gate was tuned at one temperature. Raise the temperature for variety and the same suggestions fall below it; lower it and they rise above. A gate on a softmax probability has to be re-tuned whenever temperature changes, or set on the T = 1 probability while sampling uses another T.
04Subtract the maximum
Subtract the maximum
At T = 0.01 the scores 8.00, 7.99 and 7.97 become 800, 799 and 797, and e⁸⁰⁰ is far beyond the largest double, about 1.8 × 10³⁰⁸. The total becomes infinity and the probability NaN. Subtracting the largest scaled score from all of them first changes nothing mathematically, because the factor cancels, but every exponent is now at most 0. The probability of ) is 0.7054, and its log is (800 − 800) − ln(1 + e⁻¹ + e⁻³) = −0.3490. Cancel the common shift before subtracting the logarithm: with a common offset of 10³⁰⁰, adding a small log-sum to that offset first rounds it away. Two equal top scores must still have probabilities 0.5, not 1. This assumes a positive T and finite scaled logits; max subtraction prevents exponential overflow but cannot repair an earlier overflow in z/T.
Math.exp returns Infinity above about 709.78; Infinity / Infinity is NaN, which is what the readout showed.
05Confidence is not accuracy
Confidence is not accuracy
06Gate the ghost text
Gate the ghost text
First turn the completion scores into probabilities. Apply the chosen temperature before checking the display threshold. Finally make the log readout survive both very large exponentials and huge common score offsets, so the logs still explain the editor’s decision.
An inline completion gate
Probabilities from logits, the temperature gate, then stable confidence.
In the frontend The ghost-text gate in Next.js and Svelte Optional
Editor extensions often run the gate in the client: scale by temperature, subtract the maximum, exponentiate, and draw nothing when the top probability is below the threshold.
// The editor extension's ghost-text gate: stable softmax at the sampling temperature.
type Candidate = { text: string; logit: number };
export function topCompletion(
candidates: Candidate[],
temperature: number,
minimum: number
): string | null {
const scaled = candidates.map((c) => c.logit / temperature);
const shift = Math.max(...scaled); // keeps every exponent at most 0
const weights = scaled.map((v) => Math.exp(v - shift));
const total = weights.reduce((sum, w) => sum + w, 0);
const top = weights.indexOf(Math.max(...weights));
return weights[top]! / total >= minimum ? candidates[top]!.text : null; // null: draw nothing
}
Check this idea later
Sources
- Goodfellow, Bengio and Courville: numerical computation (softmax overflow)
- Hinton, Vinyals and Dean (2015): distilling the knowledge in a neural network (temperature)
Values above were computed at 60 digits and run in Node 22, Go 1.27, Rust 1.99 and Apple clang 17. The completions and logits are a worked model.
Sign in to keep your read marks on every device. Sign in
Was this helpful?