idden Markov Models Vorlesung omputerlinguistische Techniken Alexander Koller 8. Dezember 04
n-gramm-modelle Ein n-gramm ist ein n-tupel von Wörtern. -Gramme heißen auch Unigramme; -Gramme Bigramme; -Gramme Trigramme n-gramm-modell: Unabhängigkeitsannahme P(w i w... w i- ) = P(w i w i-n+... w i- ) Für Bigramme: P (w,...,w n )=P (w ) ny P (w i w i ) i=
Stochastische Prozesse Zufallsprozess oder stochastischer Prozess: Sequenz von Zufallsvariablen X,..., X t. Wert von X i z.b. das i-te Wort im String X i könnte von X,..., X i- abhängig sein Kann dann P(a,..., a t ) ausrechnen: P (a,...,a t )=P (a )... P (a t a,...,a t ) X X X X 4 Kupfer ist ein Metall
Markov-Ketten Eine Markov-Kette ist ein stochastischer Prozess mit der Markov-Eigenschaft. Markov-Eigenschaft (von Grad ): P (X i X,...,X i )=P (X i X i ) für alle i > Bigramm-Modelle sind also Markov-Ketten von Grad.
Penn Treebank POS-Tags
idden Markov Models Generative Geschichte der letzten Woche: Wörter zufällig aus n-gramm-modell P(w n w,,w n- ). Ersetzen wir heute mit neuer generativer Geschichte: Sprache wird durch zweistufigen Prozess erzeugt. Erzeuge zuerst Sequenz von verborgenen Wortarten (partof-speech tags) t,, t T von links nach rechts aus Bigramm- Modell P(t i t i- ). Erzeuge unabhängig davon ein Wort w i aus jedem t i, aus einem Modell P(w i t i ). t t t w w w
Frage : Sprachmodellierung Gegeben ein MM und einen String w,, w T, was ist P(w w T )? Berechnen P(w w T ) effizient mit dem Forward-Algorithmus. DT NN VBD NNS IN DT NN The representative put chairs on the table. DT JJ NN VBZ IN DT NN The representative put chairs on the table. p p P(Satz) = p + p
Frage : Tagging (aka Decoding) Gegeben ein MM und beobachteten String w,, w T, was ist die wahrscheinlichste Tag- Sequenz t,, t T? Wir berechnen arg max t,...,t T P (t,w,...,t T,w T ) effizient mit dem Viterbi-Algorithmus. DT NN VBD NNS IN DT NN The representative put chairs on the table. DT JJ NN VBZ IN DT NN The representative put chairs on the table. p p
Frage : Tagging (aka Decoding) Gegeben ein MM und beobachteten String w,, w T, was ist die wahrscheinlichste Tag- Sequenz t,, t T? Wir berechnen arg max t,...,t T P (t,w,...,t T,w T ) effizient mit dem Viterbi-Algorithmus. DT NN VBD NNS IN DT NN The representative put chairs on the table. DT JJ NN VBZ IN DT NN The representative put chairs on the table. p p
Zur Erinnerung NN VVFIN PPER PAV ADV Gefallen findet er daran bestimmt. Gefallen ist er nicht. VVPP VAFIN PPER PTKNEG POS-Tagging ist nicht trivial.
Frage a: Lernen Gegeben Menge von POS-Tags und annotierte Trainingsdaten (w,t ),, (w T,t T ), berechne Parameter für MM, die die Likelihood maximieren. Geht effizient mit Maximum-Likelihood-Training plus Smoothing. DT NN VBD NNS IN DT NN The representative put chairs on the table. NNP VBZ VBN TO VB NR Secretariat is expected to race tomorrow. nächstes Mal
Frage b: Lernen Gegeben Menge von POS-Tags und unannotierte Trainingsdaten w,, w T, berechne Parameter für MM, die die Likelihood maximieren. Geht mit dem Forward-Backward-Algorithmus (Spezialfall von Expectation Maximization). The representative put chairs on the table. Secretariat is expected to race today. nächstes Mal
idden Markov Models Ein idden Markov Model (MM) ist 5-Tupel aus: endliche Menge Q = {q,..., q N } von Zuständen (= POS-Tags) endliche Menge O von möglichen Beobachtungen (= Wörtern) Übergangsw. a ij = P(X t+ = q j X t = q i ) Anfangsw. a 0i = P(X = q i ) Ausgabew. b i (o) = P(Y t = o X t = q i ) Zwei gekoppelte Zufallsprozesse: X t = q i : MM ist zum Zeitpunkt t im Zustand q i Y t = o: MM gibt zum Zeitpunkt t Zeichen o aus NX a ij = j= NX a 0i = i= X b i (o) = oo
Ein Beispiel JJ tall AT NN the man 0. boy AT NN AT JJ NN AT JJ NN the man the tall man the tall boy P = * * P = * * * * P = * * * * 0.
Varianten MMs in Jurafsky & Martin: statt Anfangsw.: Anfangszustand q 0 + Übergangsw. (äquivalent) zusätzlich Endzustand q F (nicht nötig) MMs in Manning & Schütze: Symbole werden an Kanten ausgegeben statt an Knoten (äquivalent)
Beispiel: Eisners Eiscreme 0.8 0. Eisner 0. Zustände: Wetter an bestimmtem Tag (ot, old) Beobachtungen: Wie viel Eis hat Jason an diesem Tag gegessen?
Beispiel: Eisners Eiscreme Anfangsw. a 0 Eisner 0.8 0. 0. Zustände: Wetter an bestimmtem Tag (ot, old) Beobachtungen: Wie viel Eis hat Jason an diesem Tag gegessen?
Beispiel: Eisners Eiscreme Anfangsw. a 0 Eisner 0.8 0. 0. Übergangsw. a Zustände: Wetter an bestimmtem Tag (ot, old) Beobachtungen: Wie viel Eis hat Jason an diesem Tag gegessen?
Beispiel: Eisners Eiscreme Anfangsw. a 0 0.8 0. Eisner 0. 0. Übergangsw. a 0. Zustände: Wetter an bestimmtem Tag (ot, old) Beobachtungen: Wie viel Eis hat Jason an diesem Tag gegessen?
Beispiel: Eisners Eiscreme Anfangsw. a 0 0.8 0. Eisner 0. Übergangsw. 0. a 0. Ausgabew. b () Zustände: Wetter an bestimmtem Tag (ot, old) Beobachtungen: Wie viel Eis hat Jason an diesem Tag gegessen?
Gemeinsame W. von x, y Gekoppelte Zufallsprozesse im MM definieren unmittelbar Modell für gemeinsame W. P(x, y) von y = y y T Sequenz von Beobachtungen x = x x T Sequenz von verborgenen Zuständen Definiert wie folgt: P (x, y) = P (x) P (y x) = TY P (X t = x t X t = x t ) TY P (Y t = y t X t = y t ) t= t= = TY a xt x t TY b xt (y t ) t= t=
Frage : Likelihood P(y) Wie w. ist es, dass Jason Eisner am ersten Tag Eis gegessen hat, am zweiten Tag Eis, am dritten Eis? Wollen berechnen: P(Y =, Y =, Y = ). Leicht zu berechnen: P(,,,,, ). Aber Ausgabe,, kann von vielen Zustands-Sequenzen erzeugt werden. Müssen Summe über alle diese Sequenzen bilden.
Naiver Ansatz Berechne P(,,), indem wir über alle Sequenzen von Zuständen summieren. P(,,) = P(,,,,,) + P(,,,,,) + P(,,,,,) +... + P(,,,,,) = 0.0 0.00 0.008 0.0004 0.06 Aufsummieren heißt technisch Marginalisierung. P (,, ) = X x,x,x Q P (x,,x,,x, )
Das ist zu teuer Naiver Ansatz summiert über exponentiell viele Terme. Das ist zu langsam für die Praxis. Idee: MM über die Zeit auffalten in einem Gitter (engl. trellis): eine Spalte für jeden Zeitpunkt t, repräsentiert X t jede Spalte enthält Kopie jedes Zustandes im MM Kanten von t nach t+ = Übergänge des MM Pfade im Gitter entsprechen Zustands-Sequenzen. d.h. Berechnung von P(w) = Summe über alle Pfade
Eiscreme-Gitter 0. 0. 0. 0.8 0. 0. 0. 0. 0. 0. t = t = t =
Eiscreme-Gitter 0. 0. 0. 0.8 0. 0. 0. 0. 0. 0. t = t = t =
Eiscreme-Gitter 0. 0. 0. 0.8 0. 0. 0. 0. 0. 0. t = t = t =
Eiscreme-Gitter 0. 0. 0. 0.8 0. 0. 0. 0. 0. 0. t = t = t =
Eiscreme-Gitter 0. 0. 0. 0.8 P =.0 0. 0. 0. 0. 0. 0. t = t = t =
Eiscreme-Gitter 0. 0. 0. 0.8 P =.0 0. 0. P =.00008 0. 0. 0. 0. t = t = t =
Eiscreme-Gitter 0. 0. 0. 0.8 P =.0 0. 0. P =.00008 0. P =.00004 0. 0. 0. t = t = t =
Eiscreme-Gitter 0. 0. 0. 0.8 P =.0 0. 0. P =.00008 0. P =.00004 P 4 =.00 0. 0. 0. t = t = t =
Eiscreme-Gitter 0. 0. 0. 0.8 P =.0 0. 0. P =.00008 0. P =.00004 P 4 =.00 0. 0. 0. t = t = t = P(,,) = P + P + P +... =.06
Der Forward-Algorithmus Naiver Algorithmus berechnet Zwischenergebnisse mehrfach. Das können wir vermeiden. Forward-W. α t (j): Wie w. ist es, dass MM y,, y t ausgibt und in Zustand X t = q j herauskommt? t (j) = P (y,...,y t,x t = q j ) X = P (y,...,y t,x = x,...,x t = x t,x t = q j ) x,...,x t Damit können wir leicht berechnen: P (y,...,y T )= X qq T (q)
Der Forward-Algorithmus t (j) =P (y,...,y t,x t = q j ) Basisfall, t = : (j) =P (y,x = q j )=b j (y ) a 0j Danach für alle t =,, T: t (j) =P (y,...,y t,x t = q j ) NX = P (y,...,y t,x t = q i ) P (X t = q j X t = q i ) P (y t X t = q j ) = i= NX t (i) a ij b j (y t ) i= t () q a j t () t () q q a j q j a j b j (y t ) y t
P(,,) mit Forward 0. 0. 0. 0.8 0. 0. 0. 0. 0. 0. t (j) =P (y,...,y t,x t = q j ) (j) =b j (y ) a 0j t (j) = NX t (i) a ij b j (y t ) i=
P(,,) mit Forward 0. 0. 0. α () =. 0.8 0. 0. 0. 0. 0. 0. t (j) =P (y,...,y t,x t = q j ) (j) =b j (y ) a 0j t (j) = NX t (i) a ij b j (y t ) i=
P(,,) mit Forward 0. 0. 0. α () =. 0.8 0. 0. 0. α () =.0 0. 0. 0. t (j) =P (y,...,y t,x t = q j ) (j) =b j (y ) a 0j t (j) = NX t (i) a ij b j (y t ) i=
P(,,) mit Forward 0. 0. 0. α () =. 0.8 α () =.04 0. 0. 0. α () =.0 0. 0. 0. t (j) =P (y,...,y t,x t = q j ) (j) =b j (y ) a 0j t (j) = NX t (i) a ij b j (y t ) i=
P(,,) mit Forward 0. 0. 0. α () =. 0.8 α () =.04 0. 0. 0. α () =.0 α () =.05 0. 0. 0. t (j) =P (y,...,y t,x t = q j ) (j) =b j (y ) a 0j t (j) = NX t (i) a ij b j (y t ) i=
P(,,) mit Forward 0. 0. 0. α () =. 0.8 α () =.04 α () =.0 0. 0. 0. α () =.0 α () =.05 0. 0. 0. t (j) =P (y,...,y t,x t = q j ) (j) =b j (y ) a 0j t (j) = NX t (i) a ij b j (y t ) i=
P(,,) mit Forward 0. 0. 0. α () =. 0.8 α () =.04 α () =.0 0. 0. 0. α () =.0 α () =.05 α () =.005 0. 0. 0. t (j) =P (y,...,y t,x t = q j ) (j) =b j (y ) a 0j t (j) = NX t (i) a ij b j (y t ) i=
P(,,) mit Forward 0. 0. 0. α () =. 0.8 α () =.04 α () =.0 0. α () =.0 0. 0. P(,,) = α () + α () =.06 α () =.05 α () =.005 0. 0. 0. t (j) =P (y,...,y t,x t = q j ) (j) =b j (y ) a 0j t (j) = NX t (i) a ij b j (y t ) i=
Frage : Tagging Gegeben Beobachtungen y,, y T, was ist wahrscheinlichste Zustandssequenz x,, x T? Maximale W.: max P (x,...,x T y,...,y T ) x,...,x T Wir interessieren uns v.a. für argmax: arg max x,...,x T P (x,...,x T y,...,y T ) P (x,...,x T,y,...,y T ) = arg max x,...,x T P (y,...,y T ) = arg max x,...,x T P (x,...,x T,y,...,y T )
Naive Lösung 0. 0. 0. 0.8 P =.0 0. 0. P =.00008 0. P =.00004 P 4 =.00 0. 0. 0. t = t = t = max P (x,,x,,x, ) x,x,x =0.0 arg max x,x,x P (x,,x,,x, ) =,,
Machen wir genau wie eben Likelihood: P(y) Forward-Algorithmus Tagging: argmax P(x,y) Viterbi-Algorithmus X x,...,x T P (x,...,x T,y,...,y T ) arg max x,...,x T P (x,...,x T,y,...,y T ) t(j) = X x,...,x t P (y,...,y t,x,...,x t,x t = q j ) V t (j) = max x,...,x t P (y,...,y t,x,...,x t,x t = q j ) P (y) = X qq T (q) max x P (x, y) = max V T (q) qq
Der Viterbi-Algorithmus V t (j) = max P (y,...,y t,x,...,x t,x t = q j ) x,...,x t Basisfall, t = : V (j) =b j (y ) a 0j Danach für alle t =,, T: V t (j) = N max i= V t (i) a ij b j (y t ) t () q a j t () q a j q j t () q a j y t b j (y t )
Viterbi: Beispiel 0. 0. 0. 0.8 V () =. 0. V () =.045 0. V () =.0 0. V () =.0 V () =.048 V () =.00 max P (x,,x,,x, ) x,x,x =0.0 0. 0. 0. V t (j) = max P (y,...,y t,x,...,x t,x t = q j ) x,...,x t V t (j) = N max i= V t (i) a ij b j (y t )
Backpointer Sobald wir alle V t (j) ausgerechnet haben, wollen wir eine Zustandssequenz x mit max. W. P(y x) ausrechnen. Merken uns während Berechnung von V t (j) für jedes (t,j), für welches i das Maximum erreicht wurde Backpointer bp t (j). Mit bp beste Sequenz x von rechts nach links ausrechnen: x T = arg max q V T (q) x t = bp t+ (x t+ ) für t = T-,,
Viterbi: Beispiel 0. 0. 0. 0.8 V () =. 0. V () =.045 0. V () =.0 0. V () =.0 V () =.048 V () =.00 max P (x,,x,,x, ) x,x,x =0.0 arg max P (x,,x,,x, ) x,x,x =,, 0. 0. 0. V t (j) = max P (y,...,y t,x,...,x t,x t = q j ) x,...,x t V t (j) = N max i= V t (i) a ij b j (y t )
Laufzeit Forward und Viterbi haben gleiche Laufzeit, wird vom Induktionsschritt dominiert: V t (j) = N max i= V t (i) a ij b j (y t ) Berechne N T Werte für V t (j). Brauche für jedes (t,j) Iteration über N Vorgänger-Zustände. Gesamt-Laufzeit ist O(N T), d.h. linear in Satzlänge quadratisch in Größe des Tagset
Zusammenfassung Markov-Ketten: Automatenbasierte Darstellung von Bigrammen. idden Markov Model: Automatenbasierte Erzeugung von Beobachtungen aus verborgenen Zuständen. eute gesehen: Forward-Algorithmus für Sprachmodellierung Viterbi-Algorithmus für Tagging