Softmax

2 min read

Softmax converts a vector of raw scores (logits) zRK\mathbf{z} \in \mathbb{R}^K into a probability distribution:

softmax(zi)=ezij=1Kezj\text{softmax}(z_i) = \frac{e^{z_i}}{\sum_{j=1}^{K} e^{z_j}}

Properties:

  • Output sums to 1, all entries in (0,1)(0, 1) → valid probability distribution
  • Monotonic: larger logits get larger probabilities
  • Translation invariant: softmax(z+c)=softmax(z)\text{softmax}(\mathbf{z} + c) = \text{softmax}(\mathbf{z}) — in practice, subtract max(z)\max(\mathbf{z}) for numerical stability
  • Temperature scaling: softmax(zi/τ)\text{softmax}(z_i / \tau) — low τ\tau → sharper (approaches argmax), high τ\tau → more uniform

Gradient: zjsoftmax(zi)=softmax(zi)(δijsoftmax(zj))\frac{\partial}{\partial z_j}\text{softmax}(z_i) = \text{softmax}(z_i)(\delta_{ij} - \text{softmax}(z_j))

Numerical stability: ezie^{z_i} overflows for large ziz_i. Exploit translation invariance and subtract the max so the largest exponent is e0=1e^0 = 1:

softmax(z)i=ezizmaxjezjzmax\text{softmax}(z)_i = \frac{e^{z_i - z_{\max}}}{\sum_j e^{z_j - z_{\max}}}

For log-softmax, use logsoftmax(z)i=zilogsumexp(z)\log\text{softmax}(z)_i = z_i - \operatorname{logsumexp}(z) where logsumexp(z)=zmax+logjezjzmax\operatorname{logsumexp}(z) = z_{\max} + \log\sum_j e^{z_j - z_{\max}}. This avoids both overflow (largest term is 1) and log(0)\log(0) underflow (the sum is 1\geq 1).

Online softmax trick: fuse the max-finding and denominator into a single pass by keeping a running max mkm_k and running denominator dkd_k, rescaling when the max changes:

dk+1=dkemkmk+1rescale old+exk+1mk+1new termd_{k+1} = \underbrace{d_k \cdot e^{m_k - m_{k+1}}}_{\text{rescale old}} + \underbrace{e^{x_{k+1} - m_{k+1}}}_{\text{new term}}

When the max is unchanged, the rescale factor is 1. The same recurrence applied to a running weighted sum ok=ieximkvio_k = \sum_i e^{x_i - m_k} v_i 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

Linked from