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 A1 and A2, it reduces to the product ruleP(A1∩A2)=P(A2∣A1)P(A1). For two discrete random variables S1 and S2, using the events A1:={S1=s1} and A2:={S2=s2}, the product rule becomes P(S1=s1,S2=s2)=P(S2=s2∣S1=s1)P(S1=s1). More generally, using the product rule, we have for K events P(A1∩…∩AK)=P(AK∣A1∩…∩AK−1)P(A1∩…∩AK−1). Applying the product rule one more time, we have P(A1∩…∩AK−1)=P(AK−1∣A1∩…∩AK−2)P(A1∩…∩AK−2). Repeating the process recursively, we arrive at the chain rule of probabilityP(A1∩…∩AK)=j=1∏KP(Aj∣A1∩⋯∩Aj−1)=j=1∏KP(Aji=1⋂j−1Ai). For K discrete random variables Sj, using the events Aj:={Sj=sj}, the chain rule of probability becomes P(S1=s1,…,SK=sK)=j=1∏KP(Sj=sj∣S1=s1,…,Sj−1=sj−1). Importantly, this factorization holds without any independence assumption on the variables S1,…,SK. 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 A and B are independent, then P(A∣B)=P(A). Similarly, if two random variables S1 and S2 are independent, then P(S2=s2∣S1=s1)=P(S2=s2). More generally, if we work with K variables S1,…,SK, some variables may depend on each other, while others may not. To simplify the notation, given a set C, we define the shorthands SCsC:=(Si:i∈C):=(si:i∈C). We say that a variable Sj is independent of SDconditioned on SC, with C∩D=∅, if for any sj,sC,sDP(Sj=sj∣SC=sC,SD=sD)=P(Sj=sj∣SC=sC).
10.3 Inference problems
10.3.1 Joint probability distributions
We consider a collection of K variables s:=(s1,…,sK), potentially ordered or unordered. Each s belongs to the Cartesian productS:=S1×⋯×SK. Throughout this chapter, we assume that the sets Sk are discrete for concreteness, with Sk:={v1,…,vMk}. Note that because Sk is discrete, we can always identify it with {1,…,Mk}. A graphical model specifies a joint probability distributionP(S=s)=P(S1=s1,…,SK=sK)=p(s)=p(s1,…,sK), where p is the probability mass function of the joint probability distribution. Summing over the Cartesian product of all possible configurations, we obtain s∈S∑p(s)=s1,…,sK∈S∑p(s1,…,sK)=1. As we shall see, the graph of a graphical model encodes the dependencies between the variables (S1,…,SK) 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), P(S1=s1,…,SK=sK)=p(s1,…,sK). It is also common to compute the log-likelihood, logP(S1=s1,…,SK=sK)=logp(s1,…,sK).
10.3.3 Maximum a-posteriori inference
Another common task is to compute the most likely configuration, s1∈S1,…,sK∈SKargmaxp(s1,…,sK). 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=sk. To do so, we define the Cartesian product Ck(sk):=Ak−1S1×⋯×Sk−1×{sk}×Bk+1Sk+1×⋯×SK.(10.1) Summing over all variables but Sk, we obtain the marginal probability of Sk=sk as P(Sk=sk)=s1,…,sK∈Ck(sk)∑p(s1,…,sK)=s1,…,sk−1∈Ak−1∑sk+1,…,sK∈Bk+1∑p(s1,…,sK). Defining similarly Ck,l(sk,sl):=S1×⋯×{sk}×⋯×{sl}×⋯×SK, we obtain P(Sk=sk,Sl=sl)=s1,…,sK∈Ck,l(sk,sl)∑p(s1,…,sK). In particular, we may want to compute the marginal probability of two consecutive variables, P(Sk−1=sk−1,Sk=sk).
Another common operation is to compute the expectation of ϕ(S) under a distribution p. It is defined by μ:=ES∼p[ϕ(S)]=s∈S∑p(s)ϕ(s)∈M. For the expectation under pθ, we write μ(θ):=ES∼pθ[ϕ(S)]=s∈S∑pθ(s)ϕ(s)∈M. In exponential family distributions (Section 3.4), the function ϕ is called a statistic. It decomposes as ϕ(s):=(ϕC(sC))C∈C, where C⊆[K]. Intuitively, ϕ(s) can be thought of as an encoding or embedding of 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∈S∑p(s)ϕC(sC).
Convex hull
The mean μ belongs to the convex hull of ϕ(S):={ϕ(s):s∈S}, M:=conv(ϕ(S)):={s∈S∑p(s)ϕ(s):p∈P(S)}, where P(S) is the set of all possible probability distributions over S. In other words, M is the set of all possible convex combinations of ϕ(s) for s∈S. The vertices of M are all the s∈S.
Case of binary encodings: the marginal polytope
In the special case of a discrete set Sk={v1,…,vM} and of a binary encoding (indicator function) ϕ(s), the set M is called the marginal polytope(Wainwright and Jordan, 2008), because each point μ∈M contains marginal probabilities. To see why, consider the unary potential [ϕ(s)]k,i=[ϕk(sk)]i=I(sk=vi)(10.2) where I(p):=1 if p is true, 0 otherwise. We then obtain the marginal probability of Sk=vi, [μ]k,i=ES[ϕ(S)k,i]=ESk[ϕk(Sk)i]=ESk[I(Sk=vi)]=sk∈Sk∑P(Sk=sk)I(sk=vi)=P(Sk=vi). Likewise, consider the pairwise potential [ϕ(s)]k,l,i,j=[ϕk,l(sk,sl)]i,j=I(sk=vi,sl=vj).(10.3) We then obtain the marginal probability of Sk=vi and Sl=vj, [μ]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∈Sl∑P(Sk=sk,Sl=sl)I(sk=vi,sl=vj)=P(Sk=vi,Sl=vj). 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∣) time. In particular, if ∣Sk∣=M∀k∈[K], brute force takes O(MK) 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,…,SK, a simple example of conditional independence is when each variable Sk∈Sk only depends on the previous variable Sk−1∈Sk−1, that is, P(Sk=sk∣Sk−1=sk−1,…,S1=s1)=P(Sk=sk∣Sk−1=sk−1):=pk(sk∣sk−1). 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 fk, that take sk−1 as input and output sk. In analogy, a Markov chain is specified by the conditional probability distributions pk of Sk given Sk−1. We can then define the generative processS0S1S2SK:=s0∼p1(⋅∣S0)∼p2(⋅∣S1)⋮∼pK(⋅∣SK−1). Strictly speaking, we should write Sk∣Sk−1∼pk(⋅∣Sk−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 S0 is deterministic (if this is not the case, we can always move S0 to S1 and add a dummy variable as S0). That is, P(S0=s0)=p0(s0):=1 and S0:={s0}. This amounts to setting the initial distribution of S1 as P(S1=s1):=P(S0=s0)P(S1=s1∣S0=s0)=P(S1=s1∣S0=s0). We can then compute the joint probability of the Markov chain by P(S1=s1,…,SK=sK)=p(s1,…,sK)=k=1∏KP(Sk=sk∣Sk−1=sk−1)=k=1∏Kpk(sk∣sk−1), where we left the dependence on s0 implicit, since p0(s0)=1. A Markov chain with Sk={1,2,3} is illustrated in Fig. 10.1. A chain defines a totally ordered set{1,…,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, a sequence of length K, where each Sk belongs to Sk={1,…,M}. In natural language processing, this task is called sequence tagging. We can define Sk∼Categorical(πk−1,k,Sk−1) where πk−1,k,iθk−1,k,iθk−1,k,i,j:=softargmax(θk−1,k,i)∈△M=(πk−1,k,i,j)j=1M:=(θk−1,k,i,j)j=1M∈RM:=fk−1,k(x,i,j,wk)∈R. We therefore have P(Sk=j∣Sk−1=i)=pk(j∣i)=πk−1,k,i,j=[softargmax(θk−1,k,i)]j=∑j′exp(θk−1,k,i,j′)exp(θk−1,k,i,j) and logP(Sk=j∣Sk−1=i)=logpk(j∣i)=θk−1,k,i,j−logsumexp(θk−1,k,i)=θk−1,k,i,j−logj′∑exp(θk−1,k,i,j′). We emphasize that because k−1 and k are always consecutive, the representation θk−1,k,i,j is inefficient; we could use θ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 Sk given Sk−1 is the same regardless of k: p1=⋯=pK=p. The finite-space case corresponds to when each Sk∈S can take a finite set of values S={v1,…,vM} and P(Sk=vj∣Sk−1=vi)=p(vj∣vi)=πi,j, where πi,j∈[0,1] is the transition probability from vi to vj. Because the set S={v1,…,vM} is discrete, we can always identify it with {1,…,M}. That is, we can instead write P(Sk=j∣Sk−1=i)=p(j∣i)=πi,j.
10.4.3 Higher-order Markov chains
More generally, a nth-order Markov chain may depend, not only on the last variable, but on the last n 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). Autoregressive models such as Transformers (Section 4.8) can be seen as specifying a higher-order Markov chain, with a context window of size n. 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), where the vertices V={1,…,K} represent variables and edges E represent variable dependencies. The set {i1,…,ink}=pa(k)⊆V, where nk:=∣pa(k)∣, indicates the variables Si1,…,Sink that Sk depends on. This defines a partially ordered set (poset). For notational simplicity, we again assume without loss of generality that S0 is deterministic. A computation graph is specified by functions f1,…,fK in topological order. In analogy, a Bayesian network is specified by conditional probability distributions pk of Sk given Spa(k). We can then define the generative processS0S1S2SK:=s0∼p1(⋅∣S0)∼p2(⋅∣Spa(2))⋮∼pK(⋅∣Spa(K)). 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,…,SK) P(S=s):=P(S1=s1,…,SK=sK)=k=1∏KP(Sk=sk∣Spa(k)=spa(k)):=k=1∏Kpk(sk∣spa(k)). 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 pk associated to node k depends on some parameters, they can be provided to pk as conditioning, using parent nodes.
A Bayesian network is specified by the conditional distributions pk. Therefore, unlike computation graphs, there is no notion of function fk 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 pk is normalized, the joint distribution of S=(S1,…,SK) 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,…,K and on iteration k we draw a value sk∼pk(⋅∣spa(k)) conditioned on the previous values spa(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):=Z1C∈C∏ψC(sC), where C is the set of maximal cliques of G, that is, subsets of V that are fully connected, Z is a normalization constant defined by Z:=s∈S∑C∈C∏ψC(sC), and ψC:SC→R+ is a potential function (a.k.a. compatibility function), with SC:=(Sj)j∈C. 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) and s=(s1,…,sK), recall that P(S=s)=k=1∏Kpk(sk∣sk−1). This is equivalent to an MRF with Z=1 (since a chain is automatically normalized), C:={{0,1},{1,2},…,{K−1,K}} and with potential function ψ{k−1,k}(sk−1,sk):=pk(sk∣sk−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)⟩) for some sufficient statistic function ϕC:SC→ΘC and parameters θC∈ΘC. Then, pθ(s):=Z(θ)1C∈C∏ψC(sC;θC)=Z(θ)1C∈C∏exp(⟨θC,ϕC(sC)⟩)=Z(θ)1exp(C∈C∑⟨θC,ϕC(sC)⟩)=Z(θ)1exp(⟨θ,ϕ(s)⟩)=exp(⟨θ,ϕ(s)⟩−A(θ)) where ϕ(s)θZ(θ)A(θ):=(ϕC(sC))C∈C:=(θC)C∈C:=s∈S∑C∈C∏ψC(sC;θC)=s∈S∑exp(⟨θ,ϕ(s)⟩):=logZ(θ). Therefore, for this choice of potential functions, we can view an MRF as an exponential family distribution (Section 3.4) with natural parametersθ, sufficient statisticϕ and log-partition function A(θ).
Example 10.3(Ising model)
The Ising model is a classical example of MRF. Let Y=(Y1,…,YM)∈{0,1}M be an unordered collection of binary variables Yi∈{0,1}. This forms a graph G=(V,E), where V=[M] and E⊆V2, such that (i,j)∈E means that Yi interacts with Yj. In statistical physics, Yi may indicate the presence or absence of particles, or the orientation of magnets. In image processing, Yi may represent a black and white pixel. In multi-label classification, Yi may indicate the presence or absence of a label. The probability of y=(y1,…,yM)∈{0,1}M is then P(Y=y)=pθ(y)=expi∈V∑θiyi+(i,j)∈E∑θi,jyiyj−A(θ)=exp(C∈C∑⟨θC,ϕC(y)⟩−A(θ)), where C:=V∪E and θ∈R∣V∣+∣E∣ is the concatenation of (θi)i∈V and (θi,j)(i,j)∈E. 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 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 yiyjyk for (i,j,k)∈V3. 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 conditioned on a variable x, CRFs are defined as P(Y=y∣X=x)=p(y∣x)=Z(x)1C∈C∏ψC(yC,x). Note that the potential functions ψC are allowed to depend on the whole x, as x is just a conditioning variable.
10.6.4 Sampling
Contrary to Bayesian networks, MRFs require an explicit normalization constant Z. 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)=Z1k=1∏Kψk(sk−1,sk), where Z:=s∈S∑k=1∏Kψk(sk−1,sk) and where we used ψk as a shorthand for ψk−1,k, since k−1 and k are consecutive. As explained in Example 10.2, this also includes Markov chains by setting ψk(sk−1,sk):=pk(sk∣sk−1), in which case Z=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). We can compute these sums recursively, either forward or backward. Recalling the definitions of Ak−1 and Bk+1 in Eq. (10.1), we define the summations up to and down tok, αk(sk)βk(sk):=s1,…,sk−1∈Ak−1∑j=1∏kψj(sj−1,sj)=sk−1∈Sk−1∑ψk(sk−1,sk)⋯s1∈S1∑ψ2(s1,s2)ψ1(s0,s1):=sk+1,…,sK∈Bk+1∑j=k+1∏Kψj(sj−1,sj)=sk+1∈Sk+1∑ψk+1(sk,sk+1)⋯sK∈SK∑ψK(sK−1,sK). We can compute the two quantities by recursing forward and backward αk(sk)βk(sk)=sk−1∈Sk−1∑ψk(sk−1,sk)αk−1(sk−1)=sk+1∈Sk+1∑ψk+1(sk,sk+1)βk+1(sk+1) where we defined the initializations α1(s1)βK(sK):=ψ1(s0,s1)∀s1∈S1:=1∀sK∈SK. The normalization term can then be computed by Z=sK∈SK∑αK(sK)βK(sK)=s1∈S1∑α1(s1)β1(s1) and the marginal probabilities by P(Sk=sk)P(Sk−1=sk−1,Sk=sk)=Z1αk(sk)βk(sk)=Z1αk−1(sk−1)ψk(sk−1,sk)βk(sk). We can also compute the conditional probabilities by P(Sk=sk∣Sk−1=sk−1)=P(Sk−1=sk−1)P(Sk−1=sk−1,Sk=sk)=αk−1(sk−1)βk−1(sk−1)αk−1(sk−1)ψk(sk−1,sk)βk(sk)=βk−1(sk−1)ψk(sk−1,sk)βk(sk). In practice, the two recursions are often implemented in the log-domain for numerical stability, logαk(sk)logβk(sk)=logsk−1∈Sk−1∑exp(logψk(sk−1,sk)+logαk−1(sk−1))=logsk+1∈Sk+1∑exp(logψk+1(sk,sk+1)+logβk+1(sk+1)). 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.
Similarly, using the distributivity of multiplication over maximization, =s1∈S1,…,sK∈SKmaxk=1∏Kψk(sk−1,sk)sK∈SKmaxsK−1∈SK−1maxψK(sK−1,sK)…s1∈S1maxψ2(s1,s2)ψ1(s0,s1). Let us define for k∈[K]δk(sk):=sk−1∈Sk−1maxψk(sk−1,sk)…s1∈S1maxψ2(s1,s2)ψ1(s0,s1). We can compute these quantities recursively, since for k∈[K]δk(sk)=sk−1∈Sk−1maxψk(sk−1,sk)δk−1(sk−1), with δ1(s1):=ψ1(s0,s1). We finally have s1∈S1,…,sK∈SKmaxp(s1,…,sK)=Z1sK∈SKmaxδK(sK). 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]logδk(sk)=sk−1∈Sk−1maxlogψk(sk−1,sk)+logδk−1(sk−1). To enable efficient backtracking, during the forward pass, we compute qk(sk):=sk−1∈Sk−1argmaxψk(sk−1,sk)δk−1(sk−1) which can be thought of as backpointers from sk⋆ to sk−1⋆.
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 ϕ in an exponential family distribution, the gradient ∇A(θ) of the log-partition A(θ) 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(θ)] and of its log-partition A(θ):=logs∈S∑h(s)exp[⟨θ,ϕ(s)⟩]. From Proposition 3.2, μ(θ):=∇A(θ)=EY∼pθ[ϕ(Y)]∈M. Therefore, with the binary encodings in Eq. (10.2) and Eq. (10.3), P(Sk=vi)P(Sk=vi,Sl=vj)=[∇A(θ)]k,i=[∇A(θ)]k,l,i,j. Put differently, if we have an efficient algorithm for computing A(θ), we can perform reverse-mode autodiff on A(θ) to obtain ∇A(θ), 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(θ).
In the special case of chains, we obtain P(Sk=vi)P(Sk−1=vi,Sk=vj)=[∇A(θ)]k,i=Z1αk(vi)βk(vi)=[∇A(θ)]k−1,k,i,j=Z1αk−1(vi)ψk(vi,vj)βk(vj), where we left the dependence of Z, α and β on θ implicit.
If we define Aε(θ):=εA(θ/ε), in the zero-temperature limit ε→0, we obtain that μ(θ) 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 equipped with two binary operations (⊕,⊗) such that
⊕ is associative and commutative,
⊗ is associative and distributive over ⊕,
⊕ and ⊗ have identity elements 0ˉ and 1ˉ, respectively.
We use the notations ⊕, ⊗, 0ˉ and 1ˉ to clearly distinguish them from the classical addition, multiplication, 0 and 1.
We recall the following laws for binary operations:
Commutativity of ⊕: a⊕b=b⊕a,
Associativity of ⊕: a⊕(b⊕c)=(a⊕b)⊕c,
Distributivity of ⊗ over ⊕: a⊗(b⊕c)=(a⊗b)⊕(a⊗c).
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,⊕) and (K,⊗) to be monoids, not groups.
Equipped with these definitions, we can interpret the forward passes in the Viterbi and forward-backward algorithms as follows:
the forward-backward algorithm in the exponential domain uses the semiring R+ equipped with (+,×) and identity elements (0,1);
the Viterbi algorithm in the log domain uses the semiring R equipped with (max,+) and identity elements (−∞,0);
the forward-backward algorithm in the log domain uses the semiring R equipped with (maxε,+) and identity elements (−∞,0), where we defined the soft max operator (log-add-exp) maxε(a,b):=εlog(exp(a/ε)+exp(b/ε)), with ε:=1 by default.
It can be checked that indeed maxε is commutative, associative, and addition is distributive over maxε. Its identity element is −∞. By associativity, maxε(a1,maxε(a2,a3))=logsumexpε(a1,a2,a3)=εlogi∑exp(ai/ε). In contrast, note that the sparsemax in Section 13.5 is not associative.
Thanks to associativity, we can introduce the shorthand notations v∈Vmaxεf(v):=εlogv∈V∑exp(f(v)/ε)∈R. and v∈Vargmaxεf(v):=(exp(f(v′)/ε)/v∈V∑exp(f(v)/ε))v′∈V∈P(V).
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} and Sk={1,…,M} for all k∈[K]. We focus on the case logψk(i,j)=⟨θk,ϕk(i,j)⟩=θk,i,j. We also introduce the shorthands a1,jak,j:=logα1(j)=θ1,1,j:=logαk(j)=i∈[M]maxεθk,i,j+ak−1,i and qk,j:=i∈[M]argmaxεθk,i,j+ak−1,i.
Our goal is to compute the gradient w.r.t. θ∈RK×M×M of logZ=A=j∈[M]maxεaK,j. The soft argmax counterpart of this quantity is Q:=j∈[M]argmaxεaK,j∈△M, where we used P([M])=△M.
Computing the gradient of A is similar to computing the gradient of a feedforward network, in the sense that θk influences not only ak but also ak+1,…,aK. Let us introduce the adjoint variable rk,i:=∂ak,i∂A, which we initialize as rK,i=∂aK,i∂A=Qi. Since θk,i,j directly influences ak,j, we have for k∈[K], i∈[M] and j∈[M]μk,i,j:=∂θk,i,j∂A=∂ak,j∂A⋅∂θk,i,j∂ak,j=rk,j⋅qk,j,i. Since ak,i directly influences ak+1,j for j∈[M], we have for k∈{1,…,K−1} and i∈[M]rk,i=∂ak,i∂A=j∈[M]∑∂ak+1,j∂A∂ak,i∂ak+1,j=j∈[M]∑rk+1,jqk+1,j,i=j∈[M]∑μk+1,i,j.
We summarize the procedure in Algorithm 10.3. The forward pass uses the softmax operator maxε and the softargmax operator argmaxε. In the hard max case, in Algorithm 10.2, we used q to store backpointers from integer to integer. In the soft max case, in Algorithm 10.3, we used 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
Graphical models represent the conditional dependencies between variables and therefore specify how their joint distribution factorizes.
There are clear analogies between the worlds of functions and of distributions: the counterparts of computation chains and computation graphs are Markov chains and Bayesian networks.
Inference on chains and more generally on trees, for exponential family distributions, is equivalent, both statistically and algorithmically, to differentiating the log-partition function.
The forward-backward algorithm can be seen as using a sum-product algebra, while the Viterbi algorithm can be seen as using a max-plus algebra. Equivalently, in the log domain, we can see the former as using a soft max, and the latter as using a hard max.