Explain softmax and the temperature parameter. How do you compute softmax stably, and where does temperature show up across ML?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
Softmax maps logits to a probability distribution: p_i = exp(z_i/T) / Σ_j exp(z_j/T). With temperature T=1 it's the standard form. As T→0 it approaches argmax (all mass on the top logit); as T→∞ it flattens toward uniform. Dividing by T rescales logit differences, which is what controls sharpness. Softmax is invariant to adding a constant to all logits - only differences matter - which enables the stability trick.
Full worked answer
Softmax maps logits to a probability distribution: p_i = exp(z_i/T) / Σ_j exp(z_j/T). With temperature T=1 it's the standard form. As T→0 it approaches argmax (all mass on the top logit); as T→∞ it flattens toward uniform. Dividing by T rescales logit differences, which is what controls sharpness. Softmax is invariant to adding a constant to all logits - only differences matter - which enables the stability trick.
Numerical stability: exp overflows fast (exp(89) overflows fp32). Subtract the max logit first - mathematically a no-op, numerically essential:
def softmax(z, T=1.0):
z = z / T
z = z - z.max(axis=-1, keepdims=True) # log-sum-exp trick
e = np.exp(z)
return e / e.sum(axis=-1, keepdims=True)
In practice you compute log_softmax directly and use it with NLL for a fused, stable cross-entropy - never log(softmax(z)) in two steps (catastrophic when a probability underflows to 0).
Temperature appears all over the stack, and connecting the dots is what makes this answer senior:
- LLM sampling: T scales next-token logits before sampling. Low T → deterministic, repetitive; high T → diverse, error-prone. T=0 conventionally means greedy decoding. Works alongside top-p/top-k truncation.
- Knowledge distillation (Hinton et al.): soften the teacher's distribution with T>1 so "dark knowledge" in the non-argmax classes carries gradient signal to the student.
- Contrastive losses (InfoNCE/CLIP/SimCLR): similarity scores divided by τ (~0.05-0.1) before the softmax; τ controls how hard the loss focuses on the most confusable negatives. CLIP famously learns its temperature.
- Calibration: temperature scaling fits a single T on validation logits to fix overconfidence - same knob, different objective.
- Attention: the 1/√d_k factor in scaled dot-product attention is exactly a fixed temperature keeping logit variance ~1 so the softmax doesn't saturate at init.
What a good answer covers
- Softmax maps logits to a probability distribution: p_i = exp(z_i/T) / Σ_j exp(z_j/T)
- With temperature T=1 it's the standard form
- As T→0 it approaches argmax (all mass on the top logit); as T→∞ it flattens toward uniform
- Dividing by T rescales logit differences, which is what controls sharpness
- Softmax is invariant to adding a constant to all logits - only differences matter - which enables the…
Key terms: T→0 · T→∞ · Numerical stability · LLM sampling · Knowledge distillation · Contrastive losses · Calibration · Attention
In the glossary: Nucleus sampling (top-p) · InfoNCE · CLIP · Calibration · Temperature scaling · Attention
Interviewer follow-ups to expect
Why does attention divide by √d_k - what happens without it?
Temperature appears all over the stack, and connecting the dots is what makes this answer senior:
- LLM sampling: T scales next-token logits before sampling. Low T → deterministic, repetitive; high T → diverse, error-prone. T=0 conventionally means greedy decoding. Works alongside top-p/top-k truncation.
- Knowledge distillation (Hinton et al.): soften the teacher's distribution with T>1 so "dark knowledge" in the non-argmax classes carries gradient signal to the student.
- Contrastive losses (InfoNCE/CLIP/SimCLR): similarity scores divided by τ (~0.05-0.1) before the softmax; τ controls how hard the loss focuses on the most confusable negatives. CLIP famously learns its temperature.
- Calibration: temperature scaling fits a single T on validation logits to fix overconfidence - same knob, different objective.
- Attention: the 1/√d_k factor in scaled dot-product attention is exactly a fixed temperature keeping logit variance ~1 so the softmax doesn't saturate at init.
Why is low temperature not the same as top-k truncation?
What breaks numerically if you compute log(softmax(z)) naively?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep