Skip to main content
QUICK REVIEW

[Paper Review] Accurate Computation of the Log-Sum-Exp and Softmax Functions

Pierre Blanchard, Desmond J. Higham|arXiv (Cornell University)|Sep 8, 2019
Numerical Methods and AlgorithmsComputer Science15 references6 citations
TL;DR

This paper provides a rigorous rounding error analysis of shifted algorithms for computing the log-sum-exp and softmax functions, showing that shifting by the maximum input value preserves accuracy and often improves it over division-free alternatives. The key contribution is a theoretical bound demonstrating that the relative error is proportional to the condition number and machine precision, validating the widespread use of shifting in practice.

ABSTRACT

Evaluating the log-sum-exp function or the softmax function is a key step in many modern data science algorithms, notably in inference and classification. Because of the exponentials that these functions contain, the evaluation is prone to overflow and underflow, especially in low precision arithmetic. Software implementations commonly use alternative formulas that avoid overflow and reduce the chance of harmful underflow, employing a shift or another rewriting. Although mathematically equivalent, these variants behave differently in floating-point arithmetic. We give rounding error analyses of different evaluation algorithms and interpret the error bounds using condition numbers for the functions. We conclude, based on the analysis and numerical experiments, that the shifted formulas are of similar accuracy to the unshifted ones and that the shifted softmax formula is typically more accurate than a division-free variant.

Motivation & Objective

  • To analyze the numerical accuracy of shifted algorithms for computing the log-sum-exp and softmax functions in floating-point arithmetic.
  • To quantify the impact of shifting (e.g., using max(x_i)) on rounding errors in low-precision formats like bfloat16, fp16, and fp32.
  • To compare the accuracy of shifted versus unshifted and division-free variants of the softmax function.
  • To provide theoretical error bounds using condition numbers and floating-point error analysis.
  • To validate the practical robustness of shifted algorithms in preventing overflow and underflow while maintaining accuracy.

Proposed method

  • Proposes a shifted algorithm for log-sum-exp: y = a + log(1 + sum(exp(x_i - a))), where a = max(x_i), to avoid overflow and underflow.
  • Analyzes rounding errors in the evaluation of the sum of exponentials and the final log1p operation using standard floating-point error bounds.
  • Derives a relative error bound for the computed log-sum-exp value: |(y - ŷ)/y| ≤ |(y + n - x_min)/y| u + O(u²), where u is the unit roundoff.
  • Applies similar analysis to the softmax function, showing that the shifted version is typically more accurate than division-free alternatives.
  • Uses condition number analysis to interpret the sensitivity of the functions and contextualize the error bounds.
  • Employs Taylor series expansions and perturbation analysis to bound the error propagation in the log1p and exponential operations.

Experimental results

Research questions

  • RQ1How does shifting the inputs by the maximum value affect the relative error in computing the log-sum-exp function in floating-point arithmetic?
  • RQ2Is the shifted log-sum-exp algorithm more accurate than the unshifted or division-free variants, especially in low-precision formats?
  • RQ3What is the theoretical error bound for the shifted log-sum-exp computation, and how does it relate to the condition number of the function?
  • RQ4Can the use of log1p in the shifted algorithm prevent harmful underflow while maintaining accuracy?
  • RQ5Why is the shifted softmax function more accurate than its division-free counterpart in practice?

Key findings

  • The shifted log-sum-exp algorithm achieves relative error bounded by |(y + n - x_min)/y| u + O(u²), which is proportional to the condition number and machine precision.
  • The shifted algorithm avoids overflow and mitigates harmful underflow, as underflowed terms are negligible in the sum.
  • Numerical experiments confirm that the shifted algorithm is typically more accurate than the division-free softmax variant.
  • The analysis shows that shifting does not degrade accuracy and can improve it by reducing the influence of large exponentials.
  • The use of log1p in the final step ensures stable evaluation of log(1 + s), even when s is small.
  • The error bound is tight and explains why the shifted method is robust across different precisions, including bfloat16 and fp16.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.