Lecture 8: Machine Translation, Sequence-to-sequence and Attention

Here Insert Picture Description

Pre-Neural Machine Translation

machine translation

  • Machine translation (MT) is a sentence x conversion task to another language (target language) y sentences from one language (source language).
  • 1950s: Early Machine Translation
    will translate Russian into English (motivated by Cold War)
    is basically a rule-based, using a bilingual dictionary of English words with the Russian word association
  • 1990s-2010s: Statistical Machine Translation
    • The core idea: learning a probabilistic model from the data
    • Suppose you are given the French sentence x, to find the best English translation y, a r g m a x y P ( y x ) argmax_yP (y | x)
      using Bayes' rule to Equation divided into two parts, part of the translation model, part of the language model.
      Here Insert Picture Description
      Translation model: how to convert words and phrases modeling (loyalty), learning from the parallel corpus
      language model: how to write good English sentence Modeling (fluency), learning from monolingual corpora

Translation model

  • 语言模型之前已经介绍过了,比较简单,而翻译模型很难。
  • 因为源语言句子和目标语言句子长度不一定相同,所以就会涉及到单词或短语的对齐问题。有一对多,多对一,一对NULL,NULL对有,等等。
  • IBM提出了五个经典的翻译模型,前三个是基于单词的,后两个是基于短语的。由于时间以及篇幅,还有本人也没有完全搞懂,所以这里就不深入研究了。本课中也未深入讲解,有兴趣同学可以查阅资料。

Neural Machine Translation

  • 神经机器翻译(NMT)是用单神经网络进行机器翻译的一种方法。
  • 神经网络结构称为sequence-to-sequence (也称为seq2seq),它包含两个rnn。
    Here Insert Picture Description
    seq2seq架构包含两个RNN,一个用作编码器,对输入序列进行编码获得其表示。一个用作解码器,编码器最终的隐藏层输出作为解码器的起始输入,最终生成目标句子。上图中解码器部分是test时的架构(将当前时间步的输出当作下一个时间步的输入)
  • seq2seq是多功能的
    • 不仅在MT中有用,在许多NLP任务中都有作用
      文本摘要( long text → short text)
      对话系统(previous utterances → next utterance)
      解析(input text → output parse as sequence)
      代码生成(natural language → Python code)
  • seq2seq模型是条件语言模型的一个例子。
    • 语言模型:因为解码器预测目标句子y的下一个词
    • 条件性:因为预测是基于源语言x的
  • NMT直接计算 p ( y x ) p(y|x)
    Here Insert Picture Description

How to train a NMT system?

首先需要获取一个大的平行语料
Here Insert Picture Description
如上图,seq2seq训练过程与测试过程不同。在训练过程中,解码器部分每个时间步输入是目标序列,而每个时间步的正确结果为下一个时间步的输入,损失函数使用交叉熵即可。

搜素策略

Greedy decoding

Here Insert Picture Description

  • 在解码器的每个时间步使用argmax来生成(或“解码”)目标语句,即每步都选择概率最大的一个词
  • 这样会出现一些问题
    Here Insert Picture Description
    贪婪解码没有方法去撤销每一步的决定
    如何解决呢?

Exhaustive search decoding

  • 理想情况下,我们要找到一个翻译y能够最大化
    Here Insert Picture Description
  • 穷举搜索解码,顾名思义,就是将每步的所可能性都记录下来,这样就能计算所有可能的目标句子y
    • 这就意味着在解码器的第 t 步,我们要跟踪 V t V^t 种可能的部分翻译,这里V是词表大小
    • 复杂度为 O ( V T ) O(V^T) ,计算量太大了。

Beam search decoding

  • 核心思想:在解码器的每一步,只跟踪前 k 种最有可能的部分翻译,我们称之为假设。k 是 beam size(实际中大约为5~10)

  • 一个假设的得分是其对数概率
    Here Insert Picture Description
    所有分数都是负的,越高越好
    在每个时间步,我们搜索高分的假设,跟踪前 k 个

  • Beam search 不保证找到最优解,但是比穷尽搜索更加有效

  • 例子
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture DescriptionHere Insert Picture Description
    中间过程省略,得到最终结果为
    Here Insert Picture Description
    然后回溯找到最优解
    Here Insert Picture Description

  • 停止条件:

    • 在greedy docoding中,经常解码得到<END>时停止
    • 在beam search中,不同的假设可能在不同的时间步产生<END>
      • 当一个假设产生<END>,这个假设就完整了
      • 将它放到一边,然后继续通过beam search搜索其他假设
    • 通常,在以下情况发生时,停止
      • 到达时间步T(T是提前设定好的截止值),或者
      • 当我们至少含有 n 个完整的假设(n也是提前设定好的戒指值)
  • 选择哪个作为最终的翻译?

    • 停止后,我们有一个假设的列表
    • 每个假设 y 1 , y 2 , , y t y_1, y_2, ···, y_t 都有一个分数
      Here Insert Picture Description
      如果单纯选择分数最高的一个有些问题,因为越长的假设分数越低,偏向于选择较短的假设。
    • 因此,按长度先正则化,然后选择最优的假设:
      Here Insert Picture Description

NMT的优缺点

  • Advantages of NMT
    比起SMT,NMT有很多优点
    • 更好的效果:更加流利、对上下文利用更充分、对短语相似性利用更加充分
    • 到端优化的单一神经网络,没有要单独优化的子组件
    • 需要更少的人力工程:无特征工程,对所有语言对使用相同的方法
  • Disadvantages of NMT
    比起SMT
    • NMT缺乏可解释性:很难去debug
    • NMT很难控制:例如,无法轻松指定翻译规则或指导原则,安全问题

MT的评价

  • BLEU(Bilingual Evaluation Understudy)
    BLEU通过比较机器翻译和人工翻译,并基于n-gram精确率(通常使用1,2,3和4-grams),再加上对太短句子翻译的惩罚,计算出一个相似性分数。

  • BLEU很有用但不完美

    • 翻译一个句子有很多有效的方法
    • 因此,一个好的翻译可能得到一个很差的BLEU分数,因为它与人类翻译有很低的n-gram重叠
  • MT的进步历程
    Here Insert Picture Description
    NMT是NLP深度学习的最大成功案例

  • 神经机器翻译从2014的边缘研究活动到2016的领先标准方法。

    • 2014年,第一篇seq2seq论文发表
    • 2016年,谷歌翻译从SMT转为NMT
  • 这很令人惊奇
    SMT系统是由数百名工程师多年来建造的,其性能在几个月内被少数工程师训练的NMT系统超越。

那么机器翻译问题解决了吗?

  • 并没有
  • 还有很多问题等着去解决
    • 词汇量不足的词
    • 训练与试验数据之间的域不匹配
    • 在较长文本上维护上下文
    • 缺乏资源的语言对
    • NMT在训练数据继承了偏差
      Here Insert Picture Description
    • 不可解释的系统有时候会做奇怪的事
      Here Insert Picture Description

NMT研究还在继续

  • NMT是NLP深度学习的旗舰任务
  • NMT研究开创了NLP深度学习的许多最新创新
  • 研究人员对基础RNN提出了很多的改进,比如其中之一就是ATTENTION注意力机制

Attention

瓶颈问题
Here Insert Picture Description
编码器需要将整个源语言句子的编码集中到最后一个隐藏层的输出中,这就产生了一个信息瓶颈。

Attention

  • 注意力机制提供了一个解决这个问题的方法

  • 核心思想:在解码器的每个时间步,让其与编码器的某些时间步直接连接来更加关注源语言句子的某些特定部分。

  • 以下是解码过程的图示
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description

  • 用等式来描述:

    • 编码器隐藏层状态 h 1 , h 2 , , h N R h h_1, h_2, ···, h_N \in R^h
    • 在时间步 t ,解码器隐藏层状态 s t R h s_t \in R^h
    • 在这一步中,获得attention scores e t e^t (使用点乘)
      Here Insert Picture Description
    • 然后使用softmax获得attention distribution α t \alpha^t
      Here Insert Picture Description
    • α t \alpha^t 作为权重,使用编码器隐藏状态的加权和来获得注意力输出 α t \alpha_t
      Here Insert Picture Description
    • 最后,将注意力输出 α t \alpha_t 与解码器隐藏层状态 s t s_t 拼接在一起,根据无注意力机制的seq2seq模型进行处理
      Here Insert Picture Description

注意力机制非常棒

  • 显著的提高了NMT的性能
    • 让解码器关注输入的某些特定部分非常有用
  • 注意力解决了瓶颈问题
    • 注意力允许解码器直接查看输入;绕过了瓶颈
  • 注意力有助于解决消失梯度问题
    • 提供通往遥远状态的瓶颈
  • 注意力提供了一些可解释性
    Here Insert Picture Description
    • 通过观察注意分布,我们可以看到解码器关注的是什么
    • 我们免费获得(soft)对齐!
    • 这很棒,因为我们从未明确训练过对齐系统,这个网络自动学习了对齐

Attention is a general Deep Learning technique

  • 不仅在MT中有用,在一些其他NLP任务中也很有用。
  • Attention more general definition
    given to a set of values and a query vector, attention is a weighted sum of the calculated values according to the query techniques. (In the MT example, the value herein refers to the encoder output, the query vector hidden layer state decoder means at each time step)
  • We sometimes say: query attends to the values
    Example: seq2seq + attention model, each decoder hidden (query) concern all encoders hidden layer state (values)
  • Intuitively:
    • Is a selective weighting and the value of the digest information contained in the query to determine which values ​​Follow
    • A method of obtaining a focus is represented by any set (value) of the fixed-size representation, it depends on some other representation (query).

Attention mechanism is divided into the following three steps
Here Insert Picture Description

Several variants of attention Here Insert Picture Description
of the three methods, the first two require the same dimensions, can be used as the dot.
The second is to use a weighting matrix to be converted into the dimension value and then again query the same dot.
The third is a value query and by the weight matrix are transformed into a particular dimension (attention dimensionality is a hyper-parameters), then through a nonlinear function tanh, and multiplied by a weight matrix.

to sum up

  • Learn the history of the MT (SMT difficult, not in-depth description)
  • Since 2014, machine translation nerves quickly replaced the complex statistical machine translation
  • seq2seq architecture
  • Focus mechanism is a way to enter a specific part of the concerns of the system to improve the lot of seq2seq
Published 29 original articles · won praise 10 · views 7164

Guess you like

Origin blog.csdn.net/weixin_42017042/article/details/104162592