Softmax converts a vector of raw scores (logits) into a probability distribution:
Properties:
- Output sums to 1, all entries in → valid probability distribution
- Monotonic: larger logits get larger probabilities
- Translation invariant: — in practice, subtract for numerical stability
- Temperature scaling: — low → sharper (approaches argmax), high → more uniform
Gradient:
Numerical stability: overflows for large . Exploit translation invariance and subtract the max so the largest exponent is :
For log-softmax, use where . This avoids both overflow (largest term is 1) and underflow (the sum is ).
Online softmax trick: fuse the max-finding and denominator into a single pass by keeping a running max and running denominator , rescaling when the max changes:
When the max is unchanged, the rescale factor is 1. The same recurrence applied to a running weighted sum is the core of FlashAttention, where it lets attention be computed in tiles without ever materializing the full score matrix.
Where it appears:
- Classification output layer — paired with cross-entropy loss
- Self-Attention — softmax over scaled dot-product scores turns them into attention weights
- Reinforcement learning — softmax policy converts action values to action probabilities
- Contrastive learning — InfoNCE / softmax over similarity scores
- Sampling and Decoding Strategies — temperature-scaled softmax controls randomness of generation
Softmax vs sigmoid: sigmoid is the 2-class special case. For multi-label problems (multiple independent binary decisions), use sigmoid per class, not softmax.
See also: Activation Functions, Entropy and Cross-Entropy, Self-Attention