20
Speech and Language Processing: An introduction to natural language processing, computational linguistics, and speech recognition. Daniel Jurafsky & James H. Martin. Copyright c 2006, All rights reserved. Draft of October 10, 2006. Do not cite without permission. 6 HIDDEN MARKOV MODELS AND LOGLINEAR MODELS In this chapter we introduce two important classes of statistical models for pro- cessing text and speech, the Hidden Markov Model (HMM) and the Maximum Entropy model (MaxEnt). HMMs and MaxEnt are machine learning models. We have already touched on some aspects of machine learning; we briefly introduced the Hidden Markov Model in the previous chapter, and we have introduced the N -gram model in the chapter before. In this chapter we give a more complete introduction to such mod- els, in preparation for the many statistical models that we will see throughout the book, including Naive Bayes, decision lists, and Gaussian Mixture Models. 6.1 MARKOV C HAINS The Hidden Markov Model is one of the most important machine learning mod- els in speech and language processing. In order to define it properly, we need to first introduce the Markov chain, sometimes called the observed Markov model. Markov chains and Hidden Markov Models are both extensions of the finite au- tomata of Ch. 3. Recall that a finite automaton is defined by a set of states, and a set of transitions between states that are taken based on the input observations. A weighted finite-state automaton is a simple augmentation of the finite automaton WEIGHTED in which each arc is associated with a probability, indicating how likely that path is to be taken. The probability on all the arcs leaving a node must sum to 1. A Markov chain is a special case of a weighted automaton in which the MARKOV CHAIN input sequence uniquely determines which states the automaton will go through. Because they can’t represent inherently ambiguous problems, a Markov chain is only useful for assigning probabilities to unambiguous sequences. Fig. 6.1a shows a Markov chain for assigning a probability to a sequence of weather events, where the vocabulary consists of HOT, COLD, and RAINY,. Fig. 6.1b shows another simple example of a Markov chain for assigning a prob-

HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

Speech and Language Processing: An introduction to natural language processing,computational linguistics, and speech recognition. Daniel Jurafsky & James H.Martin. Copyright c© 2006, All rights reserved. Draft of October 10, 2006. Donot cite without permission.

6HIDDEN MARKOVMODELS ANDLOGLINEAR MODELS

In this chapter we introduce two important classes of statistical models for pro-cessing text and speech, theHidden Markov Model (HMM ) and theMaximumEntropy model (MaxEnt ).

HMMs and MaxEnt are machine learning models. We have alreadytouchedon some aspects of machine learning; we briefly introduced the Hidden MarkovModel in the previous chapter, and we have introduced theN-gram model in thechapter before. In this chapter we give a more complete introduction to such mod-els, in preparation for the many statistical models that we will see throughout thebook, includingNaive Bayes, decision lists, andGaussian Mixture Models.

6.1 MARKOV CHAINS

The Hidden Markov Model is one of the most important machine learning mod-els in speech and language processing. In order to define it properly, we need tofirst introduce theMarkov chain , sometimes called theobserved Markov model.Markov chains and Hidden Markov Models are both extensions of the finite au-tomata of Ch. 3. Recall that a finite automaton is defined by a set of states, and aset of transitions between states that are taken based on theinput observations. Aweighted finite-state automatonis a simple augmentation of the finite automatonWEIGHTED

in which each arc is associated with a probability, indicating how likely that pathis to be taken. The probability on all the arcs leaving a node must sum to 1.

A Markov chain is a special case of a weighted automaton in which theMARKOV CHAIN

input sequence uniquely determines which states the automaton will go through.Because they can’t represent inherently ambiguous problems, a Markov chain isonly useful for assigning probabilities to unambiguous sequences.

Fig. 6.1a shows a Markov chain for assigning a probability toa sequenceof weather events, where the vocabulary consists ofHOT, COLD, and RAINY ,.Fig. 6.1b shows another simple example of a Markov chain for assigning a prob-

Page 2: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

2 Chapter 6. Hidden Markov Models and Loglinear Models

(a) (b)

Figure 6.1 A Markov chain for weather (a) and one for words (b). A Markov chain is specified by thestructure, the transition between states, and the start andend states.

ability to a sequence of wordsw1...wn. This Markov chain should be familiar; infact it represents a bigram language model. Given the two models in Figure 6.1 wecan assign a probability to any sequence from our vocabulary. We’ll go over howto do this shortly.

First, let’s be more formal. We’ll view a Markov chain as a kind of prob-abilistic graphical model; a way of representing probabilistic assumptions in agraph. A simple Markov model is specified by a set ofstatesQ, a set oftransi-tion probabilities A, and a specialstart state andend state(s). We can representthe states as nodes in the graph, and the transitions as edgesbetween nodes. Insummary:

• states:a set of statesQ = q1q2 . . .qN

• transition probabilities: a set of probabilitiesA = a01a02. . .an1 . . .ann. Eachai j represents the probability of transitioning from statei to statej. The setof these is thetransition probability matrix A.

• start and end states:a special start state (and optionally a special end state),which are not associated with observations, as shown in Fig.6.1.

Because eachai j expresses the probabilityp(q j|qi), the total sum of the out-going arcs from a given state must sum to 1:

n∑

j=1

ai j = 1 ∀i(6.1)

Instead of a special start state, we can represent the distribution over initialstates and accepting states explicitly:

• initial distribution: an initial probability distribution over states,π, such that

Page 3: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

Section 6.1. Markov Chains 3

πi is the probability that the HMM will start in statei. Of course some statesj may haveπ j = 0, meaning that they cannot be initial states.

• accepting states:a set of legal accepting states

Thus the probability of state 1 being the first state can be represented eitherasa01 or asπ1. Because eachπi expresses the probabilityp(qi|START), the totalsum of theπ probabilities must sum to 1:

n∑

i=1

πi = 1(6.2)

Finally, in afirst-order Markov chain, the probability of a particular state isFIRST­ORDER

dependent only on the previous state:

P(qi|q1...qi−1) = P(qi|qi−1)(6.3)

(a) (b)

Figure 6.2 Another representation of the same Markov chain for weathershown in Fig. 6.1. Insteadof using a special start state witha01 transition probabilities, we use theπ vector, which represents thedistribution over starting state probabilities. The figurein (b) shows sample probabilities.

Before you go on, use the sample probabilities inFig. 6.2b tocompute theprobability of each of the following sequences:

(6.4) hot hot hot hot

(6.5) cold hot cold hot

What does the difference in these probabilities tell you about a real-worldweather fact encoded in Fig. 6.2b?

Page 4: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

4 Chapter 6. Hidden Markov Models and Loglinear Models

6.2 THE HIDDEN MARKOV MODEL

A Markov chain is useful when we need to compute a probabilityfor a sequence ofevents that we can observe in the world. In many cases, however, the events we areinterested in may not be directly observable in the world. For example for part-of-speech tagging (Ch. 5) we didn’t observe part of speech tags in the world; we sawwords, and had to infer the correct tags from the word sequence. We call the part-of-speech tagshidden because they are not observed. We will see the same thingin speech recognition; we’ll see acoustic events in the world, and have to infer thepresence of ‘hidden’ words that are the underlying causal source of the acoustics.A Hidden Markov Model (HMM ) allows us to talk about bothobserved eventsHIDDEN MARKOV

MODEL

(like words that we see in the input) andhidden events (like part-of-speech tags)that we think of as causal factors in our probabilistic model.

To exemplify these models, we’ll use a task invented by JasonEisner (2002).Imagine that you are a climatologist in the year 2799 studying the history of globalwarming. You cannot find any records of the weather in Baltimore, Maryland, forthe summer of 2007, but you do find Jason Eisner’s diary, whichlists how many icecreams Jason ate every day that summer. Our goal is to use these observations toestimate the temperature every day. We’ll simplify this weather task by assumingthere are only two kinds of days: cold (C) and hot (H).

So the Eisner task is as follows: Given a sequence of observations (numbersof ice creams eaten) we must figure out the correct ‘hidden’ sequence of H and Cwhich caused Jason to eat ice cream.

Let’s begin by seeing how a Hidden Markov Model differs from aMarkovchain. AnHMM is specified by a set ofstatesQ, a set oftransition probabilitiesHMM

A, a set of observation likelihoodsB, a definedstart state andend state(s), and aset ofobservation symbolsO, which is not drawn from the same alphabet as thestate setQ:

• states:a set of statesQ = q1q2 . . .qN

• observations:a set of observationsO = o1o2 . . .oN , each observation drawnfrom a vocabularyV = v1,v2, ...,vV .

• transition probabilities: a set of probabilitiesA = a01a02. . .an1 . . .ann. Eachai j represents the probability of transitioning from statei to statej. The setof these is thetransition probability matrix A

• observation likelihoods: a set of observation likelihoodsB = bi(ot), eachexpressing the probability of an observationot being generated from a statei. These are often called the HMMemission probabilities.EMISSION

PROBABILITIES

As we noted for Markov chains, we can use two “special” states(non-emitting

Page 5: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

Section 6.2. The Hidden Markov Model 5

states) as the start and end state; or we can avoid the use of these states by speci-fying two more things:

• initial distribution: an initial probability distribution over states,π, such thatπi is the probability that the HMM will start in statei. Of course some statesj may haveπ j = 0, meaning that they cannot be initial states.

• accepting states:a set of legal accepting states

Again, we have the same constraints as for a Markov chain thatthe variousprobabilities must correctly sum to zero:

n∑

j=1

ai j = 1 ∀i

n∑

i=1

πi = 1

As with a first-order Markov chain, a first-order Hidden Markov Model in-stantiates two simplifying assumptions. First, the probability of a particular stateis dependent only on the previous state:

Markov Assumption: P(qi|q1...qi−1) = P(qi|qi−1)(6.6)

Second, the probability of an output observationoi is dependent only on the statethat produced the observationqi, and not on any other states or any other observa-tions:

Output Independence Assumption: P(oi|q1 . . .qi . . .qn,o1 . . .oi . . . ,on) = P(oi|qi)(6.7)

Fig. 6.3 shows a sample HMM for the ice cream task. The two hidden states(H and C) correspond to hot and cold weather, while the observations (drawn fromthe alphabetO = {1,2,3}) correspond to the number of ice creams eaten by Jasonon a given day.

Notice that in the HMM in Fig. 6.3, there is a (non-zero) probability of tran-sitioning between any two states. Such an HMM is called afully-connected orFULLY­CONNECTED

ergodic HMM . Sometimes, however, we have HMMs in which many of the tran-ERGODIC HMM

sitions between states have zero probability. For example,in left-to-right (alsoLEFT­TO­RIGHT

calledBakis) HMMs, the state transitions proceed from left to right, as shown inBAKIS

Fig. 6.4. In a Bakis HMM, there are no transitions going from ahigher-numberedstate to a lower-numbered state (or, more accurately, any transitions from a higher-number state to a lower-numbered state have zero probability). Bakis HMMs aregenerally used to model temporal processes like speech; we will see more of thisin Ch. 9.

Now that we have seen the structure of an HMM, we turn to algorithms forcomputing things with them. An influential tutorial by Rabiner (1989), based on

Page 6: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

6 Chapter 6. Hidden Markov Models and Loglinear Models

Figure 6.3 A Hidden Markov Model for relating numbers of ice creams eaten byJason (the observations) to the weather (H or C, the hidden variables).

Figure 6.4 Two 4-state Hidden Markov models; a left-to-right (Bakis) HMM onthe left, and a fully-connected (ergodic) HMM on the right. In the Bakis model, alltransitions not shown have zero probability.

tutorials by Jack Ferguson in the 1960s, introduced the ideathat Hidden MarkovModels should be characterized by three fundamental problems: (1) given a spe-cific HMM, determining the likelihood of an observation sequence, (2) given anobservation sequence and an HMM, discovering the best (mostprobable) hiddenstate sequence, and (3) given only an observation sequence,learning the HMM pa-rameters. We already saw an example of problem (2) in Ch. 5; now we introduceit more formally, along with each of the other two tasks, in the next 3 sections.

6.3 COMPUTING L IKELIHOOD : THE FORWARD ALGORITHM

Our first problem is to compute the likelihood of a particularobservation sequencegiven a specific HMM. For example, given the HMM in Fig. 6.2b, what is theprobability of the sequence3 1 3?

Page 7: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

Section 6.3. Computing Likelihood: The Forward Algorithm 7

For a Markov chain, where the surface observations are the same as the hid-den events, we could compute the probability of3 1 3 just by following the stateslabeled3 1 3 and multiplying the probabilities along the arcs. For a Hidden MarkovModel, things are not so simple. We want to determine the probability of an ice-cream observation sequence like3 1 3, but we don’t know what the hidden statesequence is!

Let’s start with a slightly simpler situation. Suppose we already knew theweather, and wanted to predict how much ice cream Jason wouldeat. This is auseful part of many HMM tasks. For a given hidden state sequence (e.g.hot hotcold) we can easily compute the output likelihood of3 1 3.

Let’s see how. First, recall that for Hidden Markov Models, each hidden stateproduces only a single observation. Thus the sequence of hidden states and thesequence of observations has the same length.1

Given this one-to-one mapping, and the Markov assumptions expressed inEq. 6.6, for a particular hidden state sequenceQ = q0,q1,q2, ...,qn and an observa-tion sequenceO = o1,o2, ...,on, the likelihood of the observation sequence (usinga special start stateq0 rather thanπ probabilities) is:

P(O|Q) =n

i=1

P(oi|qi)×n

i=1

P(qi|qi−1)(6.8)

The computation of the forward probability for our ice-cream observation3 13 from one possible hidden state sequencehot hot hot is as follows (Fig. 6.5 showsa graphic representation of this):

P(3 1 3|hot hot cold) = P(hot|start)×P(hot|hot)×P(cold|hot)

×P(3|hot)×P(1|hot)×P(3|cold)(6.9)

In order to compute the true total likelihood of3 1 3, however, we need tosum over all possible hidden state sequences (in this case, the 8 sequencescoldcold cold, cold cold hot, and so on). For an HMM withN hidden states and anobservation sequence ofT observations, there areNT possible hidden sequences.For real tasks, whereN and T are both large,NT is a very large number, andso we cannot compute the total observation likelihood by computing a separateobservation likelihood for each hidden state sequence and then summing them up.

Instead of using such an extremely exponential algorithm, we use an effi-cient algorithm called theforward algorithm .The forward algorithm is a kindFORWARD

ALGORITHM

of dynamic programming algorithm, i.e., an algorithm that uses a table to store

1 There are variants of HMMs calledsegmental HMMs (in speech recognition) orsemi-HMMs(in natural language processing) in which this one-to-one mapping between the length of the hiddenstate sequence and the length of the observation sequence does not hold.

Page 8: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

8 Chapter 6. Hidden Markov Models and Loglinear Models

Figure 6.5 The computation of the observation likelihood for the ice-cream events3 1 3 given the hidden state sequencehot hot cold.

intermediate values as it builds up the probability of the observation sequence.The forward algorithm computes the observation probability by summing over theprobabilities of all possible hidden-state paths that could generate the observationsequence, but it does so efficiently by implicitly folding each of these paths into asingleforward trellis .

Fig. 6.6 shows an example of the forward trellis for computing the likelihoodof 3 1 3 given the hidden state sequencehot hot cold.

� � � � �

� � �

�� � � � � � � � � � � � � � � � � �

� �

� � � � � � � � � � � � � ! � "

� � # � # � � � � � � # � $ � %

& ' ( ) * + ,& ' - ) ( +. / ,

. 01 2 3 4 5 6 7

1 2 8 4 3 69 : 7 9;

< = > ? @ A B C A D E < = F ? > DG H E G I

J K L M N

J K L O N

J K L M N P L O Q R S L O N P L O T K L O O U O T

J K L M N P L Q V S L O N P L M O K L O V R

Figure 6.6 The forward trellis for computing the total observation likelihood for the ice-cream events3 1 3.

Each cell of the forward algorithm trellisαt( j) represents the probability ofbeing in statej after seeing the firstt observations, given the automatonλ. Thevalue of each cellαt( j) is computed by summing over the probabilities of everypath that could lead us to this cell. Formally, each cell expresses the followingprobability:

αt( j) = P(o1,o2 . . .ot ,qt = j|λ)(6.10)

Page 9: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

Section 6.3. Computing Likelihood: The Forward Algorithm 9

Hereqt = j means “the probability that thetth state in the sequence of statesis statej”. We compute this probability by summing over the extensions of all thepaths that lead to the current cell. An extension of a path from a statei at timet−1is computed by multiplying the following three factors:

1. theprevious path probability from the previous cellαt−1(i),

2. thetransition probability ai j from previous statei to current statej, and

3. thestate observation likelihoodb j(ot) that current statej matches observa-tion symbolt.

Consider the computation in Fig. 6.6 ofα2(1), the forward probability ofbeing at time step 2 in state 1 having generated the partial observation3 2. Thisis computed by extending theα probabilities from time step 1, via two paths, eachextension consisting of the three factors above:α1(1)× P(H|H)× P(1|H) andα1(2)×P(H|C)×P(1|H).

Fig. 6.7, adapted from Rabiner (1989), shows another visualization of thisinduction step for computing the value in one new cell of the trellis.

� � � � � �

� �

� �

� � �� � �

� �

� �

� � � � �

� � � � � � � � � � � � � � � � � � � � � � � �

Figure 6.7 Visualizing the computation of a single elementαt(i) in the trellis bysumming all the previous valuesαt−1 weighted by their transition probabilitiesa andmultiplying by the observation probabilitybi(ot+1). For many applications of HMMs,many of the transition probabilities are 0, so not all previous states will contribute tothe forward probability of the current state. Adapted from Rabiner (1989).

We give two formal definitions of the Forward algorithm; the pseudocode inFig. 6.8 and a statement of the definitional recursion here:

1. Initialization:

Page 10: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

10 Chapter 6. Hidden Markov Models and Loglinear Models

function FORWARD(observations of len T,state-graph) returns forward-probability

num-states←NUM-OF-STATES(state-graph)Create a probability matrixforward[num-states+2,T+2]forward[0,0]←1.0for each time stept from 1 to T do

for each states from 1 to num-states doforward[s,t]←

1 ≤ s′≤ num-states

[

forward[s′,t−1] ∗ as′,s]

∗ bs(ot)

return the sum of the probabilities in the final column offorward

Figure 6.8 The forward algorithm; we’ve used the notationforward[s,t] to repre-sentαt(s).

α1( j) = a0 jb j(o1) 1≤ j ≤ N(6.11)

2. Recursion (since states 0 and N are non-emitting):

αt( j) =

[

N−1∑

i=1

αt−1(i)ai j

]

b j(ot); 1 < j < N,1 < t < T(6.12)

3. Termination:

P(O|λ) = αT (N) =N−1∑

i=2

αT (i)aiN(6.13)

6.4 DECODING: THE V ITERBI ALGORITHM

For any model, such as an HMM, that contains hidden variables, the task of de-termining which sequence of variables is the underlying source of some sequenceof observations is called thedecodingtask. In the ice cream domain, given a se-DECODING

quence of ice cream observations3 1 3 and an HMM, the task of thedecoderis toDECODER

find the best hidden weather sequence (H H H). More formally, given as input anHMM λ = (A,B,π) and a sequence of observationsO = o1,o2, ...,ot , find the mostprobable sequence of statesQ = (q1q2q3 . . .qt),

We might propose to find the best sequence as follows: for eachpossible hid-den state sequence (HHH, HHC, HCH, etc.), we could run the forward algorithmand compute the likelihood of the observation sequence given that hidden statesequence. Then we could choose the hidden state sequence with the max observa-tion likelihood. It should be clear from the previous section that we cannot do thisbecause there are an exponentially large number of state sequences!

Page 11: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

Section 6.4. Decoding: The Viterbi Algorithm 11

Instead, the most common decoding algorithms for HMMs is theViterbi al-VITERBI

gorithm. Like the Forward algorithm,Viterbi is a kind ofdynamic programming,and makes uses of a dynamic programming trellis. Viterbi also strongly resemblesanother dynamic programming variant, theminimum edit distance algorithm ofCh. 3.

� � � � �

� � �

�� � � � � � � � � � � � � � � � � �

� �

� � � � � � � � � � � � � ! � "

� � # � # � � � � � � # � $ � %

& ' ( ) * + , & ' - ) ( +. / , . 01 2 3 4 5 6 7

1 2 8 4 3 69 : 7 9;

< = > ? @ A B C A D E < = F ? > DG H E G I

J K L M N

J K L O N

J K P Q R S L M N T L U V W L O N T L O X Y K L O V V Z

J K P Q R S L M N T L U [ W L O N T L M O Y K L O V Z

Figure 6.9 The Viterbi trellis for computing the best path through the hidden state space for the ice-cream eating events3 1 3.

Fig. 6.9 shows an example of the Viterbi trellis for computing the best hiddenstate sequence for the observation sequence3 1 3. The idea is to process the obser-vation sequence left to right, filling out the trellis. Each cell of the Viterbi trellis,vt( j) represents the probability that the HMM is in statej after seeing the firsttobservations and passing through the most likely state sequenceq1...qt−1, giventhe automatonλ. The value of each cellvt( j) is computed by recursively takingthe most probable path that could lead us to this cell. Formally, each cell expressesthe following probability:

vt(i) = P(q0,q1...qt−1,o1,o2 . . .ot ,qt = i|λ)(6.14)

Like other dynamic programming algorithms, Viterbi fills each cell recur-sively. Given that we had already computed the probability of being in states′ attime t−1, we can compute the probability of extending this path to a new statesby multiplying these three factors:

1. theprevious path probability from the previous cellvt−1(s′),

2. thetransition probability ai j from previous states′ to current states, and

3. the observation likelihood bs(ot) of the observation symbolot given thecurrent states.

Page 12: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

12 Chapter 6. Hidden Markov Models and Loglinear Models

function V ITERBI(observations of len T,state-graph) returns best-path

num-states←NUM-OF-STATES(state-graph)Create a path probability matrixviterbi[num-states+2,T+2]viterbi[0,0]←1.0for each time stept from 1 to T do

for each states from 1 to num-states doviterbi[s,t]← max

1 ≤ s′≤ num-states

[

viterbi[s′,t−1] ∗ as′,s]

∗ bs(ot)

back-pointer[s,t]← argmax1 ≤ s′≤ num-states

[

viterbi[s′,t−1] ∗ as′,s]

Backtrace from highest probability state in final column ofviterbi[] and return path

Figure 6.10 Viterbi algorithm for finding optimal sequence of tags. Given anobservation sequence and an HMMλ = (A,B), the algorithm returns the state-paththrough the HMM which assigns maximum likelihood to the observation sequence.Note that states 0 and N+1 are non-emittingstart andend states.

Fig. 6.10 shows pseudocode for the Viterbi algorithm. Note that the Viterbialgorithm is identical to the Forward algorithm except thatit takes themax overthe previous path probabilities where Forward takes thesum. Note also that theViterbi algorithm has one component that the Forward algorithm doesn’t have:backpointers. This is because while the Forward algorithm needs to produce anobservation likelihood, the Viterbi algorithm must produce a probability and alsothe most likely state sequence. We compute this best state sequence by keepingtrack of the path of hidden states that led to each state, as suggested in Fig. 6.11.

Finally, we can give a formal definition of the Viterbi recursion as follows:

1. Initialization:

v1( j) = a0 jb j(o1) 1≤ j ≤ N(6.15)

bt1 j = 0(6.16)

2. Recursion(recall states 0 and N are non-emitting):

vt( j) =

[

N−1maxi=1

vt−1(i)ai j

]

b j(ot); 1 < j < N,1 < t < T(6.17)

btt( j) =

[

N−1argmax

i=1vt−1(i)ai j

]

b j(ot); 1 < j < N,1 < t < T(6.18)

3. Termination:

The best score:P∗ =N

maxi=1

vT (i)(6.19)

Page 13: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

Section 6.5. Training HMMs: The Forward-Backward Algorithm 13

� � � � �

� � �

�� � � � � � � � � � � � � � � � � �

� �

� � � � � � � � � � � � � ! � "

� � # � # � � � � � � # � $ � %

& ' ( ) * + , & ' - ) ( +. / , . 01 2 3 4 5 6 7

1 2 8 4 3 69 : 7 9;

< = > ? @ A B C A D E < = F ? > DG H E G I

J K L M N

J K L O N

J K P Q R S L M N T L U V W L O N T L O X Y K L O V V Z

J K P Q R S L M N T L U [ W L O N T L M O Y K L O V Z

Figure 6.11 The Viterbi backtrace. As we extend each path to a new state account for the next obser-vation, we keep a backpointer (shown with broken blue lines)to the best path that led us to this state.

The start of backtrace:qT∗ =N

argmaxi=1

btT (i)(6.20)

6.5 TRAINING HMM S: THE FORWARD-BACKWARD ALGORITHM

We turn to the third problem for HMMs: learning the parameters of an HMM, i.e.,theA andB matrices.

The input to such a learning algorithm would be an unlabeled sequence ofobservationsO and a vocabulary of potential hidden statesQ. Thus for the icecream task, we would start with a sequence of observationsO = {1,3,2, ...,}, andthe set of hidden statesH andC. For the part-of-speech tagging task we would startwith a sequence of observationsO = {w1,w2,w3 . . .} and a set of hidden statesNN,NNS, VBD, IN,... and so on.

The standard algorithm for HMM training is theforward-backward orBaum-FORWARD­BACKWARD

Welch algorithm (Baum, 1972), a special case of theExpectation-MaximizationBAUM­WELCH

or EM algorithm (Dempster et al., 1977). The algorithm will let ustrain both theEM

transition probabilitiesA and the emission probabilitiesB of the HMM.Let us begin by considering the much simpler case of traininga Markov

chain rather than a Hidden Markov Model. Since the states in aMarkov chainare observed, we can run the model on the observation sequence and directly seewhich path we took through the model, and which state generated each observationsymbol. A Markov chain of course has no emission probabilitiesB (alternativelywe could view a Markov chain as a degenerate Hidden Markov Model where all

Page 14: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

14 Chapter 6. Hidden Markov Models and Loglinear Models

the b probabilities are 1.0 for the observed symbol and 0 for all other symbols.).Thus the only probabilities we need to train are the transition probability matrixA.

We get the maximum likelihood estimate of the probabilityai j of a particulartransition between statesi and j by counting the number of times the transition wastaken, which we could callC(i→ j), and then normalizing by the total count of alltimes we took any transition from statei:

ai j =C(i→ j)

q∈QC(i→ q)(6.21)

We can directly compute this probability in a Markov chain because we knowwhich states we were in. For an HMM we cannot compute these counts directlyfrom an observation sequence since we don’t know which path of states was takenthrough the machine for a given input. The Baum-Welch algorithm uses two neatintuitions to solve this problem. The first idea is toiteratively estimate the counts.We will start with an estimate for the transition and observation probabilities, andthen use these estimated probabilities to derive better andbetter probabilities. Thesecond idea is that we get our estimated probabilities by computing the forwardprobability for an observation and then dividing that probability mass among allthe different paths that contributed to this forward probability.

In order to understand the algorithm, we need to define a useful probabilityrelated to the forward probability, called thebackward probability .BACKWARD

PROBABILITY

The backward probabilityβ is the probability of seeing the observations fromtime t +1 to the end, given that we are in statej at timet (and of course given theautomatonλ):

βt(i) = P(ot+1,ot+2 . . .oT |qt = i,λ)(6.22)

It is computed inductively in a similar manner to the forwardalgorithm.

1. Initialization:

βT (i) = aiN , 1 < i < N(6.23)

2. Recursion (again since states 0 and N are non-emitting):

βt(i) =N−1∑

i=1

ai jb j(ot+1)βt+1( j) 1 < i < N,0 < t < T(6.24)

3. Termination:

P(O|λ) = αT (N) = βT (1) =N−1∑

j=1

a1 jb j(o1)β1( j)(6.25)

Fig. 6.12 illustrates the backward induction step.We are now ready to understand how the forward and backward probabili-

ties can help us compute the transition probabilityai j and observation probability

Page 15: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

Section 6.5. Training HMMs: The Forward-Backward Algorithm 15

� � � �� �

� �

� �

� �

� � � �

� �

� � � � � �

� � � � � � � � � � � � � � � � � � � � � � ! � "� # � � � � �

� $ � � � � �

� % � � � � �

Figure 6.12 The computation ofβt(i) by summing all the successive valuesβt+1( j) weighted by their transition probabilitiesa and their observation probabil-ities b j(ot+1). After (Rabiner, 1989)

bi(ot) from an observation sequence, even though the actual path taken through themachine is hidden.

Let’s begin by showing how to reestimateai j. We will proceed to estimateai j by a variant of (6.21):

ai j =expected number of transitions from statei to statej

expected number of transitions from statei(6.26)

How do we compute the numerator? Here’s the intuition. Assume we hadsome estimate of the probability that a given transitioni→ j was taken at a par-ticular point in timet in the observation sequence. If we knew this probability foreach particular timet, we could sum over all timest to estimate the total count forthe transitioni→ j.

More formally, let’s define the probabilityξt as the probability of being instatei at time t and statej at time t + 1, given the observation sequence and ofcourse the model:

ξt(i, j) = P(qt = i,qt+1 = j|O,λ)(6.27)

In order to computeξt , we first compute a probability which is similar toξt ,but differs in including the probability of the observation:

not-quite-ξt(i, j) = P(qt = i,qt+1 = j,O|λ)(6.28)

Fig. 6.13 shows the various probabilities that go into computing not-quite-ξt :the transition probability for the arc in question, theα probability before the arc,theβ probability after the arc, and the observation probabilityfor the symbol justafter the arc.

Page 16: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

16 Chapter 6. Hidden Markov Models and Loglinear Models

� � � �� � � �

� � � �

� � � � �

� � � � � � � � �� � � �

� � � � � �

Figure 6.13 Computation of the joint probability of being in statei at timet andstate j at timet +1. The figure shows the various probabilities that need to be com-bined to produceP(qt = i,qt+1 = j,O|λ): the α andβ probabilities, the transitionprobabilityai j and the observation probabilityb j(ot+1). After Rabiner (1989).

These are multiplied together to producenot-quite-ξt as follows:

not-quite-ξt(i, j) = αt(i)ai jb j(ot+1)βt+1( j)(6.29)

In order to computeξt from not-quite-ξt , the laws of probability instruct usto divide byP(O|λ), since:

P(X |Y,Z) =P(X ,Y |Z)

P(Y |Z)(6.30)

The probability of the observation given the model is simplythe forwardprobability of the whole utterance, (or alternatively the backward probability ofthe whole utterance!), which can thus be computed in a numberof ways:

P(O|λ) = αT (N) = βT (1) =N

j=1

αt( j)βt( j)(6.31)

So, the final equation forξt is:

ξt(i, j) =αt(i)ai jb j(ot+1)βt+1( j)

αT (N)(6.32)

The expected number of transitions from statei to statej is then the sum overall t of ξ. For our estimate ofai j in (6.26), we just need one more thing: the totalexpected number of transitions from statei. We can get this by summing over all

Page 17: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

Section 6.5. Training HMMs: The Forward-Backward Algorithm 17

transitions out of statei. Here’s the final formula for ˆai j:

ai j =

∑T−1t=1 ξt(i, j)

∑T−1t=1

∑Nj=1ξt(i, j)

(6.33)

We also need a formula for recomputing the observation probability. This isthe probability of a given symbolvk from the observation vocabularyV , given astatej: b j(vk). We will do this by trying to compute:

b j(vk) =expected number of times in statej and observing symbolvk

expected number of times in statej(6.34)

For this we will need to know the probability of being in statej at time t,which we will call γt( j):

γt( j) = P(qt = j|O,λ)(6.35)

Once again, we will compute this by including the observation sequence inthe probability:

γt( j) =P(qt = j,O|λ)

P(O|λ)(6.36)

� � � �

� � � � �

� � � � �

� � � � �

Figure 6.14 The computation ofγt( j), the probability of being in statej at timet.Note thatξ is really a degenerate case ofξ and hence this figure is like a version ofFig. 6.13 with statei collapsed with statej. After Rabiner (1989).

As Fig. 6.14 shows, the numerator of (6.36) is just the product of the forwardprobability and the backward probability:

γt( j) =αt( j)βt( j)

P(O|λ)(6.37)

We are ready to computeb. For the numerator, we sumγt( j) for all timestepst in which the observationot is the symbolvk that we are interested in. For the

Page 18: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

18 Chapter 6. Hidden Markov Models and Loglinear Models

denominator, we sumγt( j) over all time stepst. The result will be the percentage ofthe times that we were in statej that we saw symbolvk (the notation

∑Tt=1s.t.Ot=vk

means ”sum over allt for which the observation at timet wasvk):

b j(vk) =

∑Tt=1s.t.Ot=vk

γt( j)∑T

t=1 γt( j)(6.38)

We now have ways in (6.33) and (6.38) tore-estimate the transitionA and ob-servationB probabilities from an observation sequenceO assuming that we alreadyhave a previous estimate ofA andB.

These re-estimations form the core of the iterative forward-backward algo-rithm.

The forward-backward algorithm starts with some initial estimate of the HMMparametersλ = (A,B,π). We then iteratively run two steps. Like other cases ofthe EM (expectation-maximization) algorithm the forward-backward algorithm hastwo steps: theexpectationstep, orE-step, and themaximization step, orM-step.EXPECTATION

E­STEP

MAXIMIZATION

M­STEP

In the E-step we compute the expected state occupancy countγ and the ex-pected state transition countξ, from the earlierA andB probabilities. In the M-step,we useγ andξ to recompute newA, B, andπ probabilities.

function FORWARD-BACKWARD (observations of lenT,output vocabulary V, hiddenstate set Q) returns HMM A, B, π

initialize A, B, andπiterate until convergence

E-step

γt( j) = αt( j)βt( j)P(O|λ) ∀ t and j

ξt(i, j) =αt(i)ai jb j(ot+1)βt+1( j)

αT (N) ∀ t, i, and jM-step

ai j =∑T−1

t=1 ξt(i, j)∑T−1

t=1

∑Nj=1 ξt(i, j)

b j(vk) =

∑Tt=1s.t. Ot=vk

γt( j)∑T

t=1 γt( j)return A, B, π

Figure 6.15 The forward-backward algorithm.

Although in principle the Forward-Backward algorithm can do completelyunsupervised learning of theA, B, andπ parameters, in practice the initial condi-tions are very important. For this reason the algorithm is often given extra informa-

Page 19: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

Section 6.6. Loglinear models 19

tion. For example, for speech recognition, in practice the HMM structure is veryoften set by hand, and only the emission (B) and (non-zero)A transition probabili-ties are trained from a set of observation sequencesO. Gaussian functions. Sec.??will also discuss how initial estimates fora andb are derived in speech recognition.We will also see in Ch. 9 that the forward-backward algorithmcan be extended toinputs which are non-discrete (“continuous observation densities”).

6.6 LOGLINEAR MODELS

6.7 MAXIMUM ENTROPY MARKOV MODELS (MEMM S)

6.8 EVALUATION AND STATISTICS

BIBLIOGRAPHICAL AND HISTORICAL NOTES

Page 20: HIDDEN MARKOV MODELS AND LOGLINEAR MODELSplaza.ufl.edu/hfilip/jurafsky.martin6.pdfstates) as the start and end state; or we can avoid the use of these states by speci-fying two more

20 Chapter 6. Hidden Markov Models and Loglinear Models

Baum, L. E. (1972). An inequality and associated max-imization technique in statistical estimation for proba-bilistic functions of Markov processes. In Shisha, O.(Ed.), Inequalities III: Proceedings of the Third Sympo-sium on Inequalities, University of California, Los An-geles, pp. 1–8. Academic Press.

Dempster, A. P., Laird, N. M., and Rubin, D. B. (1977).Maximum likelihood from incomplete data via theEMalgorithm.Journal of the Royal Statistical Society, 39(1),1–21.

Eisner, J. (2002). An interactive spreadsheet for teachingthe forward-backward algorithm. InProceedings of theACL Workshop on Effective Tools and Methodologies forTeaching NLP and CL, pp. 10–18.

Rabiner, L. R. (1989). A tutorial on Hidden Markov Mod-els and selected applications in speech recognition.Pro-ceedings of the IEEE, 77(2), 257–286.