The Elements of Differentiable Programming

Chapter 3Probabilistic learning

In this chapter, we review how to perform probabilistic learning. We also introduce exponential family distributions, as they play a key role in this book.

3.1 Probability distributions

3.1.1 Discrete probability distributions

A discrete probability distribution over a set Y\cY is specified by its probability mass function (PMF) p ⁣:Y→[0,1]p \colon \cY \to [0,1]. The probability of y∈Y\y \in \cY is then defined by P(Y=y)≔p(y), \PP(Y = \y) \coloneqq p(\y), where YY denotes a random variable. When YY follows a distribution pp, we write Y∼pY \sim p (with some abuse of notation, we use the same letter pp to denote the distribution and the PMF). The expectation of ϕ(Y)\phi(Y), where Y∼pY \sim p and ϕ ⁣:Y→RM\phi \colon \cY \to \RR^M, is then E[ϕ(Y)]=∑y∈Yp(y)ϕ(y), \EE[\phi(Y)] = \sum_{\y \in \cY} p(\y) \phi(\y), its variance (for one-dimensional variables) is V[ϕ(Y)]=E[(ϕ(Y)−E[ϕ(Y)])2]=∑y∈Yp(y)(ϕ(y)−E[ϕ(Y)])2 \VV[\phi(Y)] = \EE[(\phi(Y) - \EE[\phi(Y)])^2] = \sum_{\y \in \cY} p(\y) (\phi(\y) - \EE[\phi(Y)])^2 and its mode is arg max⁡y∈Yp(y). \argmax_{\y \in \cY} p(\y). The Kullback-Leibler (KL) divergence (also known as relative entropy) between two discrete distributions over Y\cY, with associated PMFs pp and qq, is the statistical “distance” defined by KL(p,q)≔∑y∈Yp(y)log⁡(p(y)q(y))=EY∼plog⁡(p(Y)q(Y)). \mathrm{KL}(p, q) \coloneqq \sum_{\y \in \cY} p(\y) \log\left(\frac{p(\y)}{q(\y)}\right) = \EE_{Y \sim p} \log\left(\frac{p(Y)}{q(Y)}\right).

3.1.2 Continuous probability distributions

A continuous probability distribution over Y\cY is specified by its probability density function (PDF) p ⁣:Y→R+p \colon \cY \to \RR_+. The probability of A⊆Y\cA \subseteq \cY is then P(Y∈A)=∫Ap(y)dy. \PP(Y \in \cA) = \int_\cA p(y) dy. The expectation, variance and KL divergence are defined analogously to the discrete setting, simply replacing ∑y∈Y\sum_{\y \in \cY} with ∫Y\int_{\cY}. Specifically, the expectation of ϕ(Y)\phi(Y) is E[ϕ(Y)]=∫Yp(y)ϕ(y)dy, \EE[\phi(Y)] = \int_{\cY} p(y) \phi(y) dy, the variance is V[ϕ(Y)]=E[(ϕ(Y)−E[ϕ(Y)])2]=∫Yp(y)(ϕ(y)−E[ϕ(Y)])2dy \VV[\phi(Y)] = \EE[(\phi(Y) - \EE[\phi(Y)])^2] = \int_{\cY} p(y) (\phi(y) - \EE[\phi(Y)])^2 dy and the KL divergence is KL(p,q)≔∫Yp(y)log⁡(p(y)q(y))dy=EY∼plog⁡(p(Y)q(Y)). \mathrm{KL}(p, q) \coloneqq \int_\cY p(y) \log\left(\frac{p(y)}{q(y)}\right) dy = \EE_{Y \sim p} \log\left(\frac{p(Y)}{q(Y)}\right). The mode is defined as the argmax of the PDF.

When Y=R\cY = \RR, we can also define the cumulative distribution function (CDF) P(Y≤b)=∫−∞bp(y)dy. \PP(Y \le b) = \int_{-\infty}^b p(y) dy. The probability of YY lying in the semi-closed interval (a,b](a, b] is then P(a<Y≤b)=P(Y≤b)−P(Y≤a). \PP(a < Y \le b) = \PP(Y \le b) - \PP(Y \le a).

3.2 Maximum likelihood estimation

3.2.1 Negative log-likelihood

We saw that a probability distribution over Y\cY is specified by p(y)p(\y), which is called the probability mass function (PMF) for discrete variables or the probability density function (PDF) for continuous variables. In practice, the true distribution pp generating the data is unknown and we wish to approximate it with a distribution pλp_\lambdav, with parameters λ∈Λ\lambdav \in \Lambda. Given a finite set of i.i.d. observations y1,…,yN\y_1, \dots, \y_N, how do we fit λ∈Λ\lambdav \in \Lambda to the data? This can be done by maximizing the likelihood of the data, i.e., we seek to solve λ^N≔arg max⁡λ∈Λ∏i=1Npλ(yi). \widehat \lambdav_N \coloneqq \argmax_{\lambdav \in \Lambda} \prod_{i=1}^N p_{\lambdav}(\y_i). This is known as maximum likelihood estimation (MLE). Because the log⁡\log function is monotonically increasing, this is equivalent to minimizing the negative log-likelihood, i.e., we have λ^N=arg min⁡λ∈Λ−∑i=1Nlog⁡pλ(yi). \widehat \lambdav_N = \argmin_{\lambdav \in \Lambda} -\sum_{i=1}^N \log p_{\lambdav}(\y_i).

Example 3.1 (MLE for the normal distribution)

Suppose we set pλp_\lambdav to the normal distribution with parameters λ=(μ,σ)\lambdav = (\mu, \sigma), i.e., pλ(y)≔1σ2πexp⁡(−12(y−μσ)2). p_\lambdav(y) \coloneqq \frac{1}{\sigma \sqrt{2 \pi}} \exp\left(-\frac{1}{2} \left(\frac{y - \mu}{\sigma}\right)^2\right). Then, given observations y1,…,yNy_1, \dots, y_N, the MLEs for μ\mu and σ2\sigma^2 are the sample mean and the sample variance, respectively.

3.2.2 Consistency w.r.t. the Kullback-Leibler divergence

It is well-known that the MLE is consistent, in the sense of the Kullback-Leibler divergence. That is, denoting the true distribution pp and λ∞≔arg min⁡λ∈ΛKL(p,pλ) \lambdav_\infty \coloneqq \argmin_{\lambdav \in \Lambda} \mathrm{KL}(p, p_\lambdav) where KL(p,pλ)=EY∼plog⁡(p(Y)pλ(Y)), \mathrm{KL}(p, p_\lambdav) = \EE_{Y \sim p} \log\left(\frac{p(Y)}{p_\lambdav(Y)}\right), then λ^N→λ∞\widehat \lambdav_N \to \lambdav_\infty in expectation over the observations, as N→∞N \to \infty. This can be seen by using KL(p,pλ)≈1N∑i=1Nlog⁡(p(yi)pλ(yi))=1N∑i=1N(log⁡p(yi)−log⁡pλ(yi)) \mathrm{KL}(p, p_\lambdav) \approx \frac{1}{N} \sum_{i=1}^N \log\left(\frac{p(\y_i)}{p_\lambdav(\y_i)}\right) = \frac{1}{N} \sum_{i=1}^N \left(\log p(\y_i) - \log p_\lambdav(\y_i)\right) and the law of large numbers.

3.3 Probabilistic supervised learning

3.3.1 Conditional probability distributions

Many times in machine learning, instead of a probability P(Y=y)\PP(Y = \y) for some y∈Y\y \in \cY, we wish to define a conditional probability P(Y=y∣X=x)\PP(Y = \y | X = \x), for some input x∈X\x \in \cX. This can be achieved by reduction to an unconditional probability distribution, P(Y=y∣X=x)≔pλ(y) \PP(Y = \y | X = \x) \coloneqq p_\lambdav(\y) where λ≔f(x,w) \lambdav \coloneqq f(\x, \w) and ff is a model function with model parameters w∈W\w \in \cW. That is, rather than being a deterministic function from X\cX to Y\cY, ff is a function from X\cX to Λ\Lambda, the set of permissible distribution parameters of the output distribution associated with the input.

We emphasize that λ\lambdav could be a single parameter or a collection of parameters. For instance, in the Bernoulli distribution, λ=π\lambda = \pi, while in the univariate normal distribution, λ=(μ,σ)\lambdav = (\mu, \sigma).

In Section 3.4 and throughout this book, we will also use the notation pθp_\thetav instead of pλp_\lambdav when θ\thetav are the canonical parameters of an exponential family distribution.

3.3.2 Inference

The main advantage of this probabilistic approach is that our prediction model is much richer than if we just learned a function from X\cX to Y\cY. We now have access to the whole distribution over possible outcomes in Y\cY and can compute various statistics:

We now review probability distributions useful for binary classification, multiclass classification, regression, multivariate regression, and integer regression. In the following, to make the notation more lightweight, we omit the dependence on x\x.

3.3.3 Binary classification

For binary outcomes, where Y={0,1}\cY = \{0,1\}, we can use a Bernoulli distribution with parameter λ≔π∈[0,1]. \lambda \coloneqq \pi \in [0,1]. When a random variable YY is distributed according to a Bernoulli distribution with parameter π\pi, we write Y∼Bernoulli(π). Y \sim \mathrm{Bernoulli}(\pi). The PMF of this distribution is pπ(y)≔{πif y=11−πif y=0. p_\pi(y) \coloneqq \begin{cases} \pi &\mbox{if } y = 1 \\ 1 - \pi &\mbox{if } y = 0 \end{cases}. The Bernoulli distribution is a binomial distribution with a single trial. Since y∈{0,1}y \in \{0, 1\}, the PMF can be rewritten as pπ(y)=πy(1−π)1−y. p_\pi(y) = \pi^y (1-\pi)^{1 -y}. The mean is E[Y]=π=P(Y=1) \EE[Y] = \pi = \PP(Y = 1) and the variance is V[Y]=π(1−π)=P(Y=1)P(Y=0). \VV[Y] = \pi (1-\pi) = \PP(Y=1)\PP(Y=0).

Figure 3.1: The Bernoulli distribution, whose PMF and CDF are here illustrated with parameter π=0.8\pi = 0.8. Its mean function is π=A′(θ)=logistic(θ)=11+exp⁡(−θ)\pi = A'(\theta) = \mathrm{logistic}(\theta) = \frac{1}{1 + \exp(-\theta)}, where θ\theta is for instance the output of a neural network. The negative log-likelihood leads to the logistic loss, L(θ,y)=softplus(θ)−θy=log⁡(1+exp⁡(θ))−θyL(\theta, y) = \mathrm{softplus}(\theta) - \theta y = \log (1 + \exp(\theta)) - \theta y. The loss curve is shown for y∈{0,1}y \in \{0,1\}.

Parameterization using a sigmoid

Since the parameter π\pi of a Bernoulli distribution needs to belong to [0,1][0,1], we typically use a sigmoid function (Section 4.4.3), such as a logistic function as the output layer: π≔f(x,w)≔logistic(g(x,w)), \pi \coloneqq f(\x, \w) \coloneqq \logistic(g(\x, \w)), where g ⁣:X×W→Rg \colon \cX \times \cW \to \RR is for example a neural network and logistic(a)≔11+exp⁡(−a)∈(0,1). \mathrm{logistic}(a) \coloneqq \frac{1}{1 + \exp(-a)} \in (0,1). When gg is linear in w\w, this is known as binary logistic regression.

Remark 3.1 (Link with the logistic distribution)

The logistic distribution with mean and scale parameters μ\mu and σ\sigma is a continuous probability distribution with PDF pμ,σ(u)≔1σp0,1(u−μσ) p_{\mu,\sigma}(u) \coloneqq \frac{1}{\sigma} p_{0,1}\left(\frac{u - \mu}{\sigma}\right) where p0,1(z)≔exp⁡(−z)(1+exp⁡(−z))2. p_{0,1}(z) \coloneqq \frac{\exp(-z)}{(1 + \exp(-z))^2}. If a random variable UU follows a logistic distribution with parameters μ\mu and σ\sigma, we write U∼Logistic(μ,σ)U \sim \mathrm{Logistic}(\mu, \sigma). The CDF of U∼Logistic(μ,σ)U \sim \mathrm{Logistic}(\mu, \sigma) is P(U≤u)=∫−∞upμ,σ(t)dt=logistic(u−μσ). \PP(U \le u) = \int_{-\infty}^u p_{\mu,\sigma}(t) dt = \mathrm{logistic}\left(\frac{u - \mu}{\sigma}\right). Therefore, if U∼Logistic(μ,σ) U \sim \mathrm{Logistic}(\mu, \sigma) and Y∼Bernoulli(logistic(u−μσ)), Y \sim \mathrm{Bernoulli}\left(\logistic\left(\frac{u - \mu}{\sigma}\right)\right), then P(Y=1)=P(U≤u). \PP(Y = 1) = \PP(U \le u). Here, UU can be interpreted as a latent continuous variable and uu as a threshold.

3.3.4 Multiclass classification

For categorical outcomes with MM possible choices, where Y=[M]\cY = [M], we can use a categorical distribution with parameters λ≔π∈△M, \lambdav \coloneqq \piv \in \triangle^M, where we define the probability simplex △M≔{π∈R+M ⁣:⟨π,1⁡⟩=1}, \triangle^M \coloneqq \{\piv \in \RR_+^M \colon \langle \piv, \ones \rangle = 1 \}, i.e., the set of valid discrete probability distributions. When YY follows a categorical distribution with parameter π\piv, we write Y∼Categorical(π). Y \sim \mathrm{Categorical}(\piv). The PMF of the categorical distribution is pπ(y)≔⟨π,ϕ(y)⟩=πy, p_\piv(y) \coloneqq \langle \piv, \phi(y) \rangle = \pi_y, where ϕ(y)≔ey \phi(y) \coloneqq \e_y is the standard basis vector for the coordinate y∈[M]y \in [M].

Since YY is a categorical variable, it does not make sense to compute the expectation of YY but we can compute that of ϕ(Y)=eY\phi(Y) = \e_Y, EY∼pπ[ϕ(Y)]=π. \EE_{Y \sim p_\piv}[\phi(Y)] = \piv. Therefore, as was also the case for the Bernoulli distribution, the mean and the probability distribution (represented by the vector π\piv) are the same in this case.

Figure 3.2: The categorical distribution, whose PMF and CDF are here illustrated with parameter π=(0.3,0.6,0.1)\piv = (0.3, 0.6, 0.1). Its mean function is π=∇A(θ)=softargmax(θ)\piv = \nabla A(\thetav) = \mathrm{softargmax}(\thetav), where θ∈RM\thetav \in \RR^M is for instance the output of a neural network. Here, for illustration purpose, we choose to set θ=(s,1,0)\thetav = (s, 1, 0) and vary only ss. Since the mean function ∇A(θ)\nabla A(\thetav) belongs to R3\RR^3, we choose to display ⟨∇A(θ),ei⟩=∇A(θ)i\langle \nabla A(\thetav), \e_i \rangle = \nabla A(\thetav)_i, for i∈{1,2,3}i \in \{1,2,3\}. The negative log-likelihood leads to the logistic loss, L(θ,y)=logsumexp(θ)−⟨θ,y⟩L(\thetav, \y) = \mathrm{logsumexp}(\thetav) - \langle \thetav, \y \rangle. The loss curve is shown for y∈{e1,e2,e3}\y \in \{\e_1,\e_2,\e_3\}, again with θ=(s,1,0)\thetav=(s,1,0) and varying ss.

Parameterization using a softargmax

Since the parameter vector π\piv of a categorical distribution needs to belong to △M\triangle^M, we typically use a softargmax as the output layer: π≔f(x,w)≔softargmax(g(x,w)), \piv \coloneqq f(\x, \w) \coloneqq \softargmax(g(\x, \w)), where g ⁣:X×W→RMg \colon \cX \times \cW \to \RR^M is for example a neural network and softargmax(u)≔exp⁡(u)∑jexp⁡(uj)∈relint(△M). \softargmax(\u) \coloneqq \frac{\exp(\u)}{\sum_j \exp(u_j)} \in \mathrm{relint}(\triangle^M). The output of the softargmax is in the relative interior of △M\triangle^M, relint(△M)=△M∩R>0M\mathrm{relint}(\triangle^M) = \triangle^M \cap \RR^M_{>0}. That is, the produced probabilities are always strictly positive. The categorical distribution is a multinomial distribution with a single trial. When gg is linear in w\w, this is therefore known as multiclass or multinomial logistic regression, though strictly speaking a multinomial distribution could use more than one trial.

3.3.5 Regression

For real outcomes, where Y=R\cY = \RR, we can use, among other choices, a normal distribution with parameters λ≔(μ,σ), \lambdav \coloneqq (\mu, \sigma), where μ∈R\mu \in \RR is the mean parameter and σ∈R+\sigma \in \RR_+ is the standard deviation parameter. When YY follows a normal distribution with parameters (μ,σ)(\mu, \sigma), we write Y∼Normal(μ,σ). Y \sim \mathrm{Normal}(\mu, \sigma). The PDF is pμ,σ(y)≔1σ2πexp⁡(−12(y−μ)2σ2). p_{\mu,\sigma}(y) \coloneqq \frac{1}{\sigma \sqrt{2\pi} } \exp\left(-\frac{1}{2}\frac{(y-\mu)^2}{\sigma^2}\right). The expectation is EY∼pμ,σ[Y]=μ. \EE_{Y \sim p_{\mu,\sigma}}[Y] = \mu. One advantage of the probabilistic perspective is that we are not limited to predicting the mean. We can also compute the CDF P(Y≤y)=12[1+erf(y−μσ2)], \PP(Y \le y) = \frac{1}{2}\left[ 1 + \mathrm{erf}\left(\frac{y - \mu}{\sigma \sqrt{2}}\right) \right], where we used the error function erf⁡(z)≔2π∫0ze−t2 dt. \operatorname{erf}(z) \coloneqq \frac{2}{\sqrt\pi}\int_0^z e^{-t^2}\,\mathrm{d}t. This function is available in most scientific computing libraries, such as SciPy (Virtanen et al., 2020). We can also write P(Y≤y)=Φ(y−μσ) \PP(Y \le y) = \Phi\left(\frac{y-\mu}{\sigma}\right) where Φ(z)≔12[1+erf(z2)](3.1) \Phi(z) \coloneqq \frac{1}{2}\left[ 1 + \mathrm{erf}\left(\frac{z}{\sqrt{2}}\right) \right] \tag{3.1} is the CDF of the standard Gaussian distribution (with zero mean and unit variance). From the CDF, we also easily obtain P(a<Y≤b)=12[erf(b−μσ2)−erf(a−μσ2)]. \PP(a < Y \le b) = \frac{1}{2} \left[ \mathrm{erf}\left(\frac{b - \mu}{\sigma \sqrt{2}}\right) - \mathrm{erf}\left(\frac{a - \mu}{\sigma \sqrt{2}}\right) \right].

Figure 3.3: The Gaussian distribution, with mean parameter μ\mu and variance σ2=1\sigma^2=1. Its mean function is μ=A′(θ)=θ\mu = A'(\theta) = \theta, where θ\theta is for instance the output of a neural network. The negative log-likelihood leads to the squared loss, L(θ,y)=(y−θ)2L(\theta, y) = (y - \theta)^2. The loss curve is shown for y∈{−2,0,2}y \in \{-2, 0, 2\}.

Parameterization

Typically, in regression, the mean is output by a model, while the standard deviation σ\sigma is kept fixed (typically set to 11). Since μ\mu is unconstrained, we can simply set μ≔f(x,w)∈R, \mu \coloneqq f(\x, \w) \in \RR, where f ⁣:X×W→Rf \colon \cX \times \cW \to \RR is for example a neural network. That is, the output of ff is the mean of the distribution, EY∼pμ,1[Y]=μ=f(x,w). \EE_{Y \sim p_{\mu,1}}[Y] = \mu = f(\x, \w). We can also use μ\mu to predict P(Y≤y)\PP(Y \le y) or P(a<Y≤b)\PP(a < Y \le b), as shown above.

3.3.6 Multivariate regression

More generally, for multivariate outcomes, where Y=RM\cY = \RR^M, we can use a multivariate normal distribution with parameters λ≔(μ,Σ), \lambdav \coloneqq (\muv, \Sigmav), where μ∈RM\muv \in \RR^M is the mean and Σ∈RM×M\Sigmav \in \RR^{M \times M} is the covariance matrix. When YY follows a multivariate normal distribution with parameters (μ,Σ)(\muv, \Sigmav), we write Y∼Normal(μ,Σ). Y \sim \mathrm{Normal}(\muv, \Sigmav). The PDF is pμ,Σ(y)≔1(2π)M∣Σ∣exp⁡(−12⟨y−μ,Σ−1(y−μ)⟩). p_{\muv,\Sigmav}(\y) \coloneqq \frac{1}{\sqrt{(2 \pi)^M |\Sigmav|}} \exp\left(-\frac{1}{2} \langle \y - \muv, \Sigmav^{-1} (\y - \muv) \rangle\right). Using a diagonal covariance matrix is equivalent to using MM independent normal distributions for each YjY_j, for j∈[M]j \in [M]. The expectation is EY∼pμ,Σ[Y]=μ. \EE_{Y \sim p_{\muv,\Sigmav}}[Y] = \muv.

Parameterization

Typically, in multivariate regression, the mean is output by a model, while the covariance matrix is kept fixed (typically set to the identity matrix). Since μ\muv is again unconstrained, we can simply set μ≔f(x,w)∈RM. \muv \coloneqq f(\x, \w) \in \RR^M. More generally, we can parametrize the function ff so as to output both the mean μ\muv and the covariance matrix Σ\Sigmav, i.e., (μ,Σ)≔f(x,w)∈RM×RM×M. (\muv, \Sigmav) \coloneqq f(\x, \w) \in \RR^M \times \RR^{M \times M}. The function ff must be designed such that Σ\Sigmav is symmetric and positive semi-definite. This is easy to achieve for instance by parametrizing Σ=SS⊤\Sigmav = \S \S^\top for some matrix S\S.

3.3.7 Integer regression

For integer outcomes, where Y=N\cY = \NN, we can use, among other choices, a Poisson distribution with mean parameter λ>0\lambda > 0. When YY follows a Poisson distribution with parameter λ\lambda, we write Y∼Poisson(λ). Y \sim \mathrm{Poisson}(\lambda). The PMF is P(Y=y)=pλ(y)≔λyexp⁡(−λ)y!. \PP(Y = y) = p_\lambda(y) \coloneqq \frac{\lambda^y \exp(-\lambda)}{y!}. It is the probability of yy events occurring in an interval of time. The Poisson distribution is frequently used when there is a large number of possible events, each of which is rare.

The CDF is P(Y≤y)=∑i=0yP(Y=i). \PP(Y \le y) = \sum_{i=0}^y \PP(Y = i). The Poisson distribution implies that the index of dispersion (the ratio between variance and mean) is 11, since E[Y]=V[Y]=λ. \EE[Y] = \VV[Y] = \lambda. When this assumption is inappropriate, one can use generalized Poisson distributions (Satterthwaite, 1942).

Figure 3.4: The Poisson distribution, with mean parameter λ\lambda. For the PMF and the CDF, the lines between markers are shown for visual aid: the Poisson distribution does not assign probability mass to non-integer values. Its mean function is λ=A′(θ)=exp⁡(θ)\lambda = A'(\theta) = \exp(\theta), where θ\theta is for instance the output of a neural network. The negative log-likelihood leads to the Poisson loss, L(θ,y)=−log⁡pλ(y)=−yθ+exp⁡(θ)+log⁡(y!)L(\theta, y) = -\log p_\lambda(y) = -y \theta + \exp(\theta) + \log(y!), which is a convex function of θ\theta. The loss curve is shown for y∈{1,4,10}y \in \{1, 4, 10\}.

Parameterization using an exponential

Since the parameter λ\lambda of a Poisson distribution needs to be strictly positive, we typically use an exponential function as output layer: λ≔f(x,w)≔exp⁡(g(x,w))>0, \lambda \coloneqq f(\x, \w) \coloneqq \exp(g(\x, \w)) > 0, where g ⁣:X×W→Rg \colon \cX \times \cW \to \RR.

3.3.8 Loss functions

We now discuss how to learn the model parameters w∈W\w \in \cW from input-output pairs (x1,y1),…,(xN,yN)(\x_1, \y_1), \dots, (\x_N, \y_N).

Deterministic vs. probabilistic approaches

In a deterministic approach, if we used a mapping f ⁣:X×W→Yf \colon \cX \times \cW \to \cY, we could formulate an objective function of the form L(w)≔1N∑i=1Nℓ(f(xi,w),yi), L(\w) \coloneqq \frac{1}{N} \sum_{i=1}^N \ell(f(\x_i, \w), \y_i), where ℓ ⁣:Y×Y→R\ell \colon \cY \times \cY \to \RR is a loss function. Unfortunately, L(w)L(\w) would be typically discontinuous if Y\cY is a discrete output space (as is the case in classification), making optimization difficult.

In contrast, in the probabilistic approach, we use a mapping f ⁣:X×W→Λf \colon \cX \times \cW \to \Lambda to distribution parameters, and we can formulate an objective of the form L(w)≔1N∑i=1Nℓ(f(xi,w),yi), L(\w) \coloneqq \frac{1}{N} \sum_{i=1}^N \ell(f(\x_i, \w), \y_i), where ℓ ⁣:Λ×Y→R\ell \colon \Lambda \times \cY \to \RR. This is typically a continuous objective, since Λ\Lambda is typically a continuous set and pλp_\lambdav varies continuously w.r.t. λ\lambdav even if pλp_\lambdav is a distribution over a discrete set Y\cY. In other words, the probabilistic approach is not only powerful for the inference it allows us to do (probability, expectation, variance, mode), but also because it allows us to formulate a continuous and typically differentiable objective function!

This is summarized in Fig. 3.5.

Figure 3.5: Summary of deterministic vs. probabilistic approaches.

Negative log-likelihood

In the conditional setting briefly reviewed in Section 3.3.1, we can use maximum likelihood estimation (MLE) to estimate the model parameters w∈W\w \in \cW of ff. Given a set of input-output pairs (x1,y1),…,(xN,yN)(\x_1, \y_1), \dots, (\x_N, \y_N), we choose the model parameters that maximize the likelihood of the data, w^N≔arg max⁡w∈W∏i=1Npλi(yi), \widehat \w_N \coloneqq \argmax_{\w \in \cW} \prod_{i=1}^N p_{\lambdav_i}(\y_i), where λi≔f(xi,w). \lambdav_i \coloneqq f(\x_i, \w). Again, this is equivalent to minimizing the negative log-likelihood, w^N=arg min⁡w∈W−∑i=1Nlog⁡pλi(yi). \widehat \w_N = \argmin_{\w \in \cW} -\sum_{i=1}^N \log p_{\lambdav_i}(\y_i). In the notation above, this corresponds to defining the loss function ℓ(λi,yi)≔−log⁡pλi(yi). \ell(\lambdav_i, \y_i) \coloneqq -\log p_{\lambdav_i}(\y_i).

Recovering well-known loss functions

Interestingly, MLE allows us to recover several popular loss functions.

3.4 Exponential family distributions

3.4.1 Definition

The exponential family is a class of probability distributions, whose PMF or PDF can be written in the form pθ(y)=h(y)exp⁡[⟨θ,ϕ(y)⟩]exp⁡(A(θ))=h(y)exp⁡[⟨θ,ϕ(y)⟩−A(θ)],\begin{align*} p_\thetav(\y) &= \frac{h(\y) \exp\left[\langle \thetav, \phi(\y) \rangle\right]}{\exp(A(\thetav))} \\ &= h(\y) \exp\left[\langle \thetav, \phi(\y) \rangle - A(\thetav)\right], \end{align*} where θ\thetav are the natural or canonical parameters of the distribution. The function hh is known as the base measure. The function ϕ\phi is the sufficient statistic: it holds all the information about y\y and is used to embed y\y in a vector space. The function AA is the log-partition or log-normalizer (see below for details). All the distributions we reviewed in Section 3.3 belong to the exponential family. With some abuse of notation, we use pλp_\lambdav for the distribution in original form and pθp_\thetav for the distribution in exponential family form. As we will see, we can go from θ\thetav to λ\lambdav and vice versa. We illustrate how to rewrite a distribution in exponential family form below.

Example 3.2 (Bernoulli distribution)

The PMF of the Bernoulli distribution with parameter λ=π\lambda = \pi equals pλ(y)≔πy(1−π)1−y=exp⁡(log⁡(πy(1−π)1−y))=exp⁡(ylog⁡(π)+(1−y)log⁡(1−π))=exp⁡(log⁡(π/(1−π))y+log⁡(1−π))=exp⁡(θy−log⁡(1+exp⁡(θ)))=exp⁡(θy−softplus(θ))≕pθ(y).\begin{align*} p_\lambda(y) &\coloneqq \pi^y (1-\pi)^{1-y} \\ &= \exp(\log(\pi^y (1-\pi)^{1-y})) \\ &= \exp(y \log(\pi) + (1-y) \log(1-\pi)) \\ &= \exp(\log(\pi/(1-\pi)) y + \log(1 - \pi)) \\ &= \exp(\theta y - \log(1 + \exp(\theta))) \\ &= \exp(\theta y - \mathrm{softplus}(\theta)) \\ &\eqqcolon p_\theta(y). \end{align*} Therefore, Bernoulli distributions belong to the exponential family, with natural parameter θ=logit(π)≔log⁡(π/(1+π))\theta = \mathrm{logit}(\pi) \coloneqq \log(\pi/(1+\pi)). Conversely, we have π=logistic(θ)=11+exp⁡(−θ)\pi = \mathrm{logistic}(\theta) = \frac{1}{1 + \exp(-\theta)}.

We rewrite the previously-described distributions in exponential family form in Table 3.1. This list is non-exhaustive: there are many more distributions in the exponential family! (Barndorff-Nielsen, 2014)

Table 3.1: Examples of distributions in the exponential family.

Bernoulli Categorical
Y\cY {0,1}\{0,1\} [M][M]
λ\lambdav π=logistic(θ)\pi = \mathrm{logistic}(\theta) π=softargmax(θ)\piv = \mathrm{softargmax}(\thetav)
θ\thetav logit(π)\mathrm{logit}(\pi) log⁡π+A(θ)\log \piv + A(\thetav)
ϕ(y)\phi(y) yy ey\e_y
A(θ)A(\thetav) softplus(θ)\mathrm{softplus}(\theta) logsumexp(θ)\mathrm{logsumexp}(\thetav)
h(y)h(y) 1 1
Normal (location only) Normal (location-scale)
Y\cY R\RR R\RR
λ\lambdav μ=θσ\mu = \theta \sigma (μ,σ2)=(−θ12θ2,−12θ2)(\mu, \sigma^2) = (\frac{-\theta_1}{2\theta_2}, \frac{-1}{2\theta_2})
θ\thetav μσ\frac{\mu}{\sigma} (μσ2,−12σ2)(\frac{\mu}{\sigma^2}, \frac{-1}{2\sigma^2})
ϕ(y)\phi(y) yσ\frac{y}{\sigma} (y,y2)(y, y^2)
A(θ)A(\thetav) θ22=μ22σ2\frac{\theta^2}{2} = \frac{\mu^2}{2\sigma^2} −θ124θ2−12log⁡(−2θ2)=μ22σ2+log⁡σ\frac{-\theta_1^2}{4\theta_2} - \frac{1}{2} \log(-2\theta_2) = \frac{\mu^2}{2\sigma^2} + \log \sigma
h(y)h(y) exp⁡(−y22σ2)2πσ\frac{\exp(\frac{-y^2}{2\sigma^2})}{\sqrt{2\pi}\sigma} 12π\frac{1}{\sqrt{2\pi}}
Multivariate normal Poisson
Y\cY RM\RR^M N\NN
λ\lambdav (μ,Σ)=(−12θ2−1θ1,−12θ2−1)(\muv, \Sigmav) = (-\frac{1}{2} \thetav_2^{-1} \thetav_1, -\frac{1}{2} \thetav_2^{-1}) λ=exp⁡(θ)\lambda = \exp(\theta)
θ\thetav (Σ−1μ,−12Σ−1)(\Sigmav^{-1} \muv, -\frac{1}{2} \Sigmav^{-1}) log⁡λ\log \lambda
ϕ(y)\phi(\y) (y,yy⊤)(\y, \y\y^\top) yy
A(θ)A(\thetav) −14θ1⊤θ2−1θ1−12log⁡∣−2θ2∣-\frac{1}{4} \thetav_1^\top \thetav_2^{-1} \thetav_1 - \frac{1}{2} \log |-2\thetav_2| exp⁡(θ)\exp(\theta)
=12μ⊤Σ−1μ+12log⁡∣Σ∣= \frac{1}{2} \muv^\top \Sigmav^{-1} \muv + \frac{1}{2} \log |\Sigmav|
h(y)h(\y) (2π)−M/2(2\pi)^{-M/2} 1/y!1/y!

3.4.2 The log-partition function

We focus our discussion in this section on discrete variables for simplicity. However, the results stated in this section also hold for continuous variables (Wainwright and Jordan, 2008). The log-partition function AA is the logarithm of the distribution’s normalization factor, A(θ)≔log⁡∑y∈Yh(y)exp⁡[⟨θ,ϕ(y)⟩]. A(\thetav) \coloneqq \log \sum_{\y \in \cY} h(\y) \exp\left[\langle \thetav, \phi(\y) \rangle\right]. We denote the set of valid parameters Θ≔{θ∈RM ⁣:A(θ)<+∞}⊆RM. \Theta \coloneqq \{ \thetav \in \RR^M \colon A(\thetav) < +\infty\} \subseteq \RR^M. We can conveniently rewrite A(θ)A(\thetav) as A(θ)=logsumexp(B(θ))≔log⁡∑y∈Yexp⁡([B(θ)]y), A(\thetav) = \mathrm{logsumexp}(B(\thetav)) \coloneqq \log \sum_{\y \in \cY} \exp([B(\thetav)]_\y), where we defined the affine map B(θ)≔(⟨θ,ϕ(y)⟩+log⁡h(y))y∈Y. B(\thetav) \coloneqq (\langle \thetav, \phi(\y) \rangle + \log h(\y))_{\y \in \cY}. Since A(θ)A(\thetav) is the composition of logsumexp\mathrm{logsumexp}, a convex function, and of BB, an affine map, we immediately obtain the following proposition.

Proposition 3.1 (Convexity of the log-partition)

A(θ)A(\thetav) is a convex function.

A major property of the log-partition function is that its gradient coincides with the expectation of ϕ(Y)\phi(Y) according to pθp_\thetav.

Proposition 3.2 (Gradient of the log-partition)

μ(θ)≔∇A(θ)=EY∼pθ[ϕ(Y)]∈M.\begin{align*} \muv(\thetav) \coloneqq \nabla A(\thetav) = \EE_{Y \sim p_\thetav}[\phi(Y)] \in \cM. \end{align*}

Proof

The result follows directly from ∇A(θ)=∂B(θ)∗∇logsumexp(B(θ))=(ϕ(y))y∈Y softmax(B(θ)). \nabla A(\thetav) = \partial B(\thetav)^* \nabla \mathrm{logsumexp}(B(\thetav)) = (\phi(\y))_{\y \in \cY} ~ \mathrm{softmax}(B(\thetav)).  ◻

The gradient ∇A(θ)\nabla A(\thetav) is therefore often called the mean function. The set of achievable means μ(θ)\muv(\thetav) is defined by M≔conv(ϕ(Y))≔{Ep[ϕ(Y)] ⁣:p∈P(Y)}, \cM \coloneqq \mathrm{conv}(\phi(\cY)) \coloneqq \{ \EE_{p}[\phi(Y)] \colon p \in \cP(\cY)\}, where conv(S)\mathrm{conv}(\cS) is the convex hull of S\cS and P(Y)\cP(\cY) is the set of valid probability distributions over Y\cY.

Similarly, the Hessian ∇2A(θ)\nabla^2 A(\thetav) coincides with the covariance matrix of ϕ(Y)\phi(Y) according to pθp_\thetav (Wainwright and Jordan, 2008, 3).

When the exponential family is minimal, which means that the parameters θ\thetav uniquely identify the distribution, it is known that ∇A\nabla A is a one-to-one mapping from Θ\Theta to M\cM. That is, μ(θ)=∇A(θ)\muv(\thetav) = \nabla A(\thetav) and θ=(∇A)−1(μ(θ))\thetav = (\nabla A)^{-1}(\muv(\thetav)).

3.4.3 Maximum entropy principle

Suppose we observe the empirical mean μ^=1N∑i=1Nϕ(yi)∈M\widehat \muv = \frac{1}{N} \sum_{i=1}^N \phi(\y_i) \in \cM of some observations y1,…,yN\y_1,\dots,\y_N. How do we find a probability distribution achieving this mean? Clearly, such a distribution may not be unique. One way to choose among all possible distributions is by using the maximum entropy principle. Let us define the Shannon entropy by H(p)≔−∑y∈Yp(y)log⁡p(y) H(p) \coloneqq -\sum_{\y \in \cY} p(\y) \log p(\y) for discrete variables and by H(p)≔−∫Yp(y)log⁡p(y)dy H(p) \coloneqq -\int_{\cY} p(y) \log p(y) dy for continuous variables. This captures the level of “uncertainty” in pp, i.e., it is maximized when the distribution is uniform. Then, the maximum entropy distribution satisfying the first-order moment condition (i.e., whose expectation matches the empirical mean) is p⋆≔arg max⁡p∈P(Y)H(p)s.t.EY∼p[ϕ(Y)]=μ^. p^\star \coloneqq \argmax_{p \in \cP(\cY)} H(p) \quad \text{s.t.} \quad \EE_{Y \sim p}[\phi(Y)] = \widehat \muv. It can be shown that the maximum entropy distribution is necessarily in the exponential family with sufficient statistic defined by ϕ\phi and its canonical parameters θ\thetav coincide with the Lagrange multipliers of the above constraint (Wainwright and Jordan, 2008, Section 3.1).

3.4.4 Maximum likelihood estimation

As in Section 3.2, to fit the parameters θ∈Θ\thetav \in \Theta of an exponential family distribution to some i.i.d. observations y1,…,yN\y_1, \dots, \y_N, we can use the MLE principle, i.e., θ^N=arg max⁡θ∈Θ∏i=1Npθ(yi)=arg min⁡θ∈Θ−∑i=1Nlog⁡pθ(yi). \widehat \thetav_N = \argmax_{\thetav \in \Theta} \prod_{i=1}^N p_\thetav(\y_i) = \argmin_{\thetav \in \Theta} -\sum_{i=1}^N \log p_\thetav(\y_i). Fortunately, for exponential family distributions, the log probability/density enjoys a particularly simple form.

Proposition 3.3 (Negative log-likelihood)

The negative log-likelihood of an exponential family distribution is −log⁡pθ(y)=A(θ)−⟨θ,ϕ(y)⟩−log⁡h(y). -\log p_\thetav(\y) = A(\thetav) - \langle \thetav, \phi(\y) \rangle - \log h(\y). Its gradient is −∇θlog⁡pθ(y)=∇A(θ)−ϕ(y)=EY∼pθ[ϕ(Y)]−ϕ(y) -\nabla_\thetav \log p_\thetav(\y) = \nabla A(\thetav) - \phi(\y) = \EE_{Y \sim p_\thetav}[\phi(Y)] - \phi(\y) and its Hessian is −∇θ2log⁡pθ(y)=∇2A(θ), -\nabla^2_\thetav \log p_\thetav(\y) = \nabla^2 A(\thetav), which is independent of y\y.

It follows from Proposition 3.1 that θ↦−log⁡pθ(y)\thetav \mapsto -\log p_\thetav(\y) is convex. Interestingly, we see that the gradient is the residual between the expectation of ϕ(Y)\phi(Y) according to the model and the observed ϕ(y)\phi(\y). Therefore, the negative log-likelihood of an exponential family distribution can be seen as performing first moment matching.

3.4.5 Probabilistic learning with exponential families

In the supervised probabilistic learning setting, we wish to estimate a conditional distribution of the form pw(y∣x)p_\w(\y \mid \x). Given a model function ff, such as a neural network, a common approach for defining such a conditional distribution is by reduction to the unconditional setting, pw(y∣x)≔pθ(y)whereθ≔f(x,w). p_\w(\y \mid \x) \coloneqq p_\thetav(\y) \quad \text{where} \quad \thetav \coloneqq f(\x, \w). In other words, the role of ff is to produce the parameters of pθp_\thetav given some input x\x. It is a function from X×W\cX \times \cW to Θ\Theta. Note that ff must be designed such that it produces an output in Θ≔{θ∈RM ⁣:A(θ)<+∞}. \Theta \coloneqq \{ \thetav \in \RR^M \colon A(\thetav) < +\infty\}. Many times, Θ\Theta will be the entire RM\RR^M but this is not always the case. For instance, as we previously discussed, for a multivariate normal distribution, where θ=(μ,Σ)=f(x,w)\thetav = (\muv, \Sigmav) = f(\x, \w), we need to ensure that Σ\Sigmav is a positive semidefinite matrix.

Training

Given input-output pairs {(xi,yi)}i=1N\{(\x_i, \y_i)\}_{i=1}^N, we then seek to find the parameters w\w of f(x,w)f(\x, \w) by minimizing the negative log-likelihood arg min⁡w∈W−∑i=1Nlog⁡pθi(yi)=arg min⁡w∈W∑i=1NA(θi)−⟨θi,ϕ(yi)⟩ \argmin_{\w \in \cW} -\sum_{i=1}^N \log p_{\thetav_i}(\y_i) = \argmin_{\w \in \cW} \sum_{i=1}^N A(\thetav_i) - \langle \thetav_i, \phi(\y_i) \rangle where θi≔f(xi,w)\thetav_i \coloneqq f(\x_i, \w). While −log⁡pθ(y)-\log p_\thetav(\y) is a convex function of θ\thetav for exponential family distributions, we emphasize that −log⁡pf(x,w)(y)-\log p_{f(\x,\w)}(\y) is typically a nonconvex function of w\w, when ff is a nonlinear function, such as a neural network.

Figure 3.6: Generalized linear models.

Inference

Once we have found w\w by minimizing the objective function above, there are several possible strategies to perform inference for a new input x\x.

3.5 Summary