Skip to content

下面是 Lecture 10 的期末复习版学习笔记,主题主线是:机器翻译从统计对齐模型,到搜索式解码,再到神经 Seq2Seq 与 Attention 的演进。内容依据课件 lecture-10 整理。

Lecture 10:Machine Translation, Decoding, Seq2Seq, Attention

0. 本章核心目标

本课件要解决的问题是:

给定一个外语句子 ,如何建模、生成、搜索并评价它的目标语言翻译

整个技术演进树可以概括为:

text
IBM Model 1
  ↓ 发现问题:词独立、alignment 均匀、不建模位置连续性
HMM Alignment Model
  ↓ 解决:用 Markov transition 建模相邻词对齐的局部性
Decoding / Search
  ↓ 问题:E 未知,搜索空间巨大,精确解码困难
Best-first / A* / Beam Search
  ↓ 问题:统计模型表达力有限,不能建模复杂语言模式
Seq2Seq Encoder-Decoder
  ↓ 问题:固定长度向量瓶颈、长距离依赖、RNN 串行
Attention Mechanism
  ↓ 解决:生成每个词时动态关注源句不同位置
Transformer
  ↓ 后续:彻底移除 RNN 串行瓶颈

1. IBM Model 1 的局限性

1.1 IBM Model 1 做了什么

IBM Model 1 是一个经典统计机器翻译模型。它假设外语句子 中的每个词 都由目标语言句子 中某个词 独立生成。

这里:

  • :目标语言句子,例如 English。
  • :外语句子,例如 French / Spanish。
  • :目标句长度。
  • :外语句长度。
  • :第 个外语词 对齐到的目标词位置。
  • :生成 的目标词。

1.2 IBM Model 1 的两个强假设

假设 1:词独立生成

IBM Model 1 假设:

彼此独立。

也就是说, 怎么生成,与 怎么生成无关。

问题

现实语言中,多个词经常是联合生成的。例如课件中提到,最后三个 French words 可能共同由最后一个 English word 生成。此时需要的是:

而不是简单地拆成:

假设 2:所有 alignment matrix 概率相同

IBM Model 1 认为所有对齐方式先验概率一样。

问题

这忽略了自然语言中的位置局部性:

相邻的外语词,通常会对齐到相邻的目标语言词。

例如连续两个 Spanish words 通常不会一个对齐到英文句首,另一个突然对齐到英文句尾。

重要程度:★★★★★ 考试重点:IBM Model 1 的失败点通常考 T/F 或 short answer。


2. HMM Alignment Model:把翻译对齐问题改写成序列标注问题

2.1 从 POS tagging 到 alignment

HMM 在 POS tagging 中做的是:

text
hidden states = POS tags
observations = words

机器翻译中可以类比为:

text
hidden states = alignment locations
observations = foreign words

也就是说:

  • POS tagging:预测每个词的词性。
  • Translation alignment:预测每个外语词对齐到目标句哪个位置。

于是 HMM 的元素对应关系为:

HMM in POS taggingHMM in translation
hidden state alignment position
observation foreign word
transition probability
emission probability

2.2 HMM 翻译模型的完整生成过程

课件中的联合分布可以写成:

其中:

  • :外语句子。
  • :目标语言句子。
  • :alignment sequence。
  • :目标句长度。
  • :外语句长度。
  • :给定目标句长度 ,生成外语句长度 的概率。
  • :transition probability,表示当前外语词 对齐到目标位置 的概率依赖于上一个外语词的对齐位置
  • :emission probability,表示目标词 生成外语词 的概率。

这个公式的逻辑是:

text
先决定 foreign sentence length J
然后从 j = 1 到 J:
    先根据上一个 alignment 位置生成当前 alignment
    再根据当前 alignment 对应的目标词生成 foreign word

2.3 Markov assumptions

原本最一般的模型可能是:

其中:

  • :从第 1 个到第 个外语词。
  • :前 个 alignment。
  • :前 个 alignment。
  • :完整目标语言句子。

这个模型太复杂,所以 HMM 做简化:

直觉:

第一条假设说,当前 alignment 只依赖于上一个 alignment 和目标句长度,不需要记住所有历史词。

第二条假设说,当前 foreign word 只由它对齐到的目标词生成,不依赖其他词。

重要程度:★★★★★ 考试重点:HMM 如何修正 IBM Model 1?答案就是:引入 alignment transition,建模 locality。


3. HMM 的 locality:为什么 transition probability 很关键

HMM 的核心改进不是 emission,而是 transition:

它希望鼓励:

也就是说,如果前一个外语词对齐到目标句第 3 个词,那么下一个外语词更可能对齐到第 3、4、5 个词,而不是突然跳到第 10 个词。

课件中强调 locality 是相对概念,而不是绝对位置。例如:

因为这两个 transition 的 jump 都是

所以可以定义 jump:

然后让 transition probability 主要依赖于 jump:

并且:

应该随着 增大而下降。

直觉:

text
小跳跃:高概率
大跳跃:低概率

例如:

jump直觉概率
,
,
, 很低

这就是 HMM 相比 IBM Model 1 的关键提升。


4. Translation Model 与 Decoding

4.1 HMM translation model

HMM 给出的 translation model 是:

其中:

  • :对所有可能 alignment sequence 求和。
  • 这表示我们不关心某一个具体 alignment,而是把所有可能 alignment 的概率加起来。

直觉:

text
一个 E 翻译成 F 的好坏,不只取决于词翻译概率,
还取决于是否存在合理的 alignment path。

4.2 Decoding:从 scoring 到 generation

翻译生成任务是:

其中:

  • :最优目标语言翻译。
  • :translation model,衡量 能否生成
  • :language model,衡量 本身是否流畅自然。

直觉:

text
好的翻译 = 忠实于源句 + 目标语言自然流畅

也就是:

注意:课件强调,这只是一个 scoring function,不代表搜索容易。

重要程度:★★★★★ 考试重点: 的含义。


5. 为什么 Decoding 很难

5.1 Alignment 和 decoding 是不同问题

如果 已知,HMM 可以用 Viterbi 找最优 alignment。

但真正翻译时:

这就变成了在所有可能英文句子中搜索。

5.2 复杂度问题

课件提到:带 bigram language model 的 general decoding 是 NP-complete。

原因是搜索空间极大:

text
候选词选择 × 词序排列 × phrase segmentation × language model dependency

所以实际机器翻译系统不能完全枚举所有 ,必须使用 heuristic search。


6. Search-based Decoding

6.1 Partial translation search tree

搜索式翻译把一个状态定义为 partial translation。

一个 partial translation 包含:

  • 已翻译的 foreign span。
  • 对应生成出来的 target words。
  • 当前 accumulated score。

搜索树节点就是 partial translations。


Best-first search 的策略是:

text
每次扩展当前分数最高的节点。

优点:

  • 简单。
  • 总是优先探索当前看起来最好的 translation。

缺点:

  • expensive:需要维护大量候选节点。
  • short-sighted:只看当前分数,容易陷入 local optimum。

例如当前翻译局部概率高,但未来无法形成流畅完整句子。

重要程度:★★★☆☆


A* search 改进 best-first 的方法是同时考虑:

text
当前质量 + 未来潜力

课件公式:

其中:

  • :当前 partial translation。
  • :当前 partial translation 的质量分数。
  • :尚未翻译部分的未来质量估计。
  • :综合优先级分数。

直觉:

text
不要只看现在翻译得好不好,
还要看剩下没翻译的部分有没有希望翻译好。

问题:

很难精确估计,所以需要 heuristic。

例如可以用 phrase table 中 remaining words 的简单 translation probability 作为未来分数估计。

重要程度:★★★★☆


Beam search 是神经机器翻译和传统 MT 都非常常见的近似搜索方法。

核心思想:

text
每一步只保留 top-k 个当前最好的状态。

其中 是 beam size。

流程:

text
Step 1: 初始化 beam
Step 2: 扩展 beam 中所有状态
Step 3: 对所有扩展结果打分
Step 4: 只保留 top-k
Step 5: 重复直到生成结束

优点:

  • 比 greedy 更稳。
  • 比 exhaustive search 便宜。
  • 实践中效果好。

缺点:

  • 不保证全局最优。
  • 太小容易错过好翻译。
  • 太大计算成本高。

重要程度:★★★★★ 考试重点:beam search 的定义和 top-k 保留机制。


7. BLEU:机器翻译自动评价

7.1 BLEU 的核心直觉

BLEU 衡量 candidate translation 和 reference translations 的 n-gram overlap。

直觉:

text
好的翻译应该和人工参考翻译共享较多 n-gram。

7.2 n-gram precision

,计算 n-gram precision:

其中:

  • :一个 candidate translation。
  • :候选翻译中的连续 个词。
  • :该 n-gram 在 candidate 中出现次数。
  • :该 n-gram 在 reference 中匹配的次数。
  • :n-gram precision。

BLEU 取几何平均:

课件中写作:

7.3 为什么用 geometric mean?

因为只看 unigram 会过于宽松。

例如 candidate 包含很多正确词,但词序完全错,也可能 unigram precision 很高。

加入 bigram、trigram、4-gram 后,可以同时评价:

  • word choice
  • local word order
  • phrase fluency
  • faithfulness

7.4 BLEU 的陷阱

陷阱 1:短翻译 precision 虚高

如果 reference 是:

text
the tight jump to the hole

candidate 是:

text
the

则 BLEU-1 unigram precision 是:

但这显然不是好翻译。

陷阱 2:重复词作弊

candidate:

text
the the the the the the the

reference:

text
the cat is on the mat

普通 precision 会误判很多 “the” 命中。

所以 BLEU 使用 modified precision,即 clipping。

reference 中 “the” 最多出现 2 次,所以 candidate 中最多只能算 2 个 “the” 命中:

重要程度:★★★★★ 考试重点:BLEU-1 计算、短句问题、modified precision。


8. Seq2Seq Encoder-Decoder

8.1 为什么需要 Seq2Seq

统计机器翻译依赖人工设计的 alignment、phrase table、search heuristic,表达能力有限。

Seq2Seq 的目标是用神经网络直接学习:

其中:

  • :source sentence。
  • :target sentence。
  • :输入长度。
  • :输出长度。

Seq2Seq 的优势是输入输出长度可以不同,适合:

  • machine translation
  • image captioning
  • music generation
  • summarization

8.2 Encoder

Encoder 是一个 RNN:

其中:

  • :第 个时间步的 encoder hidden state。
  • :前一时间步 hidden state。
  • :第 个输入 token 的 embedding。
  • :RNN transition function。
  • :模型参数。

直觉:

text
Encoder 从左到右读入 source sentence,
不断更新 hidden state,
最终得到一个压缩表示。

8.3 Decoder

Decoder 是另一个 RNN:

其中:

  • :第 个 decoder hidden state。
  • :前一 decoder hidden state。
  • :前一个生成的目标词。
  • :decoder RNN transition。
  • :参数。

Decoder 根据前一个词生成下一个词。

8.4 Training objective:MLE

给定训练样本:

最大似然目标是:

更准确地说,条件还应包括 source sentence:

课件中重点强调 decoder 的输出:

其中:

  • :第 步的 vocabulary logits。
  • :把 decoder hidden state 投影到词表空间的矩阵。
  • :decoder hidden state。
  • :把 logits 转换为词概率分布。

训练时通常使用 teacher forcing,即 decoder 的输入 使用真实前一个词,而不是模型自己预测的词。

重要程度:★★★★★ 考试重点:encoder/decoder 公式、MLE factorization、teacher forcing。


9. Seq2Seq 的核心问题

9.1 Fixed-length vector bottleneck

普通 Seq2Seq 把整个 source sentence 压缩到一个固定长度 hidden vector。

问题是:

text
无论输入句子多长,都必须压缩进同一个向量。

这对长句非常困难。

例如:

text
I am from China. I lived in Chicago for 6 years and then in Lehigh for another 6 years. I can speak fluent ____.

正确答案应该依赖很早出现的 China,但普通 RNN 更容易受近处词影响。

9.2 Long-range dependency failure

RNN 有 sequential recency bias。

较近 token 的信息经过更少矩阵乘法,更容易保留;较远 token 的信息经过许多递归更新,容易被冲淡。

例子:

错误:

text
The writer of the books are

正确:

text
The writer of the books is

真正主语是 writer,而不是 books。但 RNN 容易被最近的 books 干扰。

9.3 Gradient vanishing / explosion

BPTT 中,当前 token 的 error 要反传到很早之前的 token,需要经过多次矩阵乘法。

梯度可能:

  • vanish:变得接近 0。
  • explode:变得极大。

所以早期 token 难以被当前 loss 有效更新。

9.4 Cannot be parallelized

RNN 必须按时间顺序计算:

因此 依赖 ,无法像 CNN/Transformer 那样高效并行。

重要程度:★★★★★ 考试重点:fixed-length bottleneck、long-range dependency、BPTT、不能并行。


10. Attention Mechanism

10.1 Attention 要解决什么问题

普通 Seq2Seq 的问题是:

text
Decoder 只能依赖最后一个 encoder hidden state。

Attention 的改进是:

text
Decoder 每生成一个词,都可以动态查看 source sentence 的所有 hidden states。

生成 “cat” 时,它可以重点关注 source 中的 “chat”。

10.2 Query-Key-Value 直觉

课件用 Google search 类比:

概念含义
Query 当前 decoder state,表示“我现在想找什么信息”
Key source hidden states,用来匹配 query
Value source hidden states 中真正被取出的信息

在 RNN attention 中:

  • query 是 decoder 上一步状态:
  • keys 是 encoder hidden states:
  • values 也是 encoder hidden states:

10.3 Context vector

Attention 的核心公式:

其中:

  • :第 个 decoding step 的 context vector。
  • :source sentence length。
  • :source 第 个位置的 encoder hidden state。
  • :decoder 第 步对 source 第 个位置的 attention weight。

直觉:

text
c_t 是所有 source hidden states 的加权平均。
权重大,说明该位置对当前生成词更重要。

10.4 Attention weight

其中:

  • :第 个 decoding step 和第 个 source position 的匹配分数。
  • 分母对所有 source positions 做归一化。
  • 是 softmax 后的概率权重。

所以:

10.5 Score functions

课件给出两个例子:

Dot-product score

其中:

  • :decoder 上一步 hidden state。
  • :encoder 第 个 hidden state。
  • 内积越大,说明当前 decoder state 和 source position 越相关。

Bilinear score

其中:

  • :可学习参数矩阵。
  • 相比简单内积,bilinear score 多了一个可学习的匹配变换。

10.6 Decoder with attention

课件中的 decoder hidden state:

其中:

  • :当前 decoder hidden state。
  • :上一时刻 decoder hidden state。
  • :上一时刻生成词。
  • :当前 attention context。
  • :decoder transition function。

直觉:

text
当前生成不仅依赖之前生成了什么,
还依赖当前应该关注 source 的哪些位置。

10.7 Attention 的优势与不足

优势:

  • 缓解 fixed-length vector bottleneck。
  • 可以直接访问整个 source sentence。
  • 更适合 long-range dependency。
  • attention weights 有一定可解释性。

不足:

  • 如果 encoder 仍然是 RNN,则 encoder 本身还是 sequential。
  • 不能完全并行。
  • Transformer 后续通过 self-attention 进一步解决这个问题。

重要程度:★★★★★ 考试重点:、Score function、attention 如何解决 long-range dependency。


11. 方法演进对比表

阶段核心思想解决的问题仍然存在的问题重要程度
IBM Model 1每个 foreign word 独立由 target word 生成简化 word alignment不建模词依赖和位置局部性★★★★★
HMM Alignment用 Markov transition 建模 alignment sequence引入 locality仍是统计模型,表达力有限★★★★★
Decoding Objective同时考虑忠实度和流畅度搜索空间巨大★★★★★
Best-first Search每次扩展最高分 partial translation简单启发式搜索短视、昂贵、局部最优★★★☆☆
A* Search当前分数 + 未来估计缓解短视heuristic 难设计★★★★☆
Beam Search每步保留 top-k实用高效不保证全局最优★★★★★
BLEUn-gram overlap自动评价翻译短句、高频重复、语义等价问题★★★★★
Seq2SeqEncoder-Decoder 神经生成端到端学习翻译fixed vector bottleneck★★★★★
Attention动态关注 source positions缓解长距离依赖RNN encoder 仍慢★★★★★

12. 期末考高频题型整理

T/F 题

Q1:In IBM Model 1, two words are translated independently.

答案:True。

原因:IBM Model 1 假设每个 foreign word 条件独立生成。

Q2:By using HMM to model alignment matrix, many-to-many translations can be implemented.

答案:False。

原因:HMM alignment 仍然是每个 foreign word 选择一个 target position,本质是 many-to-one / one-to-many 的 alignment pattern,不是真正任意 many-to-many phrase translation。

计算题

Q:target translation 是

text
the tight jump to the hole

candidate 是

text
the

求 BLEU-1 unigram precision。

candidate 里只有一个 unigram:

reference 中包含:

所以命中 1 个,总数 1 个:

注意:这正体现了 BLEU 对短翻译的缺陷。

多选题

Q:Seq2Seq 的弱点包括什么?

(A) cannot encode the entire input sentence (B) earlier errors can lead to future mistakes (C) lack of non-linear mapping in the model

答案:A, B。

解释:

  • A 对,因为 fixed-length hidden vector 很难编码整个长句。
  • B 对,因为 decoder 是 autoregressive,前面预测错会影响后面。
  • C 错,因为 RNN/GRU/LSTM 本身有非线性映射。

13. 一句话抓重点

这节课的主线不是零散介绍 MT 技术,而是:

text
IBM Model 1 太独立 → HMM 加入 alignment locality;
解码太难 → 用 heuristic search;
统计模型表达力弱 → Seq2Seq 端到端生成;
Seq2Seq 压缩瓶颈严重 → Attention 动态读取源句信息。

最核心的考试公式是:

Static academic notes built with VitePress and KaTeX.