Post · 4 min read· pp. 684–686

What Is the Log-Sum-Exp Function?

The log-sum-exp function takes as input a real nn-vector xx and returns the scalar

lse(x)=log⁡∑i=1nexi,\notag \mathrm{lse}(x) = \log \displaystyle\sum_{i=1}^n \mathrm{e}^{x_i},

where log⁡\log is the natural logarithm. It provides an approximation to the largest element of xx, which is given by the max⁡\max function, max⁡(x)=max⁡ixi\max(x) = \max_i x_i. Indeed,

emax⁡(x)≤∑i=1nexi≤n emax⁡(x),\notag \mathrm{e}^{\max(x)} \le \displaystyle\sum_{i=1}^n \mathrm{e}^{x_i} \le n \mskip1mu \mathrm{e}^{\max(x)},

and on taking logs we obtain

max⁡(x)≤lse(x)≤max⁡(x)+log⁡n.(∗)\notag \qquad\qquad \max(x) \le \mathrm{lse}(x) \le \max(x) + \log n. \qquad\qquad (*)

The log-sum-exp function can be thought of as a smoothed version of the max function, because whereas the max function is not differentiable at points where the maximum is achieved in two different components, the log-sum-exp function is infinitely differentiable everywhere. The following plots of lse(x)\mathrm{lse}(x) and max⁡(x)\max(x) for n=2n = 2 show this connection.

lse2d.jpg

The log-sum-exp function appears in a variety of settings, including statistics, optimization, and machine learning.

For the special case where x=[0 t]Tx = [0~t]^T, we obtain the function f(t)=log⁡(1+et)f(t) = \log(1+\mathrm{e}^t), which is known as the softplus function in machine learning. The softplus function approximates the ReLU (rectified linear unit) activation function max⁡(t,0)\max(t,0) and satisfies, by (∗)(*),

max⁡(t,0)≤f(t)≤max⁡(t,0)+log⁡2.\notag \max(t,0) \le f(t) \le \max(t,0) + \log 2.

Two points are worth noting.

  • While log⁡(x1+x2)≠log⁡x1+log⁡x2\log(x_1 + x_2) \ne \log x_1 + \log x_2, in general, we do (trivially) have log⁡(x1+x2)=lse(log⁡x1,log⁡x2)\log(x_1 + x_2) = \mathrm{lse}(\log x_1,\log x_2), and more generally log⁡(x1+x2+⋯+xn)=lse(log⁡x1,log⁡x2,…,log⁡xn)\log(x_1 + x_2 + \cdots + x_n) = \mathrm{lse}(\log x_1,\log x_2,\dots,\log x_n).
  • The log-sum-exp function is not to be confused with the exp-sum-log function: exp⁡∑i=1nlog⁡xi=x1x2…xn\exp \sum_{i=1}^n \log x_i = x_1x_2\dots x_n.

Here are some examples:

text
>> format long e
>> logsumexp([1 2 3])
ans =
     3.407605964444380e+00

>> logsumexp([1 2 30])
ans =
     3.000000000000095e+01

>> logsumexp([1 2 -3])
ans =
     2.318175429247454e+00

The MATLAB function logsumexp used here is available at https://github.com/higham/logsumexp-softmax.

Straightforward evaluation of log-sum-exp from its definition is not recommended, because of the possibility of overflow. Indeed, exp⁡(x)\exp(x) overflows for x=12x = 12, x=89x = 89, and x=710x = 710 in IEEE half, single, and double precision arithmetic, respectively. Overflow can be avoided by writing

lse(x)=log⁡∑i=1nexi=log⁡∑i=1neaexi−a=log⁡(ea∑i=1nexi−a),\notag \begin{aligned} \mathrm{lse}(x) &= \log \sum_{i=1}^n \mathrm{e}^{x_i} = \log \sum_{i=1}^n \mathrm{e}^a \mathrm{e}^{x_i - a} = \log \left(\mathrm{e}^a\sum_{i=1}^n \mathrm{e}^{x_i - a}\right), \end{aligned}

which gives

lse(x)=a+log⁡∑i=1nexi−a.\notag \mathrm{lse}(x) = a + \log\displaystyle\sum_{i=1}^n \mathrm{e}^{x_i - a}.

We take a=max⁡(x)a = \max(x), so that all exponentiations are of nonpositive numbers and therefore overflow is avoided. Any underflows are harmless. A refinement is to write

lse(x)=max⁡(x)+log1p(∑i=1i≠knexi−max⁡(x)),(#)\notag \qquad\qquad \mathrm{lse}(x) = \max(x) + \mathrm{log1p}\Biggl( \displaystyle\sum_{i=1 \atop i\ne k}^n \mathrm{e}^{x_i - \max(x)}\Biggr), \qquad\qquad (\#)

where xk=max⁡(x)x_k = \max(x) (if there is more than one such kk, we can take any of them). Here, log1p(x)=log⁡(1+x)\mathrm{log1p}(x) = \log(1+x) is a function provided in MATLAB and various other languages that accurately evaluates log⁡(1+x)\log(1+x) even when xx is small, in which case 1+x1+x would suffer a loss of precision if it was explicitly computed.

Whereas the original formula involves the logarithm of a sum of nonnegative quantities, when max⁡(x)<0\max(x) < 0 the shifted formula (#)(\#) computes lse(x)\mathrm{lse}(x) as the sum of two terms of opposite sign, so could potentially suffer from numerical cancellation. It can be shown by rounding error analysis, however, that computing log-sum-exp via (#)(\#) is numerically reliable.

References

This is a minimal set of references, which contain further useful references within.

This article is part of the “What Is” series, available from https://nhigham.com/category/what-is and in PDF form from the GitHub repository https://github.com/higham/what-is.