Softmax
A normalization function that converts an arbitrary vector of real-valued logits into a valid probability distribution summing to 1, powering multi-class classification, Self-Attention weights, and LLM token sampling
Table of Contents
Structure Diagram
What is Softmax?
A normalization function that converts an arbitrary vector of real-valued logits into a valid probability distribution summing to 1, powering multi-class classification, Self-Attention weights, and LLM token sampling. It is a core concept in Model Architecture within AI / ML.
Core Principles
Softmax converts any real-valued score vector z into a differentiable probability distribution via Softmax(zᵢ) = exp(zᵢ) / Σ exp(zⱼ). Exponentiating amplifies the largest scores while ensuring all outputs are positive and sum to 1.
It appears everywhere in modern AI: LLM next-token decoding (right after Temperature and Logit Bias), Self-Attention weight normalization, MoE routing, and multi-class classification.
Practical Takeaways
- Always use Safe Softmax (Max-Shift): Subtract
max(z)before exponentiating (exp(z - max(z))) to prevent FP16/FP32 overflow (Inf/NaN) without changing the mathematical result. - Do not mistake Softmax probability for calibrated confidence: Because of exponentiation, models often output
0.99even on out-of-distribution inputs; apply temperature calibration if you rely on confidence thresholds. - Fuse Softmax with Cross-Entropy during training: Pass raw logits directly into
CrossEntropyLoss(which uses the log-sum-exp trick) for better numerical stability and speed.
Notable Examples
- LLM Next-Token Sampling: Normalizes 100K+ vocabulary logits into token probabilities after Temperature scaling.
- Self-Attention Normalization: Turns scaled dot products into attention weights summing to 1 across the context.
- MoE Gating Networks: Computes normalized combination weights over selected Top-K experts.