Statistical Machine Translation Foundations › Worked Example: Two EM Iterations by Hand
Language AI HandbookAll chapters 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 and our miniature target language "Zeta" as the target .
Training corpus, two sentence pairs:
- ,
- ,
The target vocabulary has three words, so we initialize everywhere.
Iteration 1, E-step. For sentence 1, the source is extended to . For the target word "ze":
so each source position receives posterior mass . 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 .
Iteration 1, M-step. Sum the counts. For source word "the", it appears in both sentences and receives from "ze" and from the other target word in each, for a total of . Its count for "ze" is , so
Similarly , , and the same for "dog". The model has already learned something: the cross-lexical entries dropped from to , and the "the"↔"ze" link rose to .
Iteration 2, E-step. Now the posteriors are no longer uniform. For "ze" in sentence 1, the denominator is
so each source position still gets a posterior of . For "mika" the denominator is
so "cat" gets posterior mass and the other two get each. The alignment is sharpening exactly where it should.
Iteration 2, M-step. For "the", the two occurrences of "ze" each contribute . "Mika" and "voso" each contribute in their respective sentences. The row total is , so
"Cat" appears only in sentence 1, where "ze" contributes and "mika" contributes . Dividing by their total, , gives and .
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]Expand
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]Expand

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!