The Elements of Differentiable Programming

Chapter 10Inference in graphical models as differentiation

A graphical model specifies how random variables depend on each other and therefore determines how their joint probability distribution factorizes. In this chapter, we review key concepts in graphical models and how they relate to differentiation, drawing in the process analogies with computation chains and computation graphs.

10.1 Chain rule of probability

The chain rule of probability is a fundamental law in probability theory for computing the joint probability of events. In the case of only two events A1A_1 and A2A_2, it reduces to the product rule P(A1∩A2)=P(A2∣A1)P(A1). \PP(A_1 \cap A_2) = \PP(A_2 | A_1) \PP(A_1). For two discrete random variables S1S_1 and S2S_2, using the events A1≔{S1=s1}A_1 \coloneqq \{S_1 = \s_1\} and A2≔{S2=s2}A_2 \coloneqq \{S_2 = \s_2\}, the product rule becomes P(S1=s1,S2=s2)=P(S2=s2∣S1=s1)P(S1=s1). \PP(S_1= \s_1, S_2= \s_2) = \PP(S_2=\s_2 | S_1=\s_1) \PP(S_1=\s_1). More generally, using the product rule, we have for KK events P(A1∩…∩AK)=P(AK∣A1∩…∩AK−1)P(A1∩…∩AK−1). \PP\left(A_1 \cap \ldots \cap A_K\right) = \PP\left(A_K \mid A_1 \cap \ldots \cap A_{K-1}\right) \PP\left(A_1 \cap \ldots \cap A_{K-1}\right). Applying the product rule one more time, we have P(A1∩…∩AK−1)=P(AK−1∣A1∩…∩AK−2)P(A1∩…∩AK−2). \PP\left(A_1 \cap \ldots \cap A_{K-1}\right) = \PP\left(A_{K-1} \mid A_1 \cap \ldots \cap A_{K-2}\right) \PP\left(A_1 \cap \ldots \cap A_{K-2}\right). Repeating the process recursively, we arrive at the chain rule of probability P(A1∩…∩AK)=∏j=1KP(Aj∣A1∩⋯∩Aj−1)=∏j=1KP(Aj ∣ ⋂i=1j−1Ai).\begin{align*} \PP\left(A_1 \cap \ldots \cap A_K\right) &= \prod_{j=1}^K \PP(A_j \mid A_1 \cap \dots \cap A_{j-1})\\ &= \prod_{j=1}^K \PP\left(A_j \,\Bigg|\, \bigcap_{i=1}^{j-1} A_i\right). \end{align*} For KK discrete random variables SjS_j, using the events Aj≔{Sj=sj}A_j \coloneqq \{S_j = \s_j \}, the chain rule of probability becomes P(S1=s1,…,SK=sK)=∏j=1KP(Sj=sj∣S1=s1,…,Sj−1=sj−1). \PP\left(S_1 = \s_1, \ldots,S_K = \s_K\right) = \prod_{j=1}^K \mathbb P(S_j = \s_j \mid S_1 = \s_1, \dots, S_{j-1}=\s_{j-1}). Importantly, this factorization holds without any independence assumption on the variables S1,…,SKS_1,\dots,S_K. In other words, the space of probability distributions specified by the joint probability on the left-hand side, and the space of probability distributions specified by the product of conditional probabilities on the right-hand side, are the same. We can further simplify the factorization if we make additional conditional independence assumptions.

10.2 Conditional independence

We know that if two events AA and BB are independent, then P(A∣B)=P(A). \PP(A|B) = \PP(A). Similarly, if two random variables S1S_1 and S2S_2 are independent, then P(S2=s2∣S1=s1)=P(S2=s2). \PP(S_2=\s_2|S_1=\s_1) = \PP(S_2=\s_2). More generally, if we work with KK variables S1,…,SKS_1, \dots, S_K, some variables may depend on each other, while others may not. To simplify the notation, given a set C\cC, we define the shorthands SC≔(Si ⁣:i∈C)sC≔(si ⁣:i∈C).\begin{align*} S_\cC &\coloneqq (S_i \colon i \in \cC) \\ \s_\cC &\coloneqq (\s_i \colon i \in \cC). \end{align*} We say that a variable SjS_j is independent of SDS_\cD conditioned on SCS_\cC, with C∩D=∅\cC \cap \cD = \emptyset, if for any sj,sC,sD\s_j, \s_{\cC}, \s_{\cD} P(Sj=sj∣SC=sC,SD=sD)=P(Sj=sj∣SC=sC). \PP(S_j = \s_j \mid S_\cC = \s_\cC, S_\cD = \s_\cD) = \PP(S_j = \s_j \mid S_\cC = \s_\cC).

10.3 Inference problems

10.3.1 Joint probability distributions

We consider a collection of KK variables s≔(s1,…,sK)\s \coloneqq (\s_1, \dots, \s_K), potentially ordered or unordered. Each s\s belongs to the Cartesian product S≔S1×⋯×SK\cS \coloneqq \cS_1 \times \dots \times \cS_K. Throughout this chapter, we assume that the sets Sk\cS_k are discrete for concreteness, with Sk≔{v1,…,vMk}\cS_k \coloneqq \{\v_1, \dots, \v_{M_k}\}. Note that because Sk\cS_k is discrete, we can always identify it with {1,…,Mk}\{1,\dots,M_k\}. A graphical model specifies a joint probability distribution P(S=s)=P(S1=s1,…,SK=sK)=p(s)=p(s1,…,sK),\begin{align*} \PP(S = \s) &= \PP(S_1=\s_1,\dots,S_K=\s_K) \\ &= p(\s) \\ &= p(\s_1,\dots,\s_K), \end{align*} where pp is the probability mass function of the joint probability distribution. Summing over the Cartesian product of all possible configurations, we obtain ∑s∈Sp(s)=∑s1,…,sK∈Sp(s1,…,sK)=1. \sum_{\s \in \cS} p(\s) = \sum_{\s_1,\dots,\s_K \in \cS} p(\s_1,\dots,\s_K) = 1. As we shall see, the graph of a graphical model encodes the dependencies between the variables (S1,…,SK)(S_1,\dots,S_K) and therefore how their joint distribution factorizes. Given access to a joint probability distribution, there are several inference problems one typically needs to solve.

10.3.2 Likelihood

A simple task is to compute the likelihood of some observations s=(s1,…,sK)\s = (\s_1,\dots,\s_K), P(S1=s1,…,SK=sK)=p(s1,…,sK). \PP(S_1 = \s_1, \dots, S_K = \s_K) = p(\s_1, \dots, \s_K). It is also common to compute the log-likelihood, log⁡P(S1=s1,…,SK=sK)=log⁡p(s1,…,sK). \log \PP(S_1 = \s_1, \dots, S_K = \s_K) = \log p(\s_1, \dots, \s_K).

10.3.3 Maximum a-posteriori inference

Another common task is to compute the most likely configuration, arg max⁡s1∈S1,…,sK∈SKp(s1,…,sK). \argmax_{\s_1 \in \cS_1, \dots, \s_K \in \cS_K} p(\s_1,\dots,\s_K). This is the mode of the joint probability distribution. This is also known as maximum a-posteriori (MAP) inference in the literature (Wainwright and Jordan, 2008).

10.3.4 Marginal inference

The operation of marginalization consists in summing (or integrating) over all possible values of a given variable in a joint probability distribution. This allows us to compute the marginal probability of the remaining variables. For instance, we may want to marginalize all variables but Sk=skS_k=\s_k. To do so, we define the Cartesian product Ck(sk)≔S1×⋯×Sk−1⏟Ak−1×{sk}×Sk+1×⋯×SK⏟Bk+1.\begin{align*} \cC_k(\s_k) \coloneqq \underbrace{\cS_1 \times \dots \times \cS_{k-1}}_{\cA_{k-1}} \times \{\s_k\} \times \underbrace{\cS_{k+1} \times \dots \times \cS_K}_{\cB_{k+1}}. \tag{10.1}\end{align*} Summing over all variables but SkS_k, we obtain the marginal probability of Sk=skS_k=\s_k as P(Sk=sk)=∑s1,…,sK∈Ck(sk)p(s1,…,sK)=∑s1,…,sk−1∈Ak−1∑sk+1,…,sK∈Bk+1p(s1,…,sK).\begin{align*} \PP(S_k = \s_k) &= \sum_{\s_1,\dots,\s_K \in \cC_k(\s_k)} p(\s_1,\dots,\s_K) \\ &= \sum_{\s_1,\dots,\s_{k-1} \in \cA_{k-1}} \sum_{\s_{k+1},\dots,\s_K \in \cB_{k+1}} p(\s_1,\dots,\s_K). \end{align*} Defining similarly Ck,l(sk,sl)≔S1×⋯×{sk}×⋯×{sl}×⋯×SK,\begin{align*} \cC_{k,l}(\s_k,\s_l) \coloneqq \cS_1 \times \dots \times \{\s_k\} \times \dots \times \{\s_l\} \times \dots \times \cS_K, \end{align*} we obtain P(Sk=sk,Sl=sl)=∑s1,…,sK∈Ck,l(sk,sl)p(s1,…,sK).\begin{align*} \PP(S_k = \s_k, S_l=\s_l) = \sum_{\s_1,\dots,\s_K \in \cC_{k,l}(\s_k,\s_l)} p(\s_1,\dots,\s_K). \end{align*} In particular, we may want to compute the marginal probability of two consecutive variables, P(Sk−1=sk−1,Sk=sk)\PP(S_{k-1} = \s_{k-1}, S_k=\s_k).

10.3.5 Expectation, convex hull, marginal polytope

Another common operation is to compute the expectation of ϕ(S)\phi(S) under a distribution pp. It is defined by μ≔ES∼p[ϕ(S)]=∑s∈Sp(s)ϕ(s)∈M.\begin{align*} \muv \coloneqq \EE_{S \sim p}[\phi(S)] = \sum_{\s \in \cS} p(\s) \phi(\s) \in \cM. \end{align*} For the expectation under pθp_\thetav, we write μ(θ)≔ES∼pθ[ϕ(S)]=∑s∈Spθ(s)ϕ(s)∈M.\begin{align*} \mu(\thetav) \coloneqq \EE_{S \sim p_\thetav}[\phi(S)] = \sum_{\s \in \cS} p_\thetav(\s) \phi(\s) \in \cM. \end{align*} In exponential family distributions (Section 3.4), the function ϕ\phi is called a statistic. It decomposes as ϕ(s)≔(ϕC(sC))C∈C,\begin{align*} \phi(\s) \coloneqq (\phi_\cC(\s_\cC))_{\cC \in C}, \end{align*} where C⊆[K]\cC \subseteq [K]. Intuitively, ϕ(s)\phi(\s) can be thought of as an encoding or embedding of s\s (a potentially discrete object such as a sequence of integers) in a vector space. Under this decomposition, we can also compute μC≔ES[ϕC(SC)]=∑s∈Sp(s)ϕC(sC). \muv_\cC \coloneqq \EE_S[\phi_\cC(S_\cC)] = \sum_{\s \in \cS} p(\s) \phi_\cC(\s_\cC).

Convex hull

The mean μ\muv belongs to the convex hull of ϕ(S)≔{ϕ(s) ⁣:s∈S}\phi(\cS) \coloneqq \{\phi(\s) \colon \s \in \cS\}, M≔conv⁡(ϕ(S))≔{∑s∈Sp(s)ϕ(s) ⁣:p∈P(S)}, \cM \coloneqq \conv(\phi(\cS)) \coloneqq \left\{ \sum_{\s \in \cS} p(\s) \phi(\s) \colon p \in \cP(\cS) \right\}, where P(S)\cP(\cS) is the set of all possible probability distributions over S\cS. In other words, M\cM is the set of all possible convex combinations of ϕ(s)\phi(\s) for s∈S\s \in \cS. The vertices of M\cM are all the s∈S\s \in \cS.

Case of binary encodings: the marginal polytope

In the special case of a discrete set Sk={v1,…,vM}\cS_k = \{\v_1,\dots,\v_M\} and of a binary encoding (indicator function) ϕ(s)\phi(\s), the set M\cM is called the marginal polytope (Wainwright and Jordan, 2008), because each point μ∈M\muv \in \cM contains marginal probabilities. To see why, consider the unary potential [ϕ(s)]k,i=[ϕk(sk)]i=I(sk=vi)(10.2) [\phi(\s)]_{k,i} = [\phi_k(\s_k)]_{i} = \II(\s_k = \v_i) \tag{10.2} where I(p)≔1\II(p) \coloneqq 1 if pp is true, 00 otherwise. We then obtain the marginal probability of Sk=viS_k=\v_i, [μ]k,i=ES[ϕ(S)k,i]=ESk[ϕk(Sk)i]=ESk[I(Sk=vi)]=∑sk∈SkP(Sk=sk)I(sk=vi)=P(Sk=vi).\begin{align*} [\muv]_{k,i} &= \EE_S[\phi(S)_{k,i}] \\ &= \EE_{S_k}[\phi_k(S_k)_i] \\ &= \EE_{S_k}[\II(S_k=\v_i)] \\ &= \sum_{\s_k \in \cS_k} \PP(S_k=\s_k)\II(\s_k=\v_i) \\ &= \PP(S_k = \v_i). \end{align*} Likewise, consider the pairwise potential [ϕ(s)]k,l,i,j=[ϕk,l(sk,sl)]i,j=I(sk=vi,sl=vj).(10.3) [\phi(\s)]_{k,l,i,j} = [\phi_{k,l}(\s_k, \s_l)]_{i,j} = \II(\s_k = \v_i, \s_l=\v_j) . \tag{10.3} We then obtain the marginal probability of Sk=viS_k=\v_i and Sl=vjS_l=\v_j, [μ]k,l,i,j=ES[ϕ(S)k,l,i,j]=ESk,Sl[ϕk,l(Sk,Sl)i,j]=ESk,Sl[I(Sk=vi,Sl=vj)]=∑sk∈Sk∑sl∈SlP(Sk=sk,Sl=sl)I(sk=vi,sl=vj)=P(Sk=vi,Sl=vj).\begin{align*} [\muv]_{k,l,i,j} &= \EE_S[\phi(S)_{k,l,i,j}] \\ &= \EE_{S_k,S_l}[\phi_{k,l}(S_k,S_l)_{i,j}] \\ &= \EE_{S_k,S_l}[\II(S_k=\v_i,S_l=\v_j)] \\ &= \sum_{\s_k \in \cS_k} \sum_{\s_l \in \cS_l} \PP(S_k=\s_k,S_l=\s_l) \II(\s_k=\v_i,\s_l=\v_j) \\ &= \PP(S_k=\v_i, S_l = \v_j). \end{align*} We can do the same with higher-order potential functions.

10.3.6 Complexity of brute force

Apart from computing the likelihood, which is trivial, computing the marginal, mode and expectation by brute force takes O(∏k=1K∣Sk∣)O(\prod_{k=1}^K |\cS_k|) time. In particular, if ∣Sk∣=M ∀k∈[K]|\cS_k| = M ~\forall k \in [K], brute force takes O(MK)O(M^K) time.

10.4 Markov chains

In this section, we briefly review Markov chains. Our notation is chosen to emphasize the analogies with computation chains.

10.4.1 The Markov property

When random variables are organized sequentially as S1,…,SKS_1,\dots,S_K, a simple example of conditional independence is when each variable Sk∈SkS_k \in \cS_k only depends on the previous variable Sk−1∈Sk−1S_{k-1} \in \cS_{k-1}, that is, P(Sk=sk∣Sk−1=sk−1,…,S1=s1)=P(Sk=sk∣Sk−1=sk−1)≔pk(sk∣sk−1).\begin{align*} \PP(S_k=\s_k \mid S_{k-1}=\s_{k-1}, \dots, S_1=\s_1) &= \PP(S_k=\s_k \mid S_{k-1}=\s_{k-1}) \\ &\coloneqq p_k(\s_k \mid \s_{k-1}). \end{align*} A probability distribution satisfying the above is said to satisfy the Markov property, and is called a Markov chain. A computation chain is specified by the functions fkf_k, that take sk−1\s_{k-1} as input and output sk\s_k. In analogy, a Markov chain is specified by the conditional probability distributions pkp_k of SkS_k given Sk−1S_{k-1}. We can then define the generative process S0≔s0S1∼p1(⋅∣S0)S2∼p2(⋅∣S1)⋮SK∼pK(⋅∣SK−1).\begin{align*} S_0 &\coloneqq \s_0 \\ S_1 &\sim p_1(\cdot \mid S_0) \\ S_2 &\sim p_2(\cdot \mid S_1) \\ &\vdots \\ S_K &\sim p_K(\cdot \mid S_{K-1}). \end{align*} Strictly speaking, we should write Sk∣Sk−1∼pk(⋅∣Sk−1)S_k \mid S_{k-1} \sim p_k(\cdot \mid S_{k-1}). We choose our notation both for conciseness and for analogy with computation chains. Furthermore, to simplify the notation, we assume without loss of generality that S0S_0 is deterministic (if this is not the case, we can always move S0S_0 to S1S_1 and add a dummy variable as S0S_0). That is, P(S0=s0)=p0(s0)≔1\PP(S_0 = \s_0) = p_0(\s_0) \coloneqq 1 and S0≔{s0}\cS_0 \coloneqq \{\s_0\}. This amounts to setting the initial distribution of S1S_1 as P(S1=s1)≔P(S0=s0)P(S1=s1∣S0=s0)=P(S1=s1∣S0=s0). \PP(S_1 = \s_1) \coloneqq \PP(S_0 = \s_0) \PP(S_1 = \s_1 | S_0 = \s_0) = \PP(S_1 = \s_1 | S_0 = \s_0). We can then compute the joint probability of the Markov chain by P(S1=s1,…,SK=sK)=p(s1,…,sK)=∏k=1KP(Sk=sk∣Sk−1=sk−1)=∏k=1Kpk(sk∣sk−1),\begin{align*} \PP(S_1=\s_1,\dots,S_K=\s_K) &= p(\s_1,\dots,\s_K) \\ &= \prod_{k=1}^K \PP(S_k = \s_k \mid S_{k-1} = \s_{k-1}) \\ &= \prod_{k=1}^K p_k(\s_k \mid \s_{k-1}), \end{align*} where we left the dependence on s0\s_0 implicit, since p0(s0)=1p_0(\s_0) = 1. A Markov chain with Sk={1,2,3}\cS_k = \{1,2,3\} is illustrated in Fig. 10.1. A chain defines a totally ordered set {1,…,K}\{1, \dots, K\}, since two nodes in the graph are necessarily linked to each other by a path.

 

Figure 10.1: Left: Markov chain. Right: Computation graph of the forward-backward and the Viterbi algorithms: a lattice.

Example 10.1 (Chain of categorical distributions)

Suppose our goal is to predict, from x∈X\x \in \cX, a sequence of length KK, where each SkS_k belongs to Sk={1,…,M}\cS_k = \{1, \dots, M\}. In natural language processing, this task is called sequence tagging. We can define Sk∼Categorical(πk−1,k,Sk−1) S_k \sim \mathrm{Categorical}(\piv_{k-1,k,S_{k-1}}) where πk−1,k,i≔softargmax(θk−1,k,i)∈△M=(πk−1,k,i,j)j=1Mθk−1,k,i≔(θk−1,k,i,j)j=1M∈RMθk−1,k,i,j≔fk−1,k(x,i,j,wk)∈R.\begin{align*} \piv_{k-1,k,i} &\coloneqq \mathrm{softargmax}(\thetav_{k-1,k,i}) \in \triangle^M \\ &= (\pi_{k-1,k,i,j})_{j=1}^M \\ \thetav_{k-1,k,i} &\coloneqq (\theta_{k-1,k,i,j})_{j=1}^M \in \RR^M \\ \theta_{k-1,k,i,j} &\coloneqq f_{k-1,k}(\x, i, j, \w_k) \in \RR. \end{align*} We therefore have P(Sk=j∣Sk−1=i)=pk(j∣i)=πk−1,k,i,j=[softargmax(θk−1,k,i)]j=exp⁡(θk−1,k,i,j)∑j′exp⁡(θk−1,k,i,j′)\begin{align*} \PP(S_k = j \mid S_{k-1} = i) &= p_k(j \mid i) \\ &= \pi_{k-1,k,i,j} \\ &= \left[\mathrm{softargmax}(\thetav_{k-1,k,i})\right]_j \\ &= \frac{\exp(\theta_{k-1,k,i,j})}{\sum_{j'} \exp(\theta_{k-1,k,i,j'})} \end{align*} and log⁡P(Sk=j∣Sk−1=i)=log⁡pk(j∣i)=θk−1,k,i,j−logsumexp(θk−1,k,i)=θk−1,k,i,j−log⁡∑j′exp⁡(θk−1,k,i,j′).\begin{align*} \log \PP(S_k = j \mid S_{k-1} = i) &= \log p_k(j \mid i) \\ &= \theta_{k-1,k,i,j} - \mathrm{logsumexp}(\thetav_{k-1,k,i}) \\ &= \theta_{k-1,k,i,j} - \log \sum_{j'} \exp(\theta_{k-1,k,i,j'}). \end{align*} We emphasize that because k−1k-1 and kk are always consecutive, the representation θk−1,k,i,j\theta_{k-1,k,i,j} is inefficient; we could use θk,i,j\theta_{k,i,j} instead. Our notation is designed for consistency with Markov random fields.

10.4.2 Time-homogeneous Markov chains

A time-homogeneous discrete-time Markov chain corresponds to the case when the distribution of SkS_k given Sk−1S_{k-1} is the same regardless of kk: p1=⋯=pK=p. p_1 = \dots = p_K = p. The finite-space case corresponds to when each Sk∈SS_k \in \cS can take a finite set of values S={v1,…,vM}\cS = \{\v_1,\dots,\v_M\} and P(Sk=vj∣Sk−1=vi)=p(vj∣vi)=πi,j, \PP(S_k=\v_j \mid S_{k-1} = \v_i) = p(\v_j|\v_i) = \pi_{i,j}, where πi,j∈[0,1]\pi_{i,j} \in [0,1] is the transition probability from vi\v_i to vj\v_j. Because the set S={v1,…,vM}\cS = \{\v_1,\dots,\v_M\} is discrete, we can always identify it with {1,…,M}\{1,\dots,M\}. That is, we can instead write P(Sk=j∣Sk−1=i)=p(j∣i)=πi,j. \PP(S_k=j \mid S_{k-1} = i) = p(j|i) = \pi_{i,j}.

10.4.3 Higher-order Markov chains

More generally, a nthn^{\text{th}}-order Markov chain may depend, not only on the last variable, but on the last nn variables, P(Sk=sk∣Sk−1=sk−1,…,S1=s1)=P(Sk=sk∣Sk−1=sk−1,…,Sk−n=sk−n)=pk(sk∣sk−1,…,sk−n).\begin{align*} &\PP(S_k=\s_k \mid S_{k-1}=\s_{k-1}, \dots, S_1=\s_1) \\ = &\PP(S_k=\s_k \mid S_{k-1}=\s_{k-1}, \dots, S_{k-n}=\s_{k-n}) \\ = &p_k(\s_k|\s_{k-1},\dots,\s_{k-n}). \end{align*} Autoregressive models such as Transformers (Section 4.8) can be seen as specifying a higher-order Markov chain, with a context window of size nn. The larger context makes exact inference using dynamic programming computationally intractable. This is why practitioners use beam search or ancestral sampling (Section 10.5.3) instead.

10.5 Bayesian networks

In this section, we briefly review Bayesian networks. Our notation is chosen to emphasize the analogies with computation graphs.

10.5.1 Expressing variable dependencies using DAGs

Markov chains and more generally higher-order Markov chains are a special case of Bayesian network. Similarly to computation graphs reviewed in Section 8.3, variable dependencies can be expressed using a directed acyclic graph (DAG) G=(V,E)\cG = (\cV, \cE), where the vertices V={1,…,K}\cV = \{1, \dots, K\} represent variables and edges E\cE represent variable dependencies. The set {i1,…,ink}=pa⁡(k)⊆V\{i_1, \dots, i_{n_k}\} = \parent(k) \subseteq \cV, where nk≔∣pa⁡(k)∣n_k \coloneqq |\parent(k)|, indicates the variables Si1,…,SinkS_{i_1},\dots,S_{i_{n_k}} that SkS_k depends on. This defines a partially ordered set (poset). For notational simplicity, we again assume without loss of generality that S0S_0 is deterministic. A computation graph is specified by functions f1,…,fKf_1,\dots,f_K in topological order. In analogy, a Bayesian network is specified by conditional probability distributions pkp_k of SkS_k given Spa⁡(k)S_{\parent(k)}. We can then define the generative process S0≔s0S1∼p1(⋅∣S0)S2∼p2(⋅∣Spa⁡(2))⋮SK∼pK(⋅∣Spa⁡(K)).\begin{align*} S_0 &\coloneqq \s_0 \\ S_1 &\sim p_1(\cdot \mid S_0) \\ S_2 &\sim p_2(\cdot \mid S_{\parent(2)}) \\ &\vdots \\ S_K &\sim p_K(\cdot \mid S_{\parent(K)}). \end{align*} Using the chain rule of probability and variable independencies expressed by the DAG, the joint probability distribution is then (assuming a topological order for S0,S1,…,SKS_0,S_1,\dots, S_K) P(S=s)≔P(S1=s1,…,SK=sK)=∏k=1KP(Sk=sk∣Spa⁡(k)=spa⁡(k))≔∏k=1Kpk(sk∣spa⁡(k)).\begin{align*} \PP(S = \s) &\coloneqq \PP(S_1=\s_1, \dots, S_K=\s_K) \\ &= \prod_{k=1}^K \PP(S_k = \s_k| S_{\parent(k)} = \s_{\parent(k)}) \\ &\coloneqq \prod_{k=1}^K p_k(\s_k|\s_{\parent(k)}). \end{align*} This representation is well suited to express causal relationships between random variables.

10.5.2 Parameterizing Bayesian networks

In a Bayesian framework, observed data, latent variables, parameters and noise variables are all treated as random variables. If the conditional distribution pkp_k associated to node kk depends on some parameters, they can be provided to pkp_k as conditioning, using parent nodes.

A Bayesian network is specified by the conditional distributions pkp_k. Therefore, unlike computation graphs, there is no notion of function fkf_k in a Bayesian network. However, the root nodes of the Bayesian network can be the output of a neural network. For instance, autoregressive models, such as RNNs or Transformers, specify the conditional probability distribution of a token given past tokens, and the chain rule of probability is used to obtain a probability distribution over entire sequences.

10.5.3 Ancestral sampling

A major advantage of Bayesian networks is that, provided that each conditional distribution pkp_k is normalized, the joint distribution of S=(S1,…,SK)S = (S_1, \dots, S_K) is automatically normalized. This means that we can very easily draw i.i.d. samples from the joint distribution, by following the generative process: we follow the topological order k=1,…,Kk= 1, \dots, K and on iteration kk we draw a value sk∼pk(⋅∣spa⁡(k))\s_k \sim p_k(\cdot|\s_{\parent(k)}) conditioned on the previous values spa⁡(k)\s_{\parent(k)}. This is known as ancestral sampling.

10.6 Markov random fields

10.6.1 Expressing factors using undirected graphs

A Markov random field (MRF), a.k.a. undirected graphical model, specifies a distribution that factorizes as P(S=s)=p(s)≔1Z∏C∈CψC(sC), \PP(S=\s) = p(\s) \coloneqq \frac{1}{Z} \prod_{\cC \in C} \psi_\cC(\s_\cC), where CC is the set of maximal cliques of G\cG, that is, subsets of V\cV that are fully connected, ZZ is a normalization constant defined by Z≔∑s∈S∏C∈CψC(sC), Z \coloneqq \sum_{\s \in \cS} \prod_{\cC \in C} \psi_\cC(\s_\cC), and ψC ⁣:SC→R+\psi_\cC \colon \cS_\cC \to \RR_+ is a potential function (a.k.a. compatibility function), with SC≔(Sj)j∈C\cS_\cC \coloneqq (\cS_j)_{j \in \cC}. According to the Hammersley-Clifford theorem, an MRF can be equivalently defined in terms of Markov properties; we refer the interested reader to Wainwright and Jordan (2008). For the sake of this chapter, the definition above is sufficient for our purposes.

Example 10.2 (Markov chains as Markov random fields)

For a chain, letting S=(S1,…,SK)S = (S_1, \dots, S_K) and s=(s1,…,sK)\s = (\s_1, \dots, \s_K), recall that P(S=s)=∏k=1Kpk(sk∣sk−1). \PP(S=\s) = \prod_{k=1}^K p_k(\s_k \mid \s_{k-1}). This is equivalent to an MRF with Z=1Z=1 (since a chain is automatically normalized), C≔{{0,1},{1,2},…,{K−1,K}} C \coloneqq \{\{0,1\}, \{1,2\}, \dots, \{K-1,K\}\} and with potential function ψ{k−1,k}(sk−1,sk)≔pk(sk∣sk−1). \psi_{\{k-1,k\}}(\s_{k-1},\s_k) \coloneqq p_k(\s_k|\s_{k-1}). More generally, a Bayesian network can be similarly written as an MRF by creating appropriate potential functions corresponding to the parents of each node.

10.6.2 MRFs as exponential family distributions

Let us define the potential functions ψC(sC;θC)≔exp⁡(⟨θC,ϕC(sC)⟩) \psi_\cC(\s_\cC; \thetav_\cC) \coloneqq \exp(\langle \thetav_\cC, \phi_\cC(\s_\cC) \rangle) for some sufficient statistic function ϕC ⁣:SC→ΘC\phi_\cC \colon \cS_\cC \to \Theta_\cC and parameters θC∈ΘC\thetav_\cC \in \Theta_\cC. Then, pθ(s)≔1Z(θ)∏C∈CψC(sC;θC)=1Z(θ)∏C∈Cexp⁡(⟨θC,ϕC(sC)⟩)=1Z(θ)exp⁡(∑C∈C⟨θC,ϕC(sC)⟩)=1Z(θ)exp⁡(⟨θ,ϕ(s)⟩)=exp⁡(⟨θ,ϕ(s)⟩−A(θ))\begin{align*} p_\thetav(\s) &\coloneqq \frac{1}{Z(\thetav)} \prod_{\cC \in C} \psi_\cC(\s_\cC; \thetav_\cC) \\ &= \frac{1}{Z(\thetav)} \prod_{\cC \in C} \exp(\langle \thetav_\cC, \phi_\cC(\s_\cC) \rangle) \\ &= \frac{1}{Z(\thetav)} \exp\left(\sum_{\cC \in C} \langle \thetav_\cC, \phi_\cC(\s_\cC) \rangle\right) \\ &= \frac{1}{Z(\thetav)} \exp\left(\langle \thetav, \phi(\s) \rangle\right) \\ &= \exp\left(\langle \thetav, \phi(\s) \rangle - A(\thetav)\right) \end{align*} where ϕ(s)≔(ϕC(sC))C∈Cθ≔(θC)C∈CZ(θ)≔∑s∈S∏C∈CψC(sC;θC)=∑s∈Sexp⁡(⟨θ,ϕ(s)⟩)A(θ)≔log⁡Z(θ).\begin{align*} \phi(\s) &\coloneqq (\phi_\cC(\s_\cC))_{\cC \in C} \\ \thetav &\coloneqq (\thetav_\cC)_{\cC \in C} \\ Z(\thetav) &\coloneqq \sum_{\s \in \cS} \prod_{\cC \in C} \psi_\cC(\s_\cC; \thetav_\cC) \\ &= \sum_{\s \in \cS} \exp\left(\langle \thetav, \phi(\s) \rangle\right) \\ A(\thetav) &\coloneqq \log Z(\thetav). \end{align*} Therefore, for this choice of potential functions, we can view an MRF as an exponential family distribution (Section 3.4) with natural parameters θ\thetav, sufficient statistic ϕ\phi and log-partition function A(θ)A(\thetav).

Example 10.3 (Ising model)

The Ising model is a classical example of MRF. Let Y=(Y1,…,YM)∈{0,1}MY = (Y_1, \dots, Y_M) \in \{0,1\}^M be an unordered collection of binary variables Yi∈{0,1}Y_i \in \{0,1\}. This forms a graph G=(V,E)\cG = (\cV, \cE), where V=[M]\cV = [M] and E⊆V2\cE \subseteq V^2, such that (i,j)∈E(i,j) \in \cE means that YiY_i interacts with YjY_j. In statistical physics, YiY_i may indicate the presence or absence of particles, or the orientation of magnets. In image processing, YiY_i may represent a black and white pixel. In multi-label classification, YiY_i may indicate the presence or absence of a label. The probability of y=(y1,…,yM)∈{0,1}M\y = (y_1, \dots, y_M) \in \{0,1\}^M is then P(Y=y)=pθ(y)=exp⁡(∑i∈Vθiyi+∑(i,j)∈Eθi,jyiyj−A(θ))=exp⁡(∑C∈C⟨θC,ϕC(y)⟩−A(θ)),\begin{align*} \PP(Y = \y) &= p_\thetav(\y) \\ &= \exp\left(\sum_{i \in \cV} \theta_i y_i + \sum_{(i,j) \in \cE} \theta_{i,j} y_i y_j - A(\thetav) \right) \\ &= \exp\left(\sum_{\cC \in C} \langle \thetav_\cC, \phi_\cC(\y) \rangle - A(\thetav) \right), \end{align*} where C≔V∪EC \coloneqq \cV \cup \cE and θ∈R∣V∣+∣E∣\thetav \in \RR^{|\cV| + |\cE|} is the concatenation of (θi)i∈V(\theta_i)_{i \in \cV} and (θi,j)(i,j)∈E(\theta_{i,j})_{(i,j) \in \cE}. These models are also known as Boltzmann machines in a neural network context. MAP inference in general Ising models is known to be NP-hard, but when the interaction weights θi,j\theta_{i,j} are non-negative, MAP inference can be reduced to graph cut algorithms (Greig et al., 1989). There are two ways the above equation can be extended. First, we can use higher-order interactions, such as yiyjyky_i y_j y_k for (i,j,k)∈V3(i,j,k) \in \cV^3. Second, we may want to use categorical variables, which leads to the Potts model.

10.6.3 Conditional random fields

Conditional random fields (Lafferty et al., 2001; Sutton et al., 2012) are a special case of Markov random field, in which a conditioning variable is explicitly incorporated. For example, when the goal is to predict a variable y\y conditioned on a variable x\x, CRFs are defined as P(Y=y∣X=x)=p(y∣x)=1Z(x)∏C∈CψC(yC,x). \PP(Y=\y \mid X = \x) = p(\y \mid \x) = \frac{1}{Z(\x)} \prod_{\cC \in C} \psi_\cC(\y_\cC, \x). Note that the potential functions ψC\psi_\cC are allowed to depend on the whole x\x, as x\x is just a conditioning variable.

10.6.4 Sampling

Contrary to Bayesian networks, MRFs require an explicit normalization constant ZZ. As a result, sampling from a distribution represented by a general MRF is usually more involved than for Bayesian networks. A commonly-used technique is Gibbs sampling.

10.7 Inference on chains

In this section, we review how to perform marginal inference and maximum a-posteriori inference on joint distributions of the form p(s1,…,sK)=1Z∏k=1Kψk(sk−1,sk), p(\s_1,\dots,\s_K) = \frac{1}{Z} \prod_{k=1}^K \psi_k(\s_{k-1},\s_k), where Z≔∑s∈S∏k=1Kψk(sk−1,sk) Z \coloneqq \sum_{\s \in \cS} \prod_{k=1}^K \psi_k(\s_{k-1},\s_k) and where we used ψk\psi_k as a shorthand for ψk−1,k\psi_{k-1,k}, since k−1k-1 and kk are consecutive. As explained in Example 10.2, this also includes Markov chains by setting ψk(sk−1,sk)≔pk(sk∣sk−1), \psi_k(\s_{k-1},\s_k) \coloneqq p_k(\s_k \mid \s_{k-1}), in which case Z=1Z=1.

10.7.1 The forward-backward algorithm

The key idea of the forward-backward algorithm is to use the distributivity of multiplication over addition to write Z=∑s1∈S1ψ1(s0,s1)∑s2∈S2ψ2(s1,s2)⋯∑sK∈SKψK(sK−1,sK). Z = \sum_{\s_1 \in \cS_1} \psi_1(\s_0, \s_1) \sum_{\s_2 \in \cS_2} \psi_2(\s_1, \s_2) \dots \sum_{\s_K \in \cS_K} \psi_K(\s_{K-1}, \s_K). We can compute these sums recursively, either forward or backward. Recalling the definitions of Ak−1\cA_{k-1} and Bk+1\cB_{k+1} in Eq. (10.1), we define the summations up to and down to kk, αk(sk)≔∑s1,…,sk−1∈Ak−1∏j=1kψj(sj−1,sj)=∑sk−1∈Sk−1ψk(sk−1,sk)⋯∑s1∈S1ψ2(s1,s2)ψ1(s0,s1)βk(sk)≔∑sk+1,…,sK∈Bk+1∏j=k+1Kψj(sj−1,sj)=∑sk+1∈Sk+1ψk+1(sk,sk+1)⋯∑sK∈SKψK(sK−1,sK).\begin{align*} \alpha_k(\s_k) &\coloneqq \sum_{\s_1,\dots,\s_{k-1} \in \cA_{k-1}} \prod_{j=1}^k \psi_j(\s_{j-1}, \s_j) \\ &= \sum_{\s_{k-1} \in \cS_{k-1}} \psi_k(\s_{k-1}, \s_k) \dots \sum_{\s_1 \in \cS_1} \psi_2(\s_1,\s_2) \psi_1(\s_0,\s_1) \\ \beta_k(\s_k) &\coloneqq \sum_{\s_{k+1},\dots,\s_K \in \cB_{k+1}} \prod_{j=k+1}^K \psi_j(\s_{j-1}, \s_j) \\ &= \sum_{\s_{k+1} \in \cS_{k+1}} \psi_{k+1}(\s_k,\s_{k+1}) \dots \sum_{\s_K \in \cS_K} \psi_K(\s_{K-1},\s_K). \end{align*} We can compute the two quantities by recursing forward and backward αk(sk)=∑sk−1∈Sk−1ψk(sk−1,sk)αk−1(sk−1)βk(sk)=∑sk+1∈Sk+1ψk+1(sk,sk+1)βk+1(sk+1)\begin{align*} \alpha_k(\s_k) &= \sum_{\s_{k-1} \in \cS_{k-1}} \psi_k(\s_{k-1},\s_k) \alpha_{k-1}(\s_{k-1}) \\ \beta_k(\s_k) &= \sum_{\s_{k+1} \in \cS_{k+1}} \psi_{k+1}(\s_k,\s_{k+1}) \beta_{k+1}(\s_{k+1}) \end{align*} where we defined the initializations α1(s1)≔ψ1(s0,s1)∀s1∈S1βK(sK)≔1∀sK∈SK.\begin{align*} \alpha_1(\s_1) &\coloneqq \psi_1(\s_0, \s_1) \quad \forall \s_1 \in \cS_1 \\ \beta_K(\s_K) &\coloneqq 1 \quad \forall \s_K \in \cS_K. \end{align*} The normalization term can then be computed by Z=∑sK∈SKαK(sK)βK(sK)=∑s1∈S1α1(s1)β1(s1)\begin{align*} Z = \sum_{\s_K \in \cS_K} \alpha_K(\s_K) \beta_K(\s_K) = \sum_{\s_1 \in \cS_1} \alpha_1(\s_1) \beta_1(\s_1) \end{align*} and the marginal probabilities by P(Sk=sk)=1Zαk(sk)βk(sk)P(Sk−1=sk−1,Sk=sk)=1Zαk−1(sk−1)ψk(sk−1,sk)βk(sk).\begin{align*} \PP(S_k = \s_k) &= \frac{1}{Z} \alpha_k(\s_k) \beta_k(\s_k)\\ \PP(S_{k-1}=\s_{k-1}, S_k=\s_k) &= \frac{1}{Z} \alpha_{k-1}(\s_{k-1}) \psi_k(\s_{k-1}, \s_k) \beta_k(\s_k). \end{align*} We can also compute the conditional probabilities by P(Sk=sk∣Sk−1=sk−1)=P(Sk−1=sk−1,Sk=sk)P(Sk−1=sk−1)=αk−1(sk−1)ψk(sk−1,sk)βk(sk)αk−1(sk−1)βk−1(sk−1)=ψk(sk−1,sk)βk(sk)βk−1(sk−1).\begin{align*} \PP(S_k=\s_k \mid S_{k-1}=\s_{k-1}) &= \frac{\PP(S_{k-1}=\s_{k-1}, S_k=\s_k)}{\PP(S_{k-1}=\s_{k-1})} \\ &= \frac{\alpha_{k-1}(\s_{k-1}) \psi_k(\s_{k-1}, \s_k) \beta_k(\s_k)}{ \alpha_{k-1}(\s_{k-1}) \beta_{k-1}(\s_{k-1})} \\ &= \frac{\psi_k(\s_{k-1}, \s_k) \beta_k(\s_k)}{ \beta_{k-1}(\s_{k-1})}. \end{align*} In practice, the two recursions are often implemented in the log-domain for numerical stability, log⁡αk(sk)=log⁡∑sk−1∈Sk−1exp⁡(log⁡ψk(sk−1,sk)+log⁡αk−1(sk−1))log⁡βk(sk)=log⁡∑sk+1∈Sk+1exp⁡(log⁡ψk+1(sk,sk+1)+log⁡βk+1(sk+1)).\begin{align*} \log \alpha_k(\s_k) &= \log \sum_{\s_{k-1} \in \cS_{k-1}} \exp(\log \psi_k(\s_{k-1},\s_k) + \log \alpha_{k-1}(\s_{k-1})) \\ \log \beta_k(\s_k) &= \log \sum_{\s_{k+1} \in \cS_{k+1}} \exp(\log \psi_{k+1}(\s_k,\s_{k+1}) + \log \beta_{k+1}(\s_{k+1})). \end{align*} We recognize the log-sum-exp operator, which can be implemented in a numerically stable way (Section 4.4.2). The overall dynamic programming procedure, a.k.a. forward-backward algorithm (Baum and Petrie, 1966; Rabiner, 1989), is summarized in Algorithm 10.1. We notice that the forward and backward passes are actually independent of each other, and can therefore be performed in parallel.

Algorithm 10.1: Marginal inference on a chain

Potential functions: ψ1,…,ψK\psi_1, \dots, \psi_K

Input: s0\s_0

Initialize α1(s1)≔ψ1(s0,s1) ∀s1∈S1\alpha_1(\s_1) \coloneqq \psi_1(\s_0, \s_1) ~ \forall \s_1 \in \cS_1

for k≔2,…,Kk\coloneqq 2, \ldots, K do ▷  Forward pass

for sk∈Sk\s_k \in \cS_k do

αk(sk)≔∑sk−1∈Sk−1ψk(sk−1,sk)αk−1(sk−1)\alpha_k(\s_k) \coloneqq \displaystyle{\sum_{\s_{k-1} \in \cS_{k-1}} \psi_k(\s_{k-1},\s_k) \alpha_{k-1}(\s_{k-1})}

Initialize βK(sK)≔1 ∀sK∈SK\beta_{K}(\s_K) \coloneqq 1 ~ \forall \s_K \in \cS_K

for k≔K−1,…,1k\coloneqq K-1, \ldots, 1 do ▷  Backward pass

for sk∈Sk\s_k \in \cS_k do

βk(sk)≔∑sk+1∈Sk+1ψk+1(sk,sk+1)βk+1(sk+1)\beta_k(\s_k) \coloneqq \displaystyle{\sum_{\s_{k+1} \in \cS_{k+1}} \psi_{k+1}(\s_k,\s_{k+1}) \beta_{k+1}(\s_{k+1})}

Compute Z=∑sK∈SKαK(sK)βK(sK)=∑sK∈SKαK(sK)Z = \displaystyle{\sum_{\s_K \in \cS_K} \alpha_K(\s_K) \beta_K(\s_K)} = \displaystyle{\sum_{\s_K \in \cS_K} \alpha_K(\s_K)}

Outputs: ∀k∈[K]\forall k \in [K]:

P(Sk=sk)=1Zαk(sk)βk(sk)\PP(S_k = \s_k) = \frac{1}{Z} \alpha_k(\s_k) \beta_k(\s_k)

P(Sk−1=sk−1,Sk=sk)=1Zαk−1(sk−1)ψk(sk−1,sk)βk(sk)\PP(S_{k-1}=\s_{k-1}, S_k=\s_k) = \frac{1}{Z} \alpha_{k-1}(\s_{k-1}) \psi_k(\s_{k-1}, \s_k) \beta_k(\s_k)

10.7.2 The Viterbi algorithm

Similarly, using the distributivity of multiplication over maximization, max⁡s1∈S1,…,sK∈SK∏k=1Kψk(sk−1,sk)=max⁡sK∈SKmax⁡sK−1∈SK−1ψK(sK−1,sK)…max⁡s1∈S1ψ2(s1,s2)ψ1(s0,s1).\begin{align*} &\max_{\s_1 \in \cS_1, \dots, \s_K \in \cS_K} \prod_{k=1}^K \psi_k(\s_{k-1},\s_k) \\ =& \max_{\s_K \in \cS_K} \max_{\s_{K-1} \in \cS_{K-1}} \psi_K(\s_{K-1},\s_K) \dots \max_{\s_1 \in \cS_1} \psi_2(\s_1,\s_2) \psi_1(\s_0,\s_1). \end{align*} Let us define for k∈[K]k \in [K] δk(sk)≔max⁡sk−1∈Sk−1ψk(sk−1,sk)…max⁡s1∈S1ψ2(s1,s2)ψ1(s0,s1). \delta_k(\s_k) \coloneqq \max_{\s_{k-1} \in \cS_{k-1}} \psi_k(\s_{k-1},\s_k) \dots \max_{\s_1 \in \cS_1} \psi_2(\s_1,\s_2) \psi_1(\s_0,\s_1). We can compute these quantities recursively, since for k∈[K]k \in [K] δk(sk)=max⁡sk−1∈Sk−1ψk(sk−1,sk)δk−1(sk−1), \delta_k(\s_k) = \max_{\s_{k-1} \in \cS_{k-1}} \psi_k(\s_{k-1},\s_k) \delta_{k-1}(\s_{k-1}), with δ1(s1)≔ψ1(s0,s1)\delta_1(\s_1) \coloneqq \psi_1(\s_0, \s_1). We finally have max⁡s1∈S1,…,sK∈SKp(s1,…,sK)=1Zmax⁡sK∈SKδK(sK). \max_{\s_1 \in \cS_1, \dots, \s_K \in \cS_K} p(\s_1, \dots, \s_K) = \frac{1}{Z} \max_{\s_K \in \cS_K} \delta_K(\s_K). In practice, for numerical stability, we often implement the forward recursion in the log-domain. Using the fact that the logarithm is monotonic, we indeed have for all k∈[K]k \in [K] log⁡δk(sk)=max⁡sk−1∈Sk−1log⁡ψk(sk−1,sk)+log⁡δk−1(sk−1). \log \delta_k(\s_k) = \max_{\s_{k-1} \in \cS_{k-1}} \log \psi_k(\s_{k-1},\s_k) + \log \delta_{k-1}(\s_{k-1}). To enable efficient backtracking, during the forward pass, we compute qk(sk)≔arg max⁡sk−1∈Sk−1ψk(sk−1,sk)δk−1(sk−1) q_k(\s_k) \coloneqq \argmax_{\s_{k-1} \in \cS_{k-1}} \psi_k(\s_{k-1},\s_k) \delta_{k-1}(\s_{k-1}) which can be thought of as backpointers from sk⋆\s_k^\star to sk−1⋆\s_{k-1}^\star.

The resulting dynamic programming procedure, a.k.a. Viterbi algorithm (Forney, 1973; Viterbi, 1967), is summarized in Algorithm 10.2.

Algorithm 10.2: MAP inference on a chain

Potential functions: ψ1,…,ψK\psi_1, \dots, \psi_K

Input: s0\s_0

Initialize δ1(s1)≔ψ1(s0,s1) ∀s1∈S1\delta_1(\s_1) \coloneqq \psi_1(\s_0, \s_1) ~ \forall \s_1 \in \cS_1

for k≔2,…,Kk\coloneqq 2, \ldots, K do ▷  Forward pass

for sk∈Sk\s_k \in \cS_k do

δk(sk)≔max⁡sk−1∈Sk−1ψk(sk−1,sk)δk−1(sk−1)\delta_k(\s_k) \coloneqq \displaystyle{\max_{\s_{k-1} \in \cS_{k-1}} \psi_k(\s_{k-1},\s_k) \delta_{k-1}(\s_{k-1})}

qk(sk)≔arg max⁡sk−1∈Sk−1ψk(sk−1,sk)δk−1(sk−1)q_k(\s_k) \coloneqq \displaystyle{\argmax_{\s_{k-1} \in \cS_{k-1}} \psi_k(\s_{k-1},\s_k) \delta_{k-1}(\s_{k-1})}

δ⋆≔max⁡sK∈SKδK(sK)\delta^\star \coloneqq \displaystyle{\max_{\s_K \in \cS_K} \delta_K(\s_K)}

sK⋆≔arg max⁡sK∈SKδK(sK)\s_K^\star \coloneqq \displaystyle{\argmax_{\s_K \in \cS_K} \delta_K(\s_K)}

for k≔K−1,…,1k\coloneqq K-1, \ldots, 1 do ▷  Backtracking

sk⋆≔qk+1(sk+1⋆)\s_{k}^\star \coloneqq q_{k+1}(\s_{k+1}^\star)

Outputs: max⁡s1∈S1,…,sK∈SKp(s1,…,sK)∝δ⋆\displaystyle{\max_{\s_1 \in \cS_1, \dots, \s_K \in \cS_K} p(\s_1, \dots, \s_K) \propto \delta^\star}

arg max⁡s1∈S1,…,sK∈SKp(s1,…,sK)=(s1⋆,…,sK⋆)\displaystyle{\argmax_{\s_1 \in \cS_1, \dots, \s_K \in \cS_K} p(\s_1, \dots, \s_K) = (\s_1^\star, \dots, \s_K^\star)}

10.8 Inference on trees

More generally, efficient inference based on dynamic programming can be performed when dependencies between variables are expressed using a tree or polytree. The resulting marginal inference and MAP inference algorithms are often referred to as the sum-product and max-sum algorithms. The sum-product algorithm is also known as belief propagation or message passing, since it can be interpreted as propagating “local messages” through the graph. See for instance (Wainwright and Jordan, 2008, Section 2.5.1) for more details.

10.9 Inference as differentiation

In this section, we review the profound connections between differentiating the log-partition function of an exponential family distribution on one hand, and performing marginal inference (as well as maximum a-posteriori inference in the zero-temperature limit) on the other hand.

10.9.1 Inference as gradient of the log-partition

We first discuss a well-known fact in the graphical model literature: when using a binary encoding as the sufficient statistic ϕ\phi in an exponential family distribution, the gradient ∇A(θ)\nabla A(\thetav) of the log-partition A(θ)A(\thetav) gathers all the marginals (Wainwright and Jordan, 2008).

To see why, recall from Section 3.4 the definition of an exponential family distribution pθ(s)=h(s)exp⁡[⟨θ,ϕ(s)⟩−A(θ)]\begin{align*} p_\thetav(\s) &= h(\s) \exp\left[\langle \thetav, \phi(\s) \rangle - A(\thetav)\right] \end{align*} and of its log-partition A(θ)≔log⁡∑s∈Sh(s)exp⁡[⟨θ,ϕ(s)⟩]. A(\thetav) \coloneqq \log \sum_{\s \in \cS} h(\s) \exp\left[\langle \thetav, \phi(\s) \rangle\right]. From Proposition 3.2, μ(θ)≔∇A(θ)=EY∼pθ[ϕ(Y)]∈M.\begin{align*} \mu(\thetav) \coloneqq \nabla A(\thetav) = \EE_{Y \sim p_\thetav}[\phi(Y)] \in \cM. \end{align*} Therefore, with the binary encodings in Eq. (10.2) and Eq. (10.3), P(Sk=vi)=[∇A(θ)]k,iP(Sk=vi,Sl=vj)=[∇A(θ)]k,l,i,j.\begin{align*} \PP(S_k = \v_i) &= [\nabla A(\thetav)]_{k,i} \\ \PP(S_k = \v_i, S_l = \v_j) &= [\nabla A(\thetav)]_{k,l,i,j}. \end{align*} Put differently, if we have an efficient algorithm for computing A(θ)A(\thetav), we can perform reverse-mode autodiff on A(θ)A(\thetav) to obtain ∇A(θ)\nabla A(\thetav), and therefore obtain the marginal probabilities. Following Section 8.3.3, the complexity of computing all marginal probabilities is therefore roughly the same as that of computing A(θ)A(\thetav).

In the special case of chains, we obtain P(Sk=vi)=[∇A(θ)]k,i=1Zαk(vi)βk(vi)P(Sk−1=vi,Sk=vj)=[∇A(θ)]k−1,k,i,j=1Zαk−1(vi)ψk(vi,vj)βk(vj),\begin{align*} \PP(S_k = \v_i) &= [\nabla A(\thetav)]_{k,i} = \frac{1}{Z} \alpha_k(\v_i) \beta_k(\v_i) \\ \PP(S_{k-1} = \v_i, S_k = \v_j) &= [\nabla A(\thetav)]_{k-1,k,i,j} = \frac{1}{Z} \alpha_{k-1}(\v_i) \psi_k(\v_i, \v_j) \beta_k(\v_j), \end{align*} where we left the dependence of ZZ, α\alpha and β\beta on θ\thetav implicit.

If we define Aε(θ)≔εA(θ/ε)A_\varepsilon(\thetav) \coloneqq \varepsilon A(\thetav / \varepsilon), in the zero-temperature limit ε→0\varepsilon \to 0, we obtain that μ(θ)\muv(\thetav) is a binary encoding of the mode, i.e., of the maximum a-posteriori inference solution.

We now show i) how to unify the forward pass of the forward-backward and Viterbi algorithms using semirings and softmax operators ii) how to compute the gradient of the log-partition using backpropagation.

10.9.2 Semirings and softmax operators

The forward passes in the forward-backward and Viterbi algorithms are clearly similar. In fact, they can be formally linked to each other using semirings.

Definition 10.1 (Semiring)

A semiring is a set K\KK equipped with two binary operations (⊕,⊗)(\oplus, \otimes) such that

  • ⊕\oplus is associative and commutative,

  • ⊗\otimes is associative and distributive over ⊕\oplus,

  • ⊕\oplus and ⊗\otimes have identity elements 0ˉ\bar{0} and 1ˉ\bar{1}, respectively.

We use the notations ⊕\oplus, ⊗\otimes, 0ˉ\bar{0} and 1ˉ\bar{1} to clearly distinguish them from the classical addition, multiplication, 00 and 11.

We recall the following laws for binary operations:

A set equipped with a binary operation supporting associativity and an identity element is called a monoid. A monoid such that every element has an inverse element is called a group. The difference between a ring and a semiring is that the latter only requires (K,⊕)(\KK, \oplus) and (K,⊗)(\KK, \otimes) to be monoids, not groups.

Equipped with these definitions, we can interpret the forward passes in the Viterbi and forward-backward algorithms as follows:

It can be checked that indeed maxε\text{max}_\varepsilon is commutative, associative, and addition is distributive over maxε\text{max}_\varepsilon. Its identity element is −∞-\infty. By associativity, maxε(a1,maxε(a2,a3))=logsumexpε(a1,a2,a3)=εlog⁡∑iexp⁡(ai/ε).\begin{align*} \text{max}_\varepsilon(a_1, \text{max}_\varepsilon(a_2, a_3)) &= \mathrm{logsumexp}_\varepsilon(a_1,a_2,a_3) \\ &= \varepsilon \log \sum_i \exp(a_i / \varepsilon). \end{align*} In contrast, note that the sparsemax in Section 13.5 is not associative.

Thanks to associativity, we can introduce the shorthand notations maxεv∈V f(v)≔εlog⁡∑v∈Vexp⁡(f(v)/ε)∈R. \underset{\v \in \cV}{\text{max}_\varepsilon} ~ f(\v) \coloneqq \varepsilon \log \sum_{\v \in \cV} \exp(f(\v) / \varepsilon) \in \RR. and argmaxεv∈V f(v)≔(exp⁡(f(v′)/ε)/∑v∈Vexp⁡(f(v)/ε))v′∈V∈P(V). \underset{\v \in \cV}{\text{argmax}_\varepsilon} ~ f(\v) \coloneqq \left(\exp(f(\v') / \varepsilon) / \sum_{\v \in \cV} \exp(f(\v) / \varepsilon) \right)_{\v' \in \cV} \in \cP(\cV).

Many algorithms can be generalized thanks to the use of semirings; see among others Aji and McEliece (2000; Mohri et al., 2008). The distributive and associative properties play a key role in breaking down large problems into smaller ones (Verdu and Poor, 1987).

10.9.3 Inference as backpropagation

In this section, we show that, algorithmically, backtracking is recovered as a special case of backpropagation. See also (Eisner, 2016; Mensch and Blondel, 2018).

For notation simplicity, we assume S0={1}\cS_0 = \{1\} and Sk={1,…,M}\cS_k = \{1, \dots, M\} for all k∈[K]k \in [K]. We focus on the case log⁡ψk(i,j)=⟨θk,ϕk(i,j)⟩=θk,i,j. \log \psi_k(i,j) = \langle \thetav_k,\phi_k(i,j) \rangle = \theta_{k,i,j}. We also introduce the shorthands a1,j≔log⁡α1(j)=θ1,1,jak,j≔log⁡αk(j)=maxεi∈[M] θk,i,j+ak−1,i\begin{align*} a_{1,j} &\coloneqq \log \alpha_1(j) = \theta_{1,1,j} \\ a_{k,j} &\coloneqq \log \alpha_k(j) = \underset{i \in [M]}{\text{max}_\varepsilon} ~ \theta_{k,i,j} + a_{k-1,i} \end{align*} and qk,j≔argmaxεi∈[M] θk,i,j+ak−1,i.\begin{align*} q_{k,j} \coloneqq \underset{i \in [M]}{\text{argmax}_\varepsilon} ~ \theta_{k,i,j} + a_{k-1,i}. \end{align*}

Our goal is to compute the gradient w.r.t. θ∈RK×M×M\thetav \in \RR^{K \times M \times M} of log⁡Z=A=maxεj∈[M] aK,j. \log Z = A = \underset{j \in [M]}{\text{max}_\varepsilon} ~ a_{K,j}. The soft argmax counterpart of this quantity is Q≔argmaxεj∈[M] aK,j∈△M, Q \coloneqq \underset{j \in [M]}{\text{argmax}_\varepsilon} ~ a_{K,j} \in \triangle^M, where we used P([M])=△M\cP([M]) = \triangle^M.

Computing the gradient of AA is similar to computing the gradient of a feedforward network, in the sense that θk\thetav_k influences not only aka_k but also ak+1,…,aKa_{k+1},\dots,a_K. Let us introduce the adjoint variable rk,i≔∂A∂ak,i, r_{k,i} \coloneqq \partialfrac{A}{a_{k,i}}, which we initialize as rK,i=∂A∂aK,i=Qi. r_{K,i} = \partialfrac{A}{a_{K,i}} = Q_i. Since θk,i,j\theta_{k,i,j} directly influences ak,ja_{k,j}, we have for k∈[K]k \in [K], i∈[M]i \in [M] and j∈[M]j \in [M] μk,i,j≔∂A∂θk,i,j=∂A∂ak,j⋅∂ak,j∂θk,i,j=rk,j⋅qk,j,i.\begin{align*} \mu_{k,i,j} &\coloneqq \partialfrac{A}{\theta_{k,i,j}} \\ &= \partialfrac{A}{a_{k,j}} \cdot \partialfrac{a_{k,j}}{\theta_{k,i,j}} \\ &= r_{k,j} \cdot q_{k,j,i}. \end{align*} Since ak,ia_{k,i} directly influences ak+1,ja_{k+1,j} for j∈[M]j \in [M], we have for k∈{1,…,K−1}k \in \{1, \dots, K-1\} and i∈[M]i \in [M] rk,i=∂A∂ak,i=∑j∈[M]∂A∂ak+1,j∂ak+1,j∂ak,i=∑j∈[M]rk+1,jqk+1,j,i=∑j∈[M]μk+1,i,j.\begin{align*} r_{k,i} = \partialfrac{A}{a_{k,i}} &= \sum_{j \in [M]} \partialfrac{A}{a_{k+1,j}} \partialfrac{a_{k+1,j}}{a_{k,i}} \\ &= \sum_{j \in [M]} r_{k+1,j} q_{k+1,j,i} \\ &= \sum_{j \in [M]} \mu_{k+1,i,j}. \end{align*}

We summarize the procedure in Algorithm 10.3. The forward pass uses the softmax operator maxε\text{max}_\varepsilon and the softargmax operator argmaxε\text{argmax}_\varepsilon. In the hard max case, in Algorithm 10.2, we used qq to store backpointers from integer to integer. In the soft max case, in Algorithm 10.3, we used q\q to store soft backpointers, that is, discrete probability distributions. In the zero-temperature limit, backpropagation outputs a binary encoding of the solution of backtracking.

Algorithm 10.3: Inference on a chain as backprop with max operators

Input: θ∈RK×M×M\thetav \in \RR^{K \times M \times M}

Max operator: max⁡ε\max_\varepsilon

Initialize a1,j≔θ1,1,j ∀j∈[M]a_{1,j} \coloneqq \theta_{1,1,j} ~ \forall j \in [M]

for k≔2,…,Kk\coloneqq 2, \ldots, K do ▷  Forward pass

for j∈[M]j \in [M] do

ak,j≔maxεi∈[M] θk,i,j+ak−1,i∈Ra_{k,j} \coloneqq \underset{i \in [M]}{\text{max}_\varepsilon} ~ \theta_{k,i,j} + a_{k-1,i} \in \RR

qk,j≔argmaxεi∈[M] θk,i,j+ak−1,i∈△M\q_{k,j} \coloneqq \underset{i \in [M]}{\text{argmax}_\varepsilon} ~ \theta_{k,i,j} + a_{k-1,i} \in \triangle^M

A≔maxεi∈[M] aK,i∈RA \coloneqq \underset{i \in [M]}{\text{max}_\varepsilon} ~ a_{K,i} \in \RR

Q≔argmaxεi∈[M] aK,i∈△M\bm{Q} \coloneqq \underset{i \in [M]}{\text{argmax}_\varepsilon} ~ a_{K,i} \in \triangle^M

Initialize rK,j=Qjr_{K,j} = Q_j   ∀j∈[K]\forall j \in [K]

for k≔K−1,…,1k\coloneqq K-1, \ldots, 1 do ▷  Backward pass

for i∈[M]i \in [M] do

for j∈[M]j \in [M] do

μk+1,i,j=rk+1,j⋅qk+1,j,i\mu_{k+1,i,j} = r_{k+1,j} \cdot q_{k+1,j,i}

rk,i←rk,i+μk+1,i,jr_{k,i} \leftarrow r_{k,i} + \mu_{k+1,i,j}

Outputs: max⁡εi1,…,iK∈[M]Kθ1,1,i1+∑k=2Kθk,ik−1,ik=A\underset{i_1,\dots,i_K \in [M]^K}{\max_\varepsilon} \theta_{1,1,i_1} + \sum_{k=2}^K \theta_{k,i_{k-1},i_k} = A, ∇A(θ)=μ\nabla A(\thetav) = \mu

10.10 Summary