In this chapter, we review smoothing techniques based on convolution.
14.1 Convolution
14.1.1 Convolution operators
The convolution between two functions f and g produces another function, denoted f∗g. It is defined by (f∗g)(μ):=∫−∞∞f(u)g(μ−u)du,(14.1) assuming that the integral is well-defined. It is therefore the integral of the product of f and g after g is reflected about the y-axis and shifted. It can be seen as a generalization of the moving average. Using the change of variable u:=μ+z, we can also write (f∗g)(μ)=∫−∞∞f(μ+z)g(−z)dz=∫∞−∞f(μ−v)g(v)(−dv)=∫−∞∞g(v)f(μ−v)dv=(g∗f)(μ).(14.2) The convolution operator is therefore commutative.
Remark 14.1(Change of variables)
Readers with a background in signal processing might find the change of variables u:=μ+z slightly unconventional for proving the commutativity of the convolution, as u:=μ−z more directly proves commutativity. However, we intentionally use u:=μ+z here, as this perfectly aligns our notation with the location-scale transformU:=μ+σZ from Section 12.4.1. It allows us to seamlessly interpret smoothing by convolution as the injection of additive noise.
14.1.2 Convolution with a kernel
The convolution is frequently used together with a kernelκ to create a smooth approximation f∗κ of f. The most frequently used kernel is the Gaussian kernel with width σ, defined by κσ(z):=2πσ1e−21(σz)2. This is the probability density function (PDF) of the normal distribution with zero mean and variance σ2. The term 2πσ1 is a normalization constant, ensuring that the kernel sums to 1 for all σ. We therefore say that κσ is a normalized kernel.
Averaging perspective
Applying the definition of the convolution in Eq. (14.1), we obtain (f∗κσ)(μ):=2πσ1∫−∞∞f(u)e−21(σμ−u)2du=EU∼pμ,σ[f(U)], where pμ,σ(u):=κσ(μ−u)=2πσ1e−21(σμ−u)2 is the PDF of the Gaussian distribution with mean μ and variance σ2. Therefore, we can see f∗κσ as the expectation of f(u) over a Gaussian centered aroundμ. This property is true for all translation-invariant kernels, that correspond to a location-scale family distribution (e.g., the Laplace distribution). The convolution therefore performs an averaging with all points, with points nearby μ given more weight by the distribution. The parameter σ controls the importance we want to give to farther points. We call this viewpoint averaging, as we replace f(u) by E[f(U)].
Perturbation perspective
Conversely, using the alternative definition of the convolution operator in Eq. (14.2), which stems from the commutativity of the convolution, we have (f∗κσ)(μ):=∫−∞∞f(μ+z)e−21(σ−z)2dz=∫−∞∞f(μ+z)e−21(σz)2dz=EZ∼p0,σ[f(μ+Z)], where, in the second line, we used that the Gaussian kernel p0,σ is sign invariant, i.e., p0,σ(−z)=p0,σ(z). This viewpoint shows that smoothing by convolution with a Gaussian kernel can also be seen as injecting Gaussian noise or perturbations to the function’s input.
Limit case
When σ→0, the kernel κσ converges to a Dirac delta function, σ→0limκσ(z)=δ(z). Since the Dirac delta is the multiplicative identity of the convolution algebra (this is also known as the sifting property), when σ→0, f∗κσ converges to f, i.e., σ→0lim(f∗κσ)(u)=f(u).
14.1.3 Discrete convolution
Many times, we work with functions whose convolution does not have an analytical form. In these cases, we can use a discrete convolution on a grid of values. For two functions f and g defined over Z, the discrete convolution is defined by (f∗g)[i]:=j=−∞∑∞f[j]g[i−j]. As for its continuous counterpart, the discrete convolution is commutative, namely, (f∗g)[i]=j=−∞∑∞f[i−j]g[j]=(g∗f)[i]. When g has finite support over the set S:={−M,−M+1,…,0,…,M−1,M}, meaning that g[i]=0 for all i∈S, a finite summation may be used instead, i.e., (f∗g)[i]=j=−M∑Mf[i−j]g[j]=(g∗f)[i].
In practice, convolution between a discrete signal f:Z→R and a continuous kernel κ:R→R is implemented by discretizing the kernel. One of the simplest approaches consists in sampling points on an interval, evaluating the kernel at these points and renormalizing the obtained values, so that the sampled kernel sums to 1. This is illustrated with the Gaussian kernel in Fig. 14.1. Since the Gaussian kernel decays exponentially fast, we can choose a small interval around 0. For a survey of other possible discretizations of the Gaussian kernel, see Getreuer (2013).
Figure 14.1: Smoothing of the signal f[t]:=t2+0.3sin(6πt) with a sampled and renormalized Gaussian kernel.
14.1.4 Differentiation
Remarkably, provided that the two functions are integrable with integrable derivatives, the derivative of the convolution satisfies (f∗g)′=(f′∗g)=(f∗g′), which simply stems from switching derivative and integral in the definition of the convolution. Moreover, we have the following proposition.
Proposition 14.1(Differentiability of the convolution)
If g is n-times differentiable with compact support over R and f is locally integrable over R, then f∗g is n-times differentiable over R.
14.1.5 Multidimensional convolution
So far, we studied the convolution of one-dimensional functions. The definition can be naturally extended to multidimensional functions f:RM→R and g:RM→R as (f∗g)(μ):=∫RMf(u)g(μ−u)du, assuming again that the integral exists. Typically, a Gaussian kernel with diagonal covariance matrix is used κσ(z):=j=1∏M2πσj1e−21(σjzj)2=2πMσM1e−21σ2∥z∥22,(14.3) where, in the second equality, we assumed σ1=⋯=σM. In an image processing context, where M=2, it is approximated using a discrete convolution and it is called a Gaussian blur.
14.1.6 Link between convolution and infimal convolution
The infimal convolution we studied in Section 13.1 takes the form (f□g)(μ):=u∈RMinff(u)+g(μ−u). In comparison, the classical convolution takes the form (F∗G)(μ):=∫RMF(u)G(μ−u)du. The two forms of convolution are clearly related. Infimal convolution performs an infimum and uses the sum of f and g: it uses a min-plus algebra. Classical convolution performs an integral and uses the product of F and G: it uses a sum-product algebra.
14.1.7 The soft infimal convolution
The link between the infimal convolution and the classical convolution can be further elucidated if we replace the infimum with a soft minimum in the definition of the infimal convolution.
Definition 14.1(Soft infimal convolution)
The soft infimal convolution between f:RM→R and g:RM→R is (f□εg)(μ):=u∈RMsoftminεf(u)+g(μ−u), where we defined the soft minimum (assuming that it exists) over S of any function h:S→R as u∈Ssoftminεh(u):=−εlog∫Sexp(−h(u)/ε)du.
We recover the infimal convolution as ε→0.
Computation using a convolution
We now show that we can rewrite the soft infimal convolution using a classical convolution. Indeed, by using the exponential change of variable (sometimes referred to as Cole-Hopf transformation in a partial differential equation context) Cε{f}(u)Cε−1{F}(v):=exp(−f(u)/ε)=−εlogF(v), we can define each function in the exponential domain, FεGεHε:=Cε{f}:=Cε{g}:=Cε{hε}. It is easy to check that we then have Hε(μ)=(Fε∗Gε)(μ). Back to log domain, we obtain hε(μ)=Cε−1{Hε}(μ). Combining the transformation and its inverse, we can write hε(μ)=Cε−1{Cε{f}∗Cε{g}}(μ). What we have shown is that, after an exponential change of variable, the soft infimal convolution can be reduced to the computation of a convolution. This is useful as a discrete convolution on a grid of size n can be computed in O(nlogn).
14.1.8 The soft Moreau envelope
We saw in Section 13.1.2 that the infimal convolution between f and R(z)=21z2 is the Moreau envelope, Mf(μ):=(f□R)(μ)=u∈RMinff(u)+21∥μ−u∥22. Replacing the infimal convolution with a soft infimal convolution, we can define the “soft” Moreau envelope, Mfε(μ):=(f□εR)(μ)=u∈RMsoftminεf(u)+21∥μ−u∥22. We emphasize that this operation is not the same as the convolution of f with a Gaussian kernel. Indeed, we have Mfε(μ)=−εlog∫RMexp((−f(u)−21∥μ−u∥22)/ε)du. while (f∗κσ)(μ):=∫RMf(u)κσ(μ−u)du, where κσ is for instance defined in Eq. (14.3).
We saw that the Moreau envelope is a smooth function. One may therefore ask what do we gain from using a soft Moreau envelope. The benefit can be computational, as the latter can be approximated using a discrete convolution.
14.2 Fourier and Laplace transforms
Let us define the Fourier transform of f by F(s):=F{f}(s):=∫−∞∞f(t)e−i2πstdt,s∈R. Note that F{f} is a function transformation: it transforms f into another function F.
14.2.1 Convolution theorem
Now, consider the convolution h(t):=(f∗g)(t). If we define the three transformations F:=F{f},G:=F{g},H:=F{h}, the convolution theorem states that H(s)=F(s)⋅G(s),s∈R. Written differently, we have F{f∗g}=F{f}⋅F{g}. In words, in the Fourier domain, the convolution operation becomes a multiplication. Conversely, h(t)=(f∗g)(t)=F−1{F⋅G}(t),t∈R. The convolution theorem also holds if we replace the Fourier transform with the Laplace transform or with the two-sided (bilateral) Laplace transform.
14.2.2 Link between Fourier and Legendre transforms
In Section 13.2, we studied another function transformation: the convex conjugate, also known as Legendre-Fenchel transform. We recap the analogies between these transforms in Table 14.1. In particular, the counterpart of F{f∗g}=F{f}⋅F{g} for the infimal convolution is (f□g)∗=f∗+g∗. In words, the Legendre-Fenchel transform is to the infimal convolution what the Fourier transform is to the convolution.
Table 14.1: Analogy between Fourier and Legendre transforms. See Proposition 13.3 for more conjugate calculus rules.
Fourier F{f}
Legendre f∗
Semiring
(+,⋅)
(min,+)
Scaling (a>0)
f(t)=g(t/a)
f(t)=ag(t/a)
F{f}(s)=aF{g}(as)
f∗(s)=ag∗(s)
Translation
f(t)=g(t−t0)
f(t)=g(t−t0)
F{f}(s)=e−i2πt0sF{g}(s)
f∗(s)=g∗(s)+t0
Convolution
h=f∗g
h=f□g
F{h}=F{f}⋅F{g}
h∗=f∗+g∗
Gaussian / quadratic
f(t)=e−at2
f(t)=2at2
F{f}(s)=aπe−π2s2/a
f∗(s)=2a1s2
Smoothing
f∗κσ
f□2ε1∥⋅∥22
14.2.3 The soft Legendre-Fenchel transform
We saw in Section 13.2 that the Legendre-Fenchel transform (convex conjugate) of a function f:RM→R is f∗(v):=u∈RMmax⟨u,v⟩−f(u). If necessary, we can support constraints by including an indicator function in the definition of f. The conjugate can be smoothed out using a log-sum-exp, which plays the role of a soft maximum (Section 13.5).
Definition 14.2(Soft convex conjugate)
fε∗(v):=u∈RMsoftmaxε⟨u,v⟩−f(u), where we defined the soft maximum (assuming that it exists) over S of any function g:S→R as u∈Ssoftmaxεg(u):=εlog∫Sexp(g(u)/ε)du.
In the limit ε→0, we recover the convex conjugate.
Computation using a convolution
We now show that this smoothed conjugate can be rewritten using a convolution if we apply a bijective transformation to f.
Proposition 14.2(Smoothed convex conjugate as convolution)
The smoothed conjugate can be rewritten as fε∗(v)=Qε−1{Qε{f}∗Gε1}(v) where GεQε{f}Qε−1{F}:=Cε{21∥⋅∥22}=exp(−21ε∥⋅∥22):=Cε{f(⋅)−21∥⋅∥22}=exp(2ε1∥⋅∥22−ε1f(⋅)):=21∥⋅∥22−εlog(F(⋅)).
This insight was tweeted by Gabriel Peyré in April 2020.
Figure 14.2: Applying the smoothed conjugate twice gives a smoothed biconjugate (convex envelope) of the function.
What did we gain from this viewpoint? The convex conjugate can often be difficult to compute in closed form. If we replace RM with a discrete set S (i.e., a grid), we can then approximate the smoothed convex conjugate in O(nlogn), where n=∣S∣, using a discrete convolution, (Qε{f}∗Gε)(v)≈u∈S∑Gε(v−u)Qε{f}(u)=Kq, where K is the n×n Gaussian kernel matrix whose entries correspond to exp(−2ε1∥u−u′∥22) for u,u′∈S and q is the n-dimensional vector whose entries correspond to exp(ε1(21∥v∥22−f(u)) for u∈S. This provides a GPU-friendly alternative to the fast Legendre transform algorithm, discussed in Section 13.2. Of course, due to the curse of dimensionality, the technique is limited to functions defined on low-dimensional sets. We illustrate in Fig. 14.2 the application of the technique to computing an approximate biconjugate (convex envelope) of a function.
Remark 14.2(Link with the two-sided Laplace transform)
For
one-dimensional functions, instead of using a convolution, we can also write the soft convex conjugate as fε∗(v)=εlog∫−∞∞exp(ε1[uv−f(u)]))du=εlogB{e−εf}(−εv)=−Cε−1{B{Cε{f}}}(−εv) where we defined the two-sided (bilateral) Laplace transform B{g}(v):=∫−∞∞e−uvg(u)du and where we assumed that the integral exists.
14.3 Examples
In this section, we review practical examples for which the convolution with a Gaussian kernel enjoys an analytical solution.
14.3.1 Smoothed step function
Example 14.1(Smoothed Heaviside)
The Heaviside step function is defined by step(u):=h(u):={10 if u≥0 otherwise. With the Gaussian kernel, we therefore obtain (h∗κσ)(μ)=∫−∞μκσ(z)h(μ−z)dz+∫μ∞κσ(z)h(μ−z)dz=∫−∞μκσ(z)dz=Φσ(μ)=21[1+erf(2σμ)], where Φσ(μ) is the CDF of the Gaussian distribution with zero mean and variance σ2, and where we used the error function erf(z):=π2∫0ze−t2dt, that we both already encountered in Chapter 3. Although there is no closed form for the error function, it is commonly available in numerical analysis software, such as SciPy.
14.3.2 Smoothed ReLU function
Example 14.2(Smoothed ReLU)
The ReLU is defined by r(u):={u0 if u≥0 otherwise=u⋅h(u). Similarly to the previous example, we obtain (r∗κσ)(μ)=∫−∞μκσ(z)r(μ−z)dz=∫−∞μκσ(z)(μ−z)dz=μ∫−∞μκσ(z)dz−∫−∞μκσ(z)zdz=μΦσ(μ)+σ2κσ(μ). In the second integral, setting a:=2σ21, we used ∫ze−az2dz=−2a1∫etdt=−2a1et+C=−2a1e−az2+C and t:=−az2⇒zdz=−2a1dt.
Figure 14.3: Smoothing of the ReLU and Heaviside functions by convolution with a Gaussian kernel, for three values of the width σ.
To illustrate differentiation of the convolution, we show how to differentiate the smoothed ReLU.
Example 14.3(Differentiating the smoothed ReLU)
Differentiating the smoothed ReLU from Example 14.2, we obtain (r∗κσ)′=(r′∗κσ)=h∗κσ=Φσ. Therefore, unsurprisingly, the derivative of the smoothed ReLU is the smoothed Heaviside step function. Differentiating once again, we obtain, (r∗κσ)′′=(h∗κσ)′=(h′∗κσ)=δ∗κσ=κσ, where the derivative h′ is well-defined almost everywhere. We can arrive at the same result by using that h∗κσ=Φσ and Φσ′=κσ, since Φσ and κσ are the CDF and PDF of the Gaussian with zero mean and σ2 variance.
14.4 Perturbation of blackbox functions
In this section, we review how to approximately compute a convolution with a kernel and its gradient using Monte-Carlo estimation.
14.4.1 Expectation in a location-scale family
A rather intuitive approach to smooth a function f:RM→R is to average its values on an input μ, perturbed by some additive noise Z∼p, for some noise distribution p. This defines the surrogate fσ(μ):=EZ∼p[f(μ+σZ)]. The parameter σ controls the perturbation strength: as σ→0, we naturally recover f. An equivalent viewpoint is obtained by defining the transformation (change of variables) U:=μ+σZ. We then have U∼pμ,σ, where pμ,σ is the location-family distribution generated by the noise distribution p. It is the pushforward distribution of Z through the transformation (see Section 12.4.4). In this notation, the initial noise distribution p is then simply p=p0,1. The perturbed function can then be expressed from these two perspectives as fσ(μ)=EZ∼p0,1[f(μ+σ⋅Z)]=EU∼pμ,σ[f(U)].(14.4) Writing the expectation as the integral of a p.d.f, we naturally recover the smoothing by convolution presented earlier, fσ(μ)=∫f(μ+σz)p0,1(z)dz=f∗κσ(μ), where we defined the kernel κσ(z):=p0,σ(−z). In the sequel, we assume that the noise distribution decomposes as p0,1(z):=exp(−ν(z))/C, where ν(z) is the log-density of the noise distribution and C is a normalization constant. For instance, the Gaussian distribution with diagonal covariance matrix and the corresponding Gaussian kernel are obtained with ν(z)=21∥z∥22 and C=2πM.
Approximation by Monte-Carlo estimation
Instead of approximating the integral above (continuous convolution) with a discrete convolution on a grid, as we did in Section 14.1.3, the expectation perspective suggests that we can estimate fσ(μ) by Monte-Carlo estimation: we simply draw samples from the distribution, evaluate the function at these samples and average. Beyond mere Monte-Carlo estimation, more elaborate approximation schemes are studied in (Chaudhuri and Solar-Lezama, 2010).
14.4.2 Gradient estimation by reparametrization
Provided that the conditions for swapping differentiation and integration hold (see Section 12.1), we have ∇fσ(μ)=EZ∼p0,1[∇f(μ+σ⋅Z)].(14.5) Note that if f is only differentiable almost everywhere, the formula may still hold. For example, if f is the ReLU, then ∇f is the Heaviside step function, and we obtain the correct gradient of fσ using the formula above; see Example 14.1. However, if f is not absolutely continuous, the formula may not hold. For example, if f is the Heaviside function, the right-hand side of (14.5) is 0 which does not match the gradient of fσ; see again Example 14.1.
From the second expression of fσ in (14.4), we can see the formula of the gradient in (14.5) as a reparametrization trick U=μ+σZ; see Section 12.4. Namely, we have ∇fσ(μ)=∇μEU∼pμ,σ[f(U)]=∇μEZ∼p0,1[f(μ+σ⋅Z)]=EZ∼p0,1[∇μf(μ+σ⋅Z)]=EZ∼p0,1[∇f(μ+σ⋅Z)].(14.6)
14.4.3 Gradient estimation by SFE, Stein’s lemma
In some cases, we may not have access to ∇f or f may not be absolutely continuous and therefore the formula in (14.5) cannot apply. For these cases, we can use the score function estimator (SFE) from Section 12.3. Here, for fσ(μ)=EU∼pμ,σ[f(U)], we obtain ∇fσ(μ)=EU∼pμ,σ[f(U)∇μlogpμ,σ(U)]. Since the PDF can be written as pμ,σ(u)=σ1p0,1((u−μ)/σ), where p0,1(z):=exp(−ν(z))/C, we obtain ∇μlogpμ,σ(u)=∇ν((u−μ)/σ)/σ. To summarize, we have shown that ∇fσ(μ)=EU∼pμ,σ[f(U)∇ν((U−μ)/σ)/σ]=EZ∼p0,1[f(μ+σ⋅Z)∇ν(Z)/σ],(14.7) where we used the change of variable Z=(U−μ)/σ. The same technique can also be used if we want to estimate the gradient w.r.t. θ=(μ,σ) or if we want to estimate the Jacobian of the expectation of a vector-valued function.
In the particular case of Gaussian noise, since ∇ν(z)=z, we obtain ∇fσ(μ)=EZ∼p0,1[f(μ+σ⋅Z)Z/σ]. This is known as Stein’s lemma. It should be noted that the above is an unbiased estimator of the gradient of the smoothed function fσ, but a biased estimator of the gradient of the original function f (assuming that it exists). However, smoothing is usually a good thing, as it can accelerate the convergence of gradient-based algorithms. Computing the gradient of perturbed general programs is studied in detail in (Kreikemeyer and Andelfinger, 2023).
14.4.4 Link between reparametrization and SFE
Using the log-derivative identity, we have for any distribution with differentiable density pEZ∼p[h(Z)∇logp(Z)]=∫RMh(z)(p(z)∇p(z))p(z)dz=∫RMh(z)∇p(z)dz. Using integration by parts and assuming that h(z)p(z) goes to zero when ∥z∥→∞, we have ∫RMh(z)∇p(z)dz=−∫RMp(z)∇h(z)dz. We have therefore the identity EZ∼p[h(Z)∇logp(Z)]=−EZ∼p[∇h(Z)]. Importantly, contrary to the SFE estimator from Section 12.3, this identity uses gradients with respect to z, not with respect to the parameters of the distribution. Nevertheless, using the reparametrization h(z):=f(μ+σ⋅z), we have ∇h(z)=∇f(μ+σ⋅z)⋅σ so that ∇fσ(μ)=∇μEU∼pμ,σ[f(U)]=EZ∼p0,1[∇f(μ+σ⋅Z)](reparametrization trick)=EZ∼p0,1[∇h(Z)/σ]=−EZ∼p0,1[h(Z)∇logp(Z)/σ]=EZ∼p0,1[h(Z)∇ν(Z)/σ]=EZ∼p0,1[f(μ+σ⋅Z)∇ν(Z)/σ](score function estimator) Essentially, integration by parts allowed us to convert the reparametrization trick estimator into the SFE estimator. For more applications of integration by parts in machine learning, see Francis Bach’s excellent blog post.
14.4.5 Variance reduction and evolution strategies
As discussed in Chapter 12, the SFE suffers from high variance. We now apply variance reduction techniques to it. To do so, we assume that ∇ν(Z) has zero mean for Z∼p0,1. This assumption for example holds for Gaussian noise. This assumption implies that EZ∼p0,1[f(μ)∇ν(Z)/σ]=f(μ)EZ∼p0,1[∇ν(Z)/σ]=0 and therefore ∇fσ(μ)=EZ∼p0,1[(f(μ+σ⋅Z)−f(μ))∇ν(Z)/σ].(14.8) This is an example of control variate discussed in Section 12.3. This can be interpreted as using a finite difference for computing a directional derivative in the random directionZ (see “limit case” below). Inspired by a central finite difference, we can also use ∇fσ(μ)=EZ∼p0,1[(f(μ+σ⋅Z)−f(μ−σ⋅Z))∇ν(Z)/(2σ)].(14.9) These estimators have been used as part of blackbox (zero-order) optimization algorithms, such as evolution strategies(Salimans et al., 2017) or random gradient-free optimization(Nesterov and Spokoiny, 2017). For quadratic functions, it is easy to show that the second estimator achieves lower variance (Recht and Frostig, 2017). The idea of sampling both Z and −Z simultaneously is called antithetic (Geweke, 1988) or mirrored sampling (Brockhoff et al., 2010). Evolution strategies have also been used to obtain unbiased gradient estimators of partially unrolled computational graphs (Vicol et al., 2021). We empirically compare the SFE with or without variance reduction for blackbox gradient estimation in Fig. 14.4.
Figure 14.4: Comparison of the score function estimator (SFE) with or without variance reduction for blackbox gradient estimation. We show the error ∣∇f(μ)−∇fσ(μ)∣ for f(u):=u3 and fσ(μ):=E[f(μ+σZ)], where Z∼Normal(0,1) and σ:=0.1. To estimate ∇fσ(μ), we compare three estimators: the vanilla SFE Eq. (14.7), the SFE estimator with forward difference (variance reduced) Eq. (14.8), and the SFE estimator with central difference (variance reduced) Eq. (14.9). In all three cases, we approximate the expectation by Monte-Carlo estimation using some number of samples. The variance-reduced estimators not only achieve smaller error, they are also more numerically stable as σ gets smaller.
14.4.6 Zero-temperature limit
We now discuss the limit case σ→0. That is, we assume that we do not want to perform smoothing and that ∇f exists. We recall that the directional derivative of f at μ in the direction z is ∂f(μ)[z]=⟨∇f(μ),z⟩=σ→0lim[f(μ+σ⋅z)−f(μ)]/σ. When σ→0 and Z follows the standard Gaussian distribution, meaning that ∇ν(z)=z, Eq. (14.8) therefore becomes ∇fσ(μ)=EZ∼p0,1[∂f(μ)[Z]∇ν(Z)]=EZ∼p0,1[∂f(μ)[Z]Z]=EZ∼p0,1[⟨∇f(μ),Z⟩Z]=EZ∼p0,1[ZZ⊤∇f(μ)]=∇f(μ). This should not be too surprising, as we already know from the convolution perspective that fσ(μ)=(f∗κσ)(μ)→f(μ) when σ→0. This recovers the randomized forward-mode estimator already presented in Section 8.7.
14.5 Gumbel tricks
14.5.1 The Gumbel distribution
The Gumbel distribution is a distribution frequently used in extreme value theory. As illustrated in Fig. 14.5, we consider the shifted standard Gumbel distribution, whose PDF is defined by p(z):=exp(−ν(z)), where ν(z):=z+γ+exp(−(z+γ)), and where γ≈0.577 is Euler’s constant. Note that in some formulations, the distribution is not shifted, i.e., γ is not added. If Z is distributed according to the shifted standard Gumbel distribution, we write Z∼Gumbel(0,1).
To obtain a multivariate extension with location-scale parameters μ and σ, we take M independent random variables Z:=(Z1,…,ZM) and apply the location-scale transform (Section 12.4.1). That is, U∼Gumbel(μ,σ)⟺U=μ+σZ,Zi∼Gumbel(0,1). As we used shifted standard Gumbel distributions, we naturally get that E[U]=μ and Var(U)=σ2. We can use Gumbel noise as an alternative to the Gaussian noise used in Section 14.4. Thankfully, in particular cases, we can compute closed-form expressions of the expectation of perturbed functions.
Remark 14.3(Link between Gumbel and exponential distribution)
A random variable Z is distributed as Gumbel(μ,1) if and only if exp(−Z) is distributed as an exponential distribution Exp(exp(μ−γ)). To see this, one can simply compute the CDF of exp(−Z) and recognize the CDF of Exp(exp(μ−γ)). Therefore, when comparing Gumbel distributions, we can use standard properties of the exponential distribution.
Remark 14.4(Sampling Gumbel noise)
If U∼Uniform(0,1), then Z:=−log(−log(U))−γ satisfies Z∼Gumbel(0,1), where we recall that we use Gumbel(0,1) to denote the shifted standard Gumbel distribution. To see this, note that P(Z≤t)=P(U≤exp(exp(−(γ+t))))=exp(exp(−(γ+t))), where the last expression matches the CDF of Gumbel(0,1).
Figure 14.5: We use a shifted definition of the standard Gumbel distribution so that the mean is achieved at z=0, and the mode at z=−γ. With an unshifted definition, the mean and the mode would be achieved at γ and 0, respectively.
14.5.2 Perturbed comparison
To start with, the Gumbel distribution can be used to smooth a binary comparison like the greater than or equal operators. Recall that the latter is defined for any μ1,μ2∈R as gt(μ1,μ2):={10ifμ1≥μ2ifμ1<μ2=step(μ1−μ2), where step is the Heaviside function. As shown below, by perturbing each variable with Gumbel noise, we recover logistic(a−b)=1/(1+e−(a−b)) as an approximation of step(a−b).
Proposition 14.3(Gumbel trick for binary variables)
Let Z1,Z2∼Gumbel(0,1) be two independent random variables. The difference of their location-scale transform (Section 12.4.1) is distributed according to a logistic distribution (Remark 3.1), i.e., μ1+σZ1−(μ2+σZ2)∼Logistic(μ1−μ2,σ), for μ1,μ2∈R and σ>0. In particular, we have EZ1,Z2[gt(μ1+σZ1,μ2+σZ2)]=1+e−(μ1−μ2)/σ1.
Proof
We first derive the CDF of μ1+σZ1−(μ2+σZ2) as P(μ1+σZ1−(μ2+σZ2)≤t)=P(μ1/σ+Z1≤(μ2+t)/σ+Z2)=P(e−(μ1/σ+Z1)≥e−((μ2+t)/σ+Z2)). By Remark 14.3, e−(μ1/σ+Z1)∼Exp(exp(μ1/σ−γ)), and similarly for e−(μ2+t)/σ+Z2. Now one easily shows that if U∼Exp(u),V∼Exp(v) independent, then P(U≤V)=u/(u+v). Hence, we get P(μ1+σZ1−(μ2+σZ2)≤t)=e(μ2+t)/σ−γ+eμ1/σ−γe(μ2+t)/σ−γ=1+e−(t−(μ1−μ2))/σ1. We recognize the CDF of the logistic distribution with mean μ1−μ2 and scale σ, denoted Logistic(μ1−μ2,σ). For the last claim, we simply have that E[gt(μ1+σZ1,μ2+σZ2)]=E[step(μ1+σZ1−(μ2+σZ2))]=P(μ1+σZ1−(μ2+σZ2)≥0)=1+e−(μ1−μ2)/σ1. ◻
14.5.3 Perturbed argmax
Suppose we want to smooth y(u):=y∈{e1,…,eM}argmax⟨y,u⟩=ϕ(i(u)), where i(u)ϕ(i):=i∈[M]argmaxui:=ei where ϕ(i) is the one-hot encoding of i∈[M]. It turns out that the function y(u) perturbed using Gumbel noise enjoys a closed form expectation, which is nothing else than the softargmax.
Proposition 14.4(Gumbel trick for categorical variables)
Let us define M independent random variables Z∼Gumbel(0,1)∈RM. Then, for μ∈RM and σ>0, Y:=i(μ+σ⋅Z)⟹Y∼Categorical(softargmax(μ/σ)). Moreover, we have yσ(μ)=EZ[y(μ+σ⋅Z)]=EY[ϕ(Y)]=softargmax(μ/σ).
Proof
For k∈[M], we have that P(Y=k)=P(i∈[M]argmax{μi+σZi}=k)=P(i∈[M]argmin{e−μi/σ−Zi}=k) By Remark 14.3, we have that e−μi/σ−Zi∼Exp(exp(μi/σ−γ)). One easily verifies as an exercise, that, for U1,…,UM independent exponential variables with parameters u1,…,uM, we have P(argmini∈[M]{Ui}=k)=uk/∑i=1Mui. Hence, we get P(Y=k)=∑i=1Mexp(μi/σ)exp(μk/σ), that is, Y∼Categorical(softargmax(μ/σ)). The last claim follows from the distribution of Y and the definition of ϕ. ◻
14.5.4 Perturbed max
A similar result holds if we now wish to perturb the max instead of the argmax.
Proposition 14.5(Link to log-sum-exp)
Let us define M independent random variables Z∼Gumbel(0,1)∈RM and f(u):=i∈[M]maxui. Then, for μ∈RM and σ>0, V:=f(μ+σ⋅Z)⟹V∼Gumbel(σLSE(μ/σ),σ). Moreover, we have fσ(μ):=EZ[f(μ+σ⋅Z)]=EV[V]=σ⋅LSE(μ/σ).
Proof
We derive the CDF of f(μ+σ⋅Z) as P(i∈[M]max{μi+σZi}≤t)=P(i∈[M]min{e−(μi/σ−Zi)}≥e−t/σ) We have e−(μi/σ−Zi)∼Exp(exp(μi/σ)−γ) and for U1,…,UM independent exponential random variables with parameters ui, we have mini∈[M]Ui∼Exp(∑i=1Mui). Hence, P(i∈[M]max{μi+σZi}≤t)=exp(−i=1∑M(exp(μi/σ−γ))exp(−t/σ))=exp(−exp(−(t−σLSE(μ/σ))/σ−γ)). We recognize the CDF of the shifted Gumbel distribution with location-scale parameters σLSE(μ/σ) and σ. ◻
For further reading on the Gumbel trick, see Tim Vieira’s great blog.
14.5.5 Gumbel trick for sampling
The Gumbel trick is also useful in its own right for sampling without computing the normalization constant of the softargmax. Indeed, Proposition 14.4 ensures that if Z is Gumbel noise, then Y is distributed according to Categorical(softargmax(μ/σ)). Computing the arg-maximum, as required to compute Y, can be done in one pass. Therefore, we obtain a one-pass algorithm to sample directly from the logits μ, without explicitly computing the probabilities softargmax(μ/σ).
One may wonder whether such trick could also be used with the normal distribution. Unfortunately, there is no closed form in this case because it would require integrating the CDF of the maximum of M−1 Gaussian distributions. However, other tricks can be defined such as using Weibull distributions, see Balog et al. (2017).
14.5.6 Perturb-and-MAP
Previously, we discussed the Gumbel trick in the classification setting, where Y=[M]. In the structured prediction setting, outputs are typically embedded in RM but the output space is very large. That is, Y⊆RM but ∣Y∣≫M. Structured outputs are then decoded using a maximum a-posteriori (MAP) oracle f(u)y(u):=y∈Ymax⟨y,u⟩:=y∈Yargmax⟨y,u⟩. For this setting, the perturbed versions of f and y, fσ(μ)yσ(μ):=EZ[f(μ+σ⋅Z)]:=EZ[y(μ+σ⋅Z)], no longer enjoy a closed form in general. However, we can approximate them using Monte-carlo estimation. For the gradient of ∇fσ(μ), two estimators exist (Abernethy et al., 2016; Berthet et al., 2020).
Proposition 14.6(Gradient of perturbed max)
Let Y⊆RM and Z be noise with density p0,1(z):=exp(−ν(z))/C. Then, fσ(μ) is smooth, and its gradient is given by ∇fσ(μ)=EZ[y(μ+σ⋅Z)]=EZ[f(μ+σ⋅Z)∇ν(Z)/σ]∈conv(Y). We therefore have ∇fσ(μ)=yσ(μ).
The first estimator is simply a consequence of the reparametrization trick seen in Eq. (14.6) and of y=∇f, which follows from Danskin’s theorem (see Section 11.2). The second estimator is just SFE seen in Eq. (14.7). The first estimator usually has lower variance, as it uses more information, namely that y=∇f.
Under the same notation as in Proposition 14.6, we have ∂yσ(μ)=EZ[y(μ+σZ)∇ν(Z)⊤/σ]=EZ[f(μ+σZ)(∇ν(Z)∇ν(Z)⊤−∇2ν(Z))/σ2].
The first estimator uses SFE. The second estimator is obtained by differentiating through yσ(μ)=∇fσ(μ)=EZ[f(μ+σ⋅Z)∇ν(Z)/σ]. The first estimator usually has lower variance. Note that we cannot use the reparametrization trick this time, since y is discontinuous, contrary to f.
Link between perturbation and regularization
As shown in (Berthet et al., 2020, Proposition 2.2), assuming Y is a convex polytope with non-empty interior and p has a strictly positive density, the function fσ(μ):=EZ[f(μ+σ⋅Z)]=EZ[y∈Ymax⟨μ+σ⋅Z,y⟩] is strictly convex and its convex conjugate fσ∗(y) is Legendre-type. We can therefore rewrite fσ(μ) from the regularization perspective as fσ(μ)=y∈Ymax⟨μ,y⟩−fσ∗(y). and ∇fσ(μ)=yσ(μ) is a mirror map, a one-to-one mapping from RM to the interior of Y. Unfortunately, fσ∗(y) does not enjoy a closed form in general. Conversely, does any regularization have a corresponding noise distribution? The reciprocal is not true.
14.5.7 Gumbel-softargmax
Suppose we want to smooth out the compositionh(u):=g(y(u)) between some function g:{e1,…,eM}→R and the argmax y(u):=y∈{e1,…,eM}argmax⟨y,u⟩. We can do so by hσ(μ):=EZ[g(y(μ+σZ))]. This is useful for instance to compute the expectation of a loss (instead of the loss of an expectation). To compute the gradient of hσ(μ), we can readily use the SFE. However, we saw that it suffers from high variance. Unfortunately, we cannot swap differentiation and integration (expectation) here, since y(u) is a discontinuous function. See Section 12.1 for more details regarding differentiation under the integral sign.
The key idea of the Gumbel-softargmax (Jang et al., 2016; Maddison et al., 2016) is to replace y(u) with a softargmax (with temperature parameter τ) to define hσ,τ(μ):=EZ[g(softargmaxτ(μ+σZ))]. Since the softargmax is a regularized argmax, we can see the Gumbel-softargmax approach as using both regularization and perturbation. Note that the approach is also known as Gumbel-softmax. We use the name Gumbel-softargmax for consistency with the terminology of this book.
The key benefit is that we can now swap differentiation and integration (expectation) to get an unbiased estimator of ∇hσ,τ(μ). However, this will be a biased estimator of ∇hσ(μ), the amount of bias being controlled by the temperature τ. In particular, in the limit case τ→0, we have hσ,τ(μ)→hσ(μ). One caveat, however, is that the function g needs to be well defined on △M, instead of {e1,…,eM}.
Let us define the continuous random variable T:=softargmaxτ(μ+Z)∈△M, where Z is a Gumbel random variable. Then T is distributed according to a distribution with density pμ,τ(t):=Γ(M)τM−1(i=1∑Mtiτπi)−Mi=1∏Mtiτ+1πi, where π:=softargmax(μ).
We can extend the Gumbel softargmax to the structured setting by replacing y(u):=y∈Yargmax⟨y,u⟩, with its regularized variant (Paulus et al., 2020). Similarly as before, one caveat is that g needs to be well defined on conv(Y) instead of Y. Moreover, regularizing y is not always easy computationally.
14.6 Summary
We studied smoothing techniques based on function convolution with a kernel. Due to the commutativity of the convolution, we can alternatively see these as the expectation of the function, perturbed with noise, assuming the kernel corresponds to the PDF of some noise distribution.
Their gradients can be estimated using the path gradient estimator (PGE) or score function estimator (SFE), depending on whether the gradient of the original function is available or not.
We saw that Stein’s lemma is a special case of SFE used with Gaussian noise. The so-called “evolution strategies” are just a variant of that with variance reduction and can be interpreted as randomized finite difference.
When using Gumbel noise, we were able to derive closed-form expressions for the expectation in specific cases: perturbed comparison, perturbed argmax and perturbed max.
We also studied the connections between smoothing by optimization and smoothing by integration. Infimal convolution is the counterpart of convolution, and the Legendre-Fenchel transform is the counterpart of Fourier and Laplace’s transforms. Infimal convolution uses a min-plus algebra in the log domain, while the convolution uses a sum-product algebra in the exponential domain.