LogSumExp (LSE,也称RealSoftMax [1] 或多变量softplus )函数 是一个平滑最大值 ——一个对极值 函数的光滑 近似 ,主要用在机器学习 算法中。[2] 其定义为参数的指数 的和的对数 :
L S E ( x 1 , … , x n ) = log ( exp ( x 1 ) + ⋯ + exp ( x n ) ) . {\displaystyle \mathrm {LSE} (x_{1},\dots ,x_{n})=\log \left(\exp(x_{1})+\cdots +\exp(x_{n})\right).} 性质 LogSumExp函数的定义域 为 R n {\displaystyle \mathbb {R} ^{n}} (实数空间 ),共域是 R {\displaystyle \mathbb {R} } (实数线 )。它是对极值函数 max i x i {\displaystyle \max _{i}x_{i}} 的近似,同时有如下的界限:
max { x 1 , … , x n } ≤ L S E ( x 1 , … , x n ) ≤ max { x 1 , … , x n } + log ( n ) . {\displaystyle \max {\{x_{1},\dots ,x_{n}\}}\leq \mathrm {LSE} (x_{1},\dots ,x_{n})\leq \max {\{x_{1},\dots ,x_{n}\}}+\log(n).} 第一个不等式 在 n = 1 {\displaystyle n=1} 以外的情况是严格成立的,第二个不等式仅在所有元素相等时取等号。(证明:令 m = max i x i {\displaystyle m=\max _{i}x_{i}} ,则 exp ( m ) ≤ ∑ i = 1 n exp ( x i ) ≤ n exp ( m ) {\displaystyle \exp(m)\leq \sum _{i=1}^{n}\exp(x_{i})\leq n\exp(m)} 。将不等式取对数即可。)
另外,我们可以将不等式缩放到更紧的界限。考虑函数 1 t L S E ( t x ) {\displaystyle {\frac {1}{t}}\mathrm {LSE} (tx)} 。然后,
max { x 1 , … , x n } < 1 t L S E ( t x ) ≤ max { x 1 , … , x n } + log ( n ) t {\displaystyle \max {\{x_{1},\dots ,x_{n}\}}<{\frac {1}{t}}\mathrm {LSE} (tx)\leq \max {\{x_{1},\dots ,x_{n}\}}+{\frac {\log(n)}{t}}} (证明:将上式 x i {\displaystyle x_{i}} 用 t > 0 {\displaystyle t>0} 的 t x i {\displaystyle tx_{i}} 替换,得到
max { t x 1 , … , t x n } < L S E ( t x 1 , … , t x n ) ≤ max { t x 1 , … , t x n } + log ( n ) {\displaystyle \max {\{tx_{1},\dots ,tx_{n}\}}<\mathrm {LSE} (tx_{1},\dots ,tx_{n})\leq \max {\{tx_{1},\dots ,tx_{n}\}}+\log(n)} 由于 t > 0 {\displaystyle t>0} ,
t max { x 1 , … , x n } < L S E ( t x 1 , … , t x n ) ≤ t max { x 1 , … , x n } + log ( n ) {\displaystyle t\max {\{x_{1},\dots ,x_{n}\}}<\mathrm {LSE} (tx_{1},\dots ,tx_{n})\leq t\max {\{x_{1},\dots ,x_{n}\}}+\log(n)} 最后,同除 t {\displaystyle t} 得到结果。)
此外,如果我们乘上一个负数,可以得到一个与 min {\displaystyle \min } 有关的不等式:
min { x 1 , … , x n } − log ( n ) t ≤ 1 − t L S E ( − t x ) < min { x 1 , … , x n } . {\displaystyle \min {\{x_{1},\dots ,x_{n}\}}-{\frac {\log(n)}{t}}\leq {\frac {1}{-t}}\mathrm {LSE} (-tx)<\min {\{x_{1},\dots ,x_{n}\}}.} LogSumExp函数是凸函数 ,因此在定义域上严格递增 。[3] (但并非处处都是严格凸的[4] 。)
令 x = ( x 1 , … , x n ) {\displaystyle \mathbf {x} =(x_{1},\dots ,x_{n})} ,偏导数 为:
∂ ∂ x i L S E ( x ) = exp x i ∑ j exp x j , {\displaystyle {\frac {\partial }{\partial x_{i}}}{\mathrm {LSE} (\mathbf {x} )}={\frac {\exp x_{i}}{\sum _{j}\exp {x_{j}}}},} 表明LogSumExp的梯度 是softmax函数 。
LogSumExp的凸共轭 是负熵 。
对数域中的log-sum-exp计算技巧 当通常的算术计算在对数尺度 上进行时,经常会遇到LSE函数,例如对数概率。[5]
类似于线性尺度中的乘法运算变成对数尺度中的简单加法,线性尺度中的加法运算变成对数尺度中的LSE:
L S E ( log ( x 1 ) , . . . , log ( x n ) ) = log ( x 1 + ⋯ + x n ) {\displaystyle \mathrm {LSE} (\log(x_{1}),...,\log(x_{n}))=\log(x_{1}+\dots +x_{n})} 使用对数域计算的一个常见目的是在使用有限精度浮点数直接表示(在线性域中)非常小或非常大的数字时提高精度并避免溢出问题.[6]
不幸的是,在一些情况下直接使用 LSE 依然会导致上溢/下溢问题,必须改用以下等效公式(尤其是当上述“最大”近似值的准确性不够时)。 因此,IT++等很多数学库都提供了LSE的默认例程,并在内部使用了这个公式。
L S E ( x 1 , … , x n ) = x ∗ + log ( exp ( x 1 − x ∗ ) + ⋯ + exp ( x n − x ∗ ) ) {\displaystyle \mathrm {LSE} (x_{1},\dots ,x_{n})=x^{*}+\log \left(\exp(x_{1}-x^{*})+\cdots +\exp(x_{n}-x^{*})\right)} 其中 x ∗ = max { x 1 , … , x n } {\displaystyle x^{*}=\max {\{x_{1},\dots ,x_{n}\}}}
一个严格凸的log-sum-exp型函数 LSE是凸的,但不是严格凸的。我们可以通过增加一项为零的额外参数来定义一个严格凸的log-sum-exp型函数[7] :
L S E 0 + ( x 1 , . . . , x n ) = L S E ( 0 , x 1 , . . . , x n ) {\displaystyle \mathrm {LSE} _{0}^{+}(x_{1},...,x_{n})=\mathrm {LSE} (0,x_{1},...,x_{n})} This function is a proper Bregman generator (strictly convex and differentiable). It is encountered in machine learning, for example, as the cumulant of the multinomial/binomial family.
在热带分析 中,这是对数半环 的和。
参见 参考资料