Skip to main content
QUICK 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 Algorithms参考文献 15被引用 6
一句话总结

本文对用于计算 log-sum-exp 和 softmax 函数的偏移算法进行了严格的舍入误差分析,表明通过将输入值的最大值作为偏移量,可保持精度,且通常优于无除法的替代方法。主要贡献在于提出了一个理论误差界,证明相对误差与条件数和机器精度成正比,从而验证了实践中广泛采用偏移法的合理性。

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.

研究动机与目标

  • 分析在浮点数运算中,用于计算 log-sum-exp 和 softmax 函数的偏移算法的数值精度。
  • 量化在 bfloat16、fp16 和 fp32 等低精度格式中,使用最大值(如 max(x_i))进行偏移对舍入误差的影响。
  • 比较 softmax 函数中偏移与非偏移、无除法变体的精度差异。
  • 利用条件数和浮点数误差分析,提供理论误差界。
  • 验证偏移算法在防止溢出和下溢的同时保持精度的实践鲁棒性。

提出的方法

  • 提出一种用于 log-sum-exp 的偏移算法:y = a + log(1 + sum(exp(x_i - a))),其中 a = max(x_i),以避免溢出和下溢。
  • 使用标准浮点数误差界,分析指数求和与最终 log1p 操作中的舍入误差。
  • 推导出计算得到的 log-sum-exp 值的相对误差界:|(y - ŷ)/y| ≤ |(y + n - x_min)/y| u + O(u²),其中 u 为单位舍入误差。
  • 对 softmax 函数应用类似分析,表明偏移版本通常比无除法替代方法更精确。
  • 利用条件数分析解释函数的敏感性,并为误差界提供上下文背景。
  • 采用泰勒级数展开和扰动分析,界定 log1p 和指数运算中的误差传播。

实验结果

研究问题

  • RQ1在浮点数运算中,将输入值偏移最大值对计算 log-sum-exp 函数的相对误差有何影响?
  • RQ2在低精度格式中,偏移 log-sum-exp 算法是否比非偏移或无除法变体更精确?
  • RQ3偏移 log-sum-exp 计算的理论误差界是什么?它与函数条件数有何关系?
  • RQ4在偏移算法中使用 log1p 是否能防止有害下溢,同时保持精度?
  • RQ5为何在实践中,偏移 softmax 函数比其无除法对应方法更精确?

主要发现

  • 偏移 log-sum-exp 算法的相对误差被限制在 |(y + n - x_min)/y| u + O(u²) 以内,该界与条件数和机器精度成正比。
  • 该偏移算法可避免溢出,并通过使下溢项在求和中可忽略来缓解有害下溢。
  • 数值实验表明,偏移算法通常比无除法 softmax 变体更精确。
  • 分析表明,偏移不会降低精度,反而通过减少大指数项的影响而提升精度。
  • 在最后一步使用 log1p 可确保即使 s 很小时,log(1 + s) 的计算也稳定。
  • 误差界是紧致的,能够解释为何该偏移方法在不同精度下(包括 bfloat16 和 fp16)均表现出鲁棒性。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。