8 Machine Translation, Seq2Seq and Attention

1 Pre-Neural Machine Translation

机器翻译(Machine Translation, MT)的就是将一种语言(source language)的句子翻译到另一种语言(target language)的句子。

关于machine translation的研究最早能追溯到20世纪50年代,冷战时期,美国人需要将俄语翻译成英语,这时的MT系统大多数是基于规则的(rule-based),使用一个双语词典做俄语到英语的映射。

1990s-2010s的方法主要是统计机器翻译(Statistical Machine Translation,SMT),它的一个主要思想是从数据中学习一个概率模型,假设任务是将法语翻译成英语,source language-法语的句子为x,target language-英语的句子为y,SMT目标就是:
argmaxyP(yx) \arg \max_{y} P(y|x)
使用贝叶斯公式(Bayes Rule),
=argmaxyP(xy)P(y) =\arg \max_{y} P(x|y)P(y)
两部分可以分开学习,第一部分P(xy)P(x|y)是翻译模型(translation model),从parallel data中学习单词和短语如何翻译 (追求的是 fidelity) 。第二部分P(y)P(y)是语言模型(language model),从monolingual data中学习如何生成流利的英语 (追求的是 fluency )。换句话说,翻译追求的信达雅,信就是translation model的目标,达就是language model的目标,雅还不在考虑范围。

如何学习translation model P(xy)P(x|y)

需要大量parallel data,就是表达同样意思但是语言不同的数据。对于P(xy)P(x|y)进一步分解,P(x,ay)P(x, a|y),其中aa代表指派(alignment),即法语句子x与英语句子y之间词一级别的联系,例如Japan-Japon,two-deux。

如何计算argmax

简单的想法枚举出每个可能的y然后计算概率,但是这种方法too expensive。一般的做法是使用一个启发式搜索算法(heuristic search algorithm)来搜寻最好的翻译,这个过程被称作解码(decoding)。

2 Neural Machine Translation(NMT)

2.1 seq2seq

Neural Machine Translation就是指使用一个神经网络来做机器翻译,这种神经网络结构被称为sequence-to-sequence(seq2seq),其中包含两个RNN。

cs224笔记:Lecture 8 Machine Translation, Seq2Seq and Attention

seq2seq模型如上,包括两个RNN,一个作为编码器(encoder),将源句子(source sentence)通过RNN编码成向量表达,然后作为解码器(decoder)的输入,然后解码器不断的生成目标句子(target sentence)。

seq2seq结构不仅仅只能做机器翻译,它可以用于很多NLP任务中,例如summarization,dialogue,parsing,code generation。

seq2seq模型可以视为条件语言模型(conditional language model),称之为language model的原因是解码器预测target sentence中的下一个单词,称之为conditional的原因是预测基于给定句子的条件下。

2.2 Decoding

Greedy Decoding

所谓greedy decoding就是在解码的过程中,每一个时刻生成一个预测,这种方法显然有其漏洞,因为很多时候获取到后面的信息后,会对前面已生成的内容有所改变,而greedy encoding无法更改。

Exhaustive search Decoding

一个naive的解决greedy decoding的办法就是在解空间内做搜索,例如长度为T的序列,就是最大化下面的概率:
P(yx)=P(y1x)P(y2y1,x)P(yTy1,yT1,x)=t=1TP(yty1,,yt1,x) P(y|x)=P(y_1|x)P(y_2|y_1,x)\cdots P(y_T|y_1, \dots y_{T-1},x)\\ = \prod_{t=1}^T P(y_t|y_1, \dots , y_{t-1},x)
我们对每一个可能的序列y,都可以求出上面的概率,然后选出最大概率的序列作为预测。但是这样的做法计算过于昂贵,因为这样的序列有VT|V|^T个,其中V|V|是词典大小。

Beam search Decoding

针对exhaustive search的弊端,一个解决方案就是beam search,中心思想就是在解码器的每一步生成k个最可能的预测值(称之为hypothesis),k代表beam的大小,一般5到10,这样大大减少了计算量,而且有效的保存了许多可能性。

对hypotheis打分,保留分数高的k个hypothesis,计算如下
score(y1,,yt)=logP(y1,,yt)=i=1tlogP(yiy1,,yi) score(y_1, \dots, y_t)=\log P(y_1, \dots, y_t) = \sum_{i=1}^{t} \log P(y_i | y_1, \dots, y_i)

例如一个k=2的decoding过程如下:

cs224笔记:Lecture 8 Machine Translation, Seq2Seq and Attention

在第一步时,通过计算,挑选k个得分最大的hypothesis保留下来,logP(he<START>)=0.7\log P(he|<START>) = -0.7logP(I<START>)=0.9\log P(I|<START>) = -0.9;第二步的时候,再计算hypothesis,然后挑选k个得分最大的,logP(hit<START>,he)+0.7=(1.7)\log P(hit|<START> , he) + -0.7 = (-1.7)logP(struck<START>,he)+(0.9)=2.9\log P(struck|<START>, he) + (-0.9) = -2.9logP(was<START>,I)+(0.9)=1.6\log P(was|<START>, I) + (-0.9)= -1.6logP(got<START>,I)+(0.9)=1.8\log P(got|<START>, I) + (-0.9)= -1.8,然后再在这k×k=4k\times k=4个结果中挑选k个最大的hypothesis,到最后一步backtrack获取整个序列。

对于上面beam search的一个问题就是,序列越长则他的score越小(由score定义的式子可以看出)。解决方法就是做一个长度的正则化: 1ti=1tlogP(yiy1,,yi)\frac{1}{t} \sum_{i=1}^{t} \log P(y_i | y_1, \dots, y_i)

2.3 Cons & Pros

NMT相较以前SMT的优点有:1、更好的性能,更加流畅,更好的利用context信息;2、使用一个神经网络可以端到端的优化(end-to-end),这样不需要对一些子系统做单独优化:3、不需要太多人工的特征工程
缺点有:1、很少的可解读性(interpretable);2、难以控制,难以对翻译规定规则,加入先验知识

2.4 评估系统的方法

BLEU(Bilingual Evaluation Understudy)

BLEU compares the machine-written translation to one or several human-written translation(s), and computes a similarity score based on:

n-gram precision (usually for 1, 2, 3 and 4-grams)

• Plus a penalty for too-short system translations

3. Attention

seq2seq一个瓶颈是encoder太难了,需要把所有输入序列编码到一个向量里面,难度过大。所以提出了attention机制,以更好的获取输入序列的信息,中心思想就是decoder直接和输入序列连接,根据”自己“的需要,获取其中的信息。

如下图,在第一个时刻,解码器会获取每个时刻编码器隐层的信息,做一个点积,然后接softmax,获得一个概率分布,称之为attention distribution,根据这个概率分布获取编码器隐层的加权和,然后和编码器输出拼接起来,作为这个时刻解码器的输入做计算。

cs224笔记:Lecture 8 Machine Translation, Seq2Seq and Attention

形式解释如下,

encoder hidden states:h1,hNRh\mathbf{h}_1, \dots \mathbf{h}_N \in \mathbb{R}^h

timestep t, decoder hidden state: stRh\mathbf{s}_t \in \mathbb{R}^h

attension socres at this timestep: et=[stTh1,,stThN]RN\mathbf{e}^t = [\mathbf{s}_t^T\mathbf{h}_1,\dots, \mathbf{s}_t^T\mathbf{h}_N] \in \mathbb{R}^N

softmax to get attention distribution: αt=softmax(et)RN\alpha^t = softmax(\mathbf{e}^t) \in \mathbb{R}^N

get weighted sum of encoder hidden states : at=i=1Nαithi\mathbf{a}^t = \sum_{i=1}^{N} \alpha^t_i \mathbf{h}_i

Concatanate: [at;st]R2h[\mathbf{a}^t; \mathbf{s}^t] \in \mathbb{R}^{2h}

相关文章:

  • 2021-12-23
  • 2021-05-22
  • 2021-04-20
  • 2021-12-09
  • 2021-05-30
  • 2021-12-21
  • 2021-04-23
  • 2021-04-01
猜你喜欢
  • 2021-06-22
  • 2021-09-09
  • 2021-11-08
  • 2021-09-17
  • 2021-05-16
  • 2021-08-05
  • 2021-09-03
相关资源
相似解决方案