← Nestor G Pestelos Jr · Reference

Mathematics & Machine Learning

Softmax Function

August 25, 2026

Softmax is a normalized exponential mathematical function that maps a \(K\)-dimensional vector of arbitrary real-valued numbers (logits) into a \(K\)-dimensional vector of real values in the range \((0, 1)\) that sum to exactly \(1\), forming a valid discrete probability distribution. Originally introduced by John S. Bridle in 1990 for feed-forward neural networks [1], Softmax serves as the foundational probability layer for multi-class classification, autoregressive next-token prediction, self-attention routing in transformer architectures, and sparse expert gating in Mixture-of-Experts systems.

1. Mathematical Formulation

1.1 Standard Definition

Given an input vector \(\mathbf{z} = (z_1, z_2, \dots, z_K) \in \mathbb{R}^K\), the Softmax function \(\sigma: \mathbb{R}^K o (0, 1)^K\) is defined for each component \(i \in \{1, \dots, K\}\) as:

\[\sigma(\mathbf{z})_i = rac{e^{z_i}}{\sum_{j=1}^K e^{z_j}}\]

The denominator \(\sum_{j=1}^K e^{z_j}\) acts as a normalizing partition function (analogous to the Boltzmann distribution in statistical mechanics), ensuring that the output elements satisfy the Kolmogorov probability axioms.

1.2 Core Mathematical Properties

2. Numerical Stability

2.1 Floating-Point Hazards

Direct evaluation of \(\sigma(\mathbf{z})_i\) using standard IEEE 754 floating-point representations (such as FP32, FP16, or BF16) is numerically unstable:

2.2 The Subtract-Max Invariance Trick

Using the shift-invariance property, production implementations subtract the maximum element \(m = \max_j(z_j)\) from the logit vector before computing the exponential:

\[\sigma(\mathbf{z})_i = rac{e^{z_i - \max_j(z_j)}}{\sum_{k=1}^K e^{z_k - \max_j(z_j)}}\]

Because \(z_i - \max_j(z_j) \le 0\) for all \(i\), every exponent is bounded in \((-\infty, 0]\), ensuring that \(e^{z_i - m} \in (0, 1]\). Overflow is mathematically eliminated, and at least one term in the denominator equals \(e^0 = 1\), preventing division by zero [3].

2.3 Log-Softmax Formulation

When computing loss functions, calculating \(\log(\sigma(\mathbf{z})_i)\) directly from Softmax outputs introduces precision loss. Deep learning frameworks implement the LogSoftmax primitive via the Log-Sum-Exp operator:

\[\log \sigma(\mathbf{z})_i = z_i - ext{LSE}(\mathbf{z}) = z_i - \left( m + \log \sum_{j=1}^K e^{z_j - m} ight)\]

3. Temperature Scaling

Temperature scaling introduces a strictly positive hyperparameter \(T > 0\) into the logit arguments:

\[\sigma(\mathbf{z}, T)_i = rac{e^{z_i / T}}{\sum_{j=1}^K e^{z_j / T}}\]

Temperature modulates the entropy (flatness vs. sharpness) of the resulting probability distribution:

In post-processing, temperature scaling is also used to calibrate confidence probabilities in neural network classifiers without altering class rankings [4].

4. Derivatives and Gradient Backpropagation

4.1 Jacobian Matrix

The derivative of the \(i\)-th Softmax output \(\sigma_i\) with respect to the \(j\)-th input logit \(z_j\) is given by the quotient rule:

\[ rac{\partial \sigma_i}{\partial z_j} = \sigma_i (\delta_{ij} - \sigma_j) = egin{cases} \sigma_i (1 - \sigma_i) & ext{if } i = j \ -\sigma_i \sigma_j & ext{if } i eq j \end{cases}\]

where \(\delta_{ij}\) is the Kronecker delta. In vector form, the Jacobian matrix \(J \in \mathbb{R}^{K imes K}\) is:

\[J = ext{diag}(\sigma) - \sigma \sigma^T\]

4.2 Combined Softmax and Cross-Entropy Loss

In classification and autoregressive language modeling, Softmax is paired with categorical Cross-Entropy loss \(\mathcal{L} = -\sum_{k=1}^K y_k \log(\hat{y}_k)\), where \(\mathbf{y}\) is the ground-truth one-hot target vector and \(\hat{\mathbf{y}} = \sigma(\mathbf{z})\).

Applying the chain rule produces an elegant, numerically stable gradient with respect to the pre-activation logit \(z_i\):

\[ rac{\partial \mathcal{L}}{\partial z_i} = \sum_{k=1}^K rac{\partial \mathcal{L}}{\partial \hat{y}_k} rac{\partial \hat{y}_k}{\partial z_i} = \hat{y}_i - y_i\]

The gradient is simply the signed residual error between the predicted probability distribution and the true target [2].

5. Applications in Modern AI Architectures

5.1 Scaled Dot-Product Attention in Transformers

In the transformer architecture [5], Softmax computes dynamic attention weights between query matrix \(Q\) and key matrix \(K\):

\[ ext{Attention}(Q, K, V) = ext{softmax}\left( rac{QK^T}{\sqrt{d_k}} ight)V\]

The scaling factor \( rac{1}{\sqrt{d_k}}\) prevents dot products from growing excessively large in high dimensions, which would otherwise push the Softmax function into regions with near-zero gradients (the vanishing gradient regime).

5.2 Sparse Mixture-of-Experts Gating

In Mixture-of-Experts (MoE) architectures (such as DeepSeek-V4-Flash and Llama 4), a Softmax gating router determines which feed-forward expert networks process each token [6]:

\[G(x) = ext{softmax}( ext{TopK}(H(x), k))\]

5.3 Autoregressive Decoding and Sampling

Autoregressive language models project the final hidden state \(h_t\) across the vocabulary matrix \(W_v\) to produce vocabulary logits \(\mathbf{z}_t \in \mathbb{R}^{|V|}\). Softmax converts these logits into the next-token probability distribution \(P(w_{t+1} \mid w_{1:t}) = ext{softmax}(\mathbf{z}_t / T)\), which is subsequently filtered via Top-\(p\) (nucleus) or Top-\(k\) truncation prior to stochastic sampling [7].

6. Variants and Continuous Relaxations

See also

References

  1. [1] J. S. Bridle, "Probabilistic Interpretation of Feedforward Classification Network Outputs, with Relationships to Statistical Pattern Recognition," in Neurocomputing: Algorithms, Architectures and Applications, Springer, 1990, pp. 227–236.
  2. [2] I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016. https://www.deeplearningbook.org
  3. [3] C. M. Bishop, Pattern Recognition and Machine Learning, Springer, 2006.
  4. [4] C. Guo, G. Pleiss, Y. Sun, and K. Q. Weinberger, "On Calibration of Modern Neural Networks," in International Conference on Machine Learning (ICML), 2017. https://arxiv.org/abs/1706.04599
  5. [5] A. Vaswani, N. Shazeer, N. Parmar, J. Uszkoreit, L. Jones, A. N. Gomez, L. Kaiser, and I. Polosukhin, "Attention Is All You Need," in Advances in Neural Information Processing Systems (NeurIPS), vol. 30, 2017. https://arxiv.org/abs/1706.03762
  6. [6] N. Shazeer, A. Mirhoseini, K. Maziarz, A. Davis, Q. Le, G. Hinton, and J. Dean, "Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer," in ICLR, 2017. https://arxiv.org/abs/1701.06538
  7. [7] A. Holtzman, J. Buys, L. Du, M. Forbes, and Y. Choi, "The Curious Case of Neural Text De-Generation," in ICLR, 2020. https://arxiv.org/abs/1904.09751
  8. [8] E. Jang, S. Gu, and B. Poole, "Categorical Reparameterization with Gumbel-Softmax," in ICLR, 2017. https://arxiv.org/abs/1611.01144
  9. [9] A. F. Martins and R. F. Astudillo, "From Softmax to Sparsemax: A Sparse Model of Attention and Multi-Label Classification," in ICML, 2016. https://arxiv.org/abs/1602.02068
  10. [10] E. Grave, A. Joulin, M. Cissé, D. Grangier, and H. Jégou, "Efficient Softmax Approximation for GPUs," in ICML, 2017. https://arxiv.org/abs/1609.04309