Statistical Machine Translation Foundations › Worked Example: Two EM Iterations by Hand

Michael BrenndoerferNovember 26, 20264 min read
Language AI HandbookAll chapters
Part 27 of 67Machine Translation and Speech Translation
Chapter 190Statistical Machine Translation Foundations › Worked Example: Two EM Iterations by Hand
Reading section 8 of 12 · Worked Example: Two EM Iterations by Hand · 4 minSection 8 of 12 · 4 min

Section 8 of 12 · 4 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

Before writing code, let us run IBM Model 1 by hand on a tiny corpus so the mechanics are unambiguous. Use English as the source e\mathbf{e} and our miniature target language "Zeta" as the target f\mathbf{f}.

Training corpus, two sentence pairs:

  1. e=(the,cat)\mathbf{e} = (\text{the}, \text{cat}), f=(ze,mika)\mathbf{f} = (\text{ze}, \text{mika})
  2. e=(the,dog)\mathbf{e} = (\text{the}, \text{dog}), f=(ze,voso)\mathbf{f} = (\text{ze}, \text{voso})

The target vocabulary has three words, so we initialize t(f∣e)=1/3t(f \mid e) = 1/3 everywhere.

Iteration 1, E-step. For sentence 1, the source is extended to (NULL,the,cat)(\text{NULL}, \text{the}, \text{cat}). For the target word "ze":

denom=t(ze∣NULL)+t(ze∣the)+t(ze∣cat)=13+13+13=1,\text{denom} = t(\text{ze}\mid\text{NULL}) + t(\text{ze}\mid\text{the}) + t(\text{ze}\mid\text{cat}) = \tfrac13 + \tfrac13 + \tfrac13 = 1,

so each source position receives posterior mass 13\tfrac13. The same holds for "mika". Sentence 2 has the same structure at initialization, so the same computation applies to "ze" and "voso". The posterior table after the E-step is therefore uniform: every (source position, target word) pair inside a sentence gets 13\tfrac13.

Iteration 1, M-step. Sum the counts. For source word "the", it appears in both sentences and receives 13\tfrac13 from "ze" and 13\tfrac13 from the other target word in each, for a total of 43\tfrac43. Its count for "ze" is 23\tfrac23, so

t(ze∣the)=2/34/3=0.5,t(mika∣the)=t(voso∣the)=0.25.t(\text{ze} \mid \text{the}) = \frac{2/3}{4/3} = 0.5, \qquad t(\text{mika}\mid\text{the}) = t(\text{voso}\mid\text{the}) = 0.25.

Similarly t(ze∣cat)=0.5t(\text{ze}\mid\text{cat}) = 0.5, t(mika∣cat)=0.5t(\text{mika}\mid\text{cat}) = 0.5, and the same for "dog". The model has already learned something: the cross-lexical entries dropped from 0.3330.333 to 0.250.25, and the "the"↔"ze" link rose to 0.50.5.

Iteration 2, E-step. Now the posteriors are no longer uniform. For "ze" in sentence 1, the denominator is

t(ze∣NULL)+t(ze∣the)+t(ze∣cat)=0.5+0.5+0.5=1.5,t(\text{ze}\mid\text{NULL}) + t(\text{ze}\mid\text{the}) + t(\text{ze}\mid\text{cat}) = 0.5 + 0.5 + 0.5 = 1.5,

so each source position still gets a posterior of 0.5/1.5=130.5 / 1.5 = \tfrac13. For "mika" the denominator is

t(mika∣NULL)+t(mika∣the)+t(mika∣cat)=0.25+0.25+0.5=1.0,t(\text{mika}\mid\text{NULL}) + t(\text{mika}\mid\text{the}) + t(\text{mika}\mid\text{cat}) = 0.25 + 0.25 + 0.5 = 1.0,

so "cat" gets posterior mass 0.5/1.0=0.50.5 / 1.0 = 0.5 and the other two get 0.25/1.0=0.250.25 / 1.0 = 0.25 each. The alignment is sharpening exactly where it should.

Iteration 2, M-step. For "the", the two occurrences of "ze" each contribute 13\tfrac13. "Mika" and "voso" each contribute 14\tfrac14 in their respective sentences. The row total is 23+14+14=76\tfrac23+\tfrac14+\tfrac14=\tfrac76, so

t(ze∣the)=2/37/6=47,t(mika∣the)=t(voso∣the)=314.t(\text{ze}\mid\text{the}) = \frac{2/3}{7/6} = \tfrac47, \qquad t(\text{mika}\mid\text{the}) = t(\text{voso}\mid\text{the}) = \tfrac{3}{14}.

"Cat" appears only in sentence 1, where "ze" contributes 13\tfrac13 and "mika" contributes 12\tfrac12. Dividing by their total, 56\tfrac56, gives t(mika∣cat)=35t(\text{mika}\mid\text{cat})=\tfrac35 and t(ze∣cat)=25t(\text{ze}\mid\text{cat})=\tfrac25.

The two updates shift lexical probability toward the recurring correspondence between "the" and "ze" and toward the content words' partners. The expected counts remain fractional. The plotted values show how the finite run behaves on this specific two-pair corpus.

The hand-worked example shows the direction of the updates. Repeating the same deterministic EM updates for ten iterations makes the pace of convergence visible.

PythonIn [5]
CollapseExpand
from collections import defaultdict

import numpy as np
from book_plot_style import PALETTE

tiny_pairs = [
    (("the", "cat"), ("ze", "mika")),
    (("the", "dog"), ("ze", "voso")),
]
tiny_src = ["<NULL>", "the", "cat", "dog"]
tiny_tgt = ["ze", "mika", "voso"]
tiny_t = {e: {f: 1.0 / len(tiny_tgt) for f in tiny_tgt} for e in tiny_src}
tiny_iters = 10
tiny_tracked = [("the", "ze"), ("cat", "mika"), ("the", "mika")]
tiny_curves = {pair: [] for pair in tiny_tracked}

for _ in range(tiny_iters):
    for pair in tiny_tracked:
        e, f = pair
        tiny_curves[pair].append(tiny_t[e][f])
    counts = {e: defaultdict(float) for e in tiny_src}
    totals = defaultdict(float)
    for src, tgt in tiny_pairs:
        src_ext = ["<NULL>"] + list(src)
        for f in tgt:
            denom = sum(tiny_t[e][f] for e in src_ext)
            for e in src_ext:
                mass = tiny_t[e][f] / denom
                counts[e][f] += mass
                totals[e] += mass
    for e in tiny_src:
        denom = totals[e] if totals[e] > 0 else 1.0
        tiny_t[e] = {f: counts[e][f] / denom for f in tiny_tgt}

tiny_x = np.arange(1, tiny_iters + 1)
tiny_series = [
    ("t(ze | the)", tiny_curves[("the", "ze")], PALETTE["primary"]),
    ("t(mika | cat)", tiny_curves[("cat", "mika")], PALETTE["amber"]),
    ("t(mika | the)", tiny_curves[("the", "mika")], PALETTE["muted"]),
]
VisualizationOut [6]
CollapseExpand
Line plot shows t(ze | the) and t(mika | cat) rising and t(mika | the) falling from the same initial probability of one-third.
Lexical probabilities before each EM update for three word pairs in the two-sentence example. The two recurring correspondences gain probability, while t(mika | the) loses probability.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy