Softmax

Softmax converts logits into a probability distribution over mutually exclusive choices.

Here is a logit and is the normalized probability for class or option .

Softmax is invariant to adding the same constant to all logits:

This is used for numerical stability:

Log-Softmax

Many losses use log probabilities directly:

The stable form uses log-sum-exp:

This avoids overflow and is why implementations often combine log-softmax with negative log-likelihood or cross-entropy.

Temperature

A temperature rescales logits:

Small makes the distribution sharper. Large makes it flatter. Temperature is used in decoding, calibration, distillation, and contrastive losses.

With one-hot target , softmax cross-entropy is:

For logits and probabilities :

This simple gradient is one reason softmax and cross-entropy are usually implemented together.

Masked Softmax

For attention or constrained choices, invalid positions are masked before softmax:

where for valid positions and for invalid positions.

Attention Interpretation

In attention, softmax normalizes scores over keys for each query:

The axis matters: normalizing over keys answers “where should this query attend?” Normalizing over queries would mean a different operation.

Where It Appears

  • Attention weights in Attention.
  • Classification heads.
  • Autoregressive next-token distributions.
  • Routing probabilities in Mixture of experts.

Checks

  • Softmax is sensitive to logit scale.
  • Masks usually add to impossible positions before softmax.
  • For multi-label prediction, sigmoid is usually more appropriate than softmax.
  • Probabilities can still be poorly calibrated; see Calibration.
  • Check the axis: class dimension, token dimension, expert dimension, or candidate list.
  • Check whether logits are raw scores, normalized similarities, or temperature-scaled scores.
  • Use log-softmax or fused cross-entropy for training losses when possible.
  • Confirm that masked positions are impossible after normalization, not merely small.