Quantities of the form log(i=1∑nexp(xi)) for x∈Rn, often referred to as “log-sum-exp” functions are ubiquitous in machine learning, as they appear in normalizing constants of exponential families, and thus in many supervised learning formulations such as softmax regression, but also more generally in (Bayesian or frequentist) probabilistic modelling. Moreover, as often used in optimization, they are a standard way of performing a soft maximum. In this context, the function defined as
fε(x)=εlog(i=1∑nexp(xi/ε)),
is such that ε→0+limfε(x)=max{x1,…,xn}.
Is it possible to go from the maximum back to the softmax fε(x) (for any strictly positive ε, and thus equivalently for ε=1) ? Beyond being a nice mathematical puzzle, this would be useful for very large n when the maximization problem could be easier than the summation problem (more on this below).
While this seems at first impossible, the Gumbel trick provides a surprising randomized solution. We consider adding some independent and identically distributed random variables γ1,…,γn, to form the vector x+γ∈Rn and takes its maximum component, that is,
This is achieved by the Gumbel distribution, hence the name “Gumbel trick” (the first applications within machine learning seem to be from [1] and [2], while this is a well-known fact in social sciences, see, e.g., [3]). We first make a detour through exponential distributions.
Minimum of independent exponential distributions
A random variable T is distributed according to the exponential distribution with parameter λ∈R, if it is supported on the positive real line and its density there is equal to t↦λexp(–λt). We have these classical properties:
ET=1/λ and var(T)=1/λ2.
T has the same distribution as −(1/λ)logU, where U has a uniform distribution on [0,1], leading to a simple algorithm to sample T.
A key property which is used heavily in point processes and queuing theory is characterizing the minimum and minimizer of independent exponentially distributed random variables.
Proposition. Given n independent variables Ti distributed according to exponential distributions with parameter λi, i=1,…,n, the minimum V=min{T1,…,Tn} is distributed according to an exponential distribution with parameter λ1+⋯+λn, and the minimizer i∗=argmini∈{1,…,n}Ti (breaking ties arbitrarily) is distributed according to a multinomial distribution of parameter π with πi=λ1+⋯+λnλi, for i∈{1,…,n}. Moreover, i∗ and V are independent.
(simple) Proof. A direct integration shows that
P(T≥u)=∫u+∞λexp(−λt)=exp(−λu).
We can then directly compute the probability, for a fixed i∈1,…,n and u∈R+:
which exactly shows the desired result (both independence and the claimed distributions).
From exponential to Gumbel distributions
A real random variable Γ is distributed according to a Gumbel distribution with parameter x∈R, if it is equal in distribution to Γ=−logT–C, where T is distributed according to an exponential distribution of parameter λ=exp(x), and C≈0.5772 is the Euler constant (note that in some conventions, C is not subtracted). We have the following properties:
var(Γ)=6π2 (proof left as an exercise, derivatives of the Gamma function could be handy).
Γ has the same distribution as x–C–log(–logU), where U has a uniform distribution on [0,1], leading to a simple algorithm to sample Γ.
The density of Γ is p(γ)=exp(x−γ−C–exp(x–γ–C)), as shown below for x=0.
The proposition above on exponential distributions leads to the following result.
Proposition. Given n independent variables Γi distributed according to Gumbel distributions with parameter xi, i=1,…,n, the maximum Z=max{Γ1,…,Γn} is distributed according to a Gumbel distribution with parameter (and thus mean) log(∑k=1nexp(xk)), the minimizer i∗=argmaxi∈1,…,nΓi (breaking ties arbitrarily) is distributed according to a multinomial distribution of parameter π with πi=exp(x1)+⋯+exp(xn)exp(xi), for i∈1,…,n. Moreover, i∗ and Z are independent.
Applications in machine learning
The Gumbel trick can be used in mainly two ways: (a) the main goal is to approximate the expectation using a finite average or using stochastic gradients (then only the value of the maximum is used), (b) the minimizer is used for sampling purposes. I will focus only on the first type of applications and refer to the very nice posts of Tim Vieira, Chris Maddison, and Laurent Dinh for the sampling aspects.
Log-partition functions
We consider d finite sets X1,…,Xd, and a probability mass function at x=(x1,…,xd) of the form:
p(x)=p(x1,…,xd)=exp(f(x)–A(f)),
where f:X1×⋯×Xd→R is a any function, and
A(f)=log(x1∈X1,…,xd∈Xd∑exp(f(x)))
is the so-called log-partition function (which is here to make the probability mass sum to one). The cardinality of X1×⋯×Xd is exponential in d and thus the sum defining A(f) cannot in general be computed even for moderate d. Since computing A(f) or its derivatives is a key step in many inference tasks, finding approximations is one of the standard computational problems in probabilistic modelling.
The Gumbel trick allows to go from maximization problems to “log-sum-exp” problems, and is only useful when the maximization problem is easy to solve while the “log-sum-exp” problem is not. Can this really happen? In other words…
Is the Gumbel trick useful?
There are many conditions under which combinatorial optimization problems can be solved in polynomial time, but two properties have emerged as crucial (at least in machine learning applications): (1) dynamic programming, and (2) linear programming. For simplicity, I will illustrate them for binary problems where Xi={0,1} for all i, and so-called “cut functions” f(x)=∑i,j=1dwi,jxixj+∑i=1dwixi, for arbitrary scalar weights wi,j, wi.
Dynamic programming: this corresponds to the situations where most of the weights wi,j are equal to zero, with the graph on the vertex set {1,…,n} with edge set equal to the pairs (i,j) with wi,j=0, being a tree, or a chain.
For example, consider f(x)=∑i=1d−1wi,i+1xixi+1+∑i=1dwixi. In this situation, one can compute sequentially the functions (with increasing index k)
which can be computed easily; the recursion leads to the desired value maxxd∈{0,1}fd(xd).
In order to obtain the identity above, we have simply used the property
max{A+B,C+B}=B+max{A,C}.
However, this property is also true for the softmax, as
log(exp(A+B)+exp(C+B))=B+log(exp(A)+exp(C)).
Thus, the same dynamic programming strategy works for softmax, and using the Gumbel trick is here useless. (here, we have described a particular instance of the sum-product and max-product algorithms for graphical models).
Linear programming: for the particular generic quadratic binary problem, we can write for any xi,xj∈{0,1}, xixj=min{xi,xj}, and, if all wi,j are non-positive (and here no constraint regarding having many zero values), then the function g:Rd→R defined as g(x)=∑i,j=1dwi,jmin{xi,xj}+∑i=1dwixi is concave and equal to f for x∈{0,1}d. The convex relaxation of computing maxx∈{0,1}df(x) is maxx∈[0,1]df(x), which is a concave maximization problem, which can be solved in polynomial time (it can be cast as a linear programming problem). For this particular problems, due to a property called submodularity, the convex relaxation happens to be tight. In these situations, maximizing is feasible while “log-sum-exp” is not. However, the Gumbel trick requires to perturb f(x) with a different random values for all2d assignments of (x1,…,xd), thus totally losing the structure above. Therefore, using the Gumbel trick is here also useless.
Thus it seems that for classical situations where maximization can be done in polynomial time, the Gumbel trick is useless, either because performing log-sum-exp is as simple (for dynamic programming), or because maximization is intractable once the Gumbel perturbations are added (for linear programming). Can we make it useful?
Separable Gumbel perturbations
This is where Tamir Hazan and Tommi Jaakkola [4] come in with the following very nice and important result, which is true beyond cut functions, and shows that with a separable random perturbation, we get an upper-bound of the log-partition function.
Proposition. Given d random functions γi:Xi↦R, i=1,…,d, such that each value (within and across the functions) is sampled independently distributed from a Gumbel distribution with zero mean, then
A(f)≤E[x1∈X1,…,xd∈Xdmaxf(x)+i=1∑dγi(xi)].
Proof. We can show the result by induction on d, by first conditioning on γd to get
For binary problems like for cut-functions, this corresponds to adding to f(x)=∑i,j=1dwi,jxixj+∑i=1dwixi, the term ∑i=1dxiγi(1)+∑i=1d(1−xi)γi(0), which is a linear function of x, and still leading to a tight convex relaxation. Here 2d Gumbel variables are used instead of 2d.
This seminal result led to a series of works using this result to approximate the log-partition function for parameter learning in intractable distributions where the maximization problem is efficient [5, 6].
Interestingly, the Gumbel trick is a simple consequence of a special property of a specific exponential family (here the exponential distribution). Some other exponential families (e.g., Gamma or Dirichlet) lead to different special properties with other nice consequences, but this is for future posts.