Softmax Function
The softmax function converts a vector of raw real-valued scores (logits) into a probability distribution, exponentiating each score and dividing by the sum of all exponentials so that every output lies between 0 and 1 and the full set of…
Definition
The softmax function converts a vector of raw real-valued scores (logits) into a probability distribution, exponentiating each score and dividing by the sum of all exponentials so that every output lies between 0 and 1 and the full set of outputs sums to exactly 1.
Overview
Softmax is one of the most widely used functions in machine learning, applied whenever a model needs to turn arbitrary real-valued scores into something that behaves like a probability distribution over a fixed set of categories. Given a vector of logits — the raw, unnormalized outputs of a neural network's final layer — softmax computes each output as e raised to that logit divided by the sum of e raised to every logit in the vector. The exponentiation ensures all values are positive, and dividing by the total ensures the results sum to 1, satisfying the basic requirements of a probability distribution. Softmax is the standard output layer for multi-class classification: a model predicting which of several categories an input belongs to (for example, classifying an image as one of a thousand object types) typically ends with a softmax layer, and the resulting probabilities are compared against the true label using cross-entropy loss during training. Because softmax and cross-entropy loss are differentiable, gradients can flow back through the network via backpropagation, letting the model learn to sharpen its predictions toward correct classes. Beyond classification, softmax is the mechanism that makes the attention mechanism in transformer models work: raw attention scores between tokens are passed through softmax to produce normalized weights that sum to 1, determining how much each token 'attends to' every other token. It is also used to convert a language model's final-layer logits into a probability distribution over the entire vocabulary, from which the next token is sampled — a process often modulated by a temperature parameter that divides logits before the softmax to make the distribution sharper (more deterministic, lower temperature) or flatter (more random, higher temperature). A practical numerical issue is that exponentiating large logits can overflow floating-point precision, so implementations commonly subtract the maximum logit from all logits before exponentiating, a mathematically equivalent but numerically stable variant.
Key Concepts
- Exponentiates each input score, then normalizes by the sum of all exponentials
- Produces outputs that are all positive and sum to exactly 1
- Fully differentiable, enabling gradient-based training via backpropagation
- Standard output layer for multi-class classification, paired with cross-entropy loss
- Core normalization step in the transformer self-attention mechanism
- Used to convert next-token logits into a probability distribution for text generation
- Modulated by a temperature parameter to control sampling randomness
- Requires numerical stabilization (max-subtraction) in practical implementations to avoid overflow