Softmax: Difference between revisions
| Line 32: | Line 32: | ||
== Temperature parameter == | == Temperature parameter == | ||
[[File:Softmax temperature.gif|thumbnail|400px|'''Temperature''' exaggerates/flattens the probability distribution.|left]] | |||
[[File:LLM temperature.png|thumbnail|400px|left|In a '''[[large language model (LLM)]]''', a higher temperature encourages the model to experiment more with less conventional sentence and passage structures, whereas a lower temperature encourages conservative, conventional construction.]] | [[File:LLM temperature.png|thumbnail|400px|left|In a '''[[large language model (LLM)]]''', a higher temperature encourages the model to experiment more with less conventional sentence and passage structures, whereas a lower temperature encourages conservative, conventional construction.]] | ||
A temperature parameter $\tau>0$ controls the sharpness of the distribution: | A temperature parameter $\tau>0$ controls the sharpness of the distribution: | ||
Revision as of 12:44, 11 November 2026

Softmax is a normalisation function that maps a vector of real-valued logits to a categorical probability distribution.
Definition
For $\vec z=(z_1,\dots,z_K)^{\top}\in\mathbb{R}^{K}$,
$$ \operatorname{softmax}(\vec z)_i = \frac{\exp(z_i)} {\sum_{j=1}^{K}\exp(z_j)}, \qquad i=1,\dots,K. $$
Shift and numerical stability
In practice, it is evaluated with a shift for numerical stability. Let $m=\max_{j=1,\dots,K}z_j$; then
$$ \operatorname{softmax}(\vec z)_i = \frac{\exp(z_i-m)} {\sum_{j=1}^{K}\exp(z_j-m)}. $$
Mathematically, the shift $m$ represents dividing both the numerator and denominator by $e^m$ to simplify calculation. e.g., to calculate the softmax of $\vec z = (100,102,101)^\top$, one calculate instead softmax of $\vec z - \vec 1 \cdot m = (100-m,102-m,101-m)^\top$, where $m$ can be, for example, $102$.
Temperature parameter


A temperature parameter $\tau>0$ controls the sharpness of the distribution:
$$ \operatorname{softmax}(\vec z/\tau)_i = \frac{\exp(z_i/\tau)} {\sum_{j=1}^{K}\exp(z_j/\tau)}. $$
As $\tau\to 0^{+}$, the distribution approaches an one-hot vector; as $\tau\to\infty$, it approaches the uniform distribution.
Common form
Including both the shift and the temperature parameter:
$$ \operatorname{softmax}(\vec z/\tau)_i = \frac{\exp((z_i-m)/\tau)} {\sum_{j=1}^{K}\exp((z_j-m)/\tau)}. $$ where $m=\max_{j=1,\dots,K}z_j$ and $\tau \in (0,\infty)$.
Properties
$\sum_{i=1,\dots,K} \operatorname{softmax}(\vec z/\tau)_i=1$
as required to constitute a categorical probability distribution.