LSTM-Attention模型:Effective Approaches to Attention-based Neural Machine Translation

参考链接

模型(model):

【1】Neural Machine Translation(NMT):

  • 神经机器翻译(NMT)是直接用神经网络去模拟条件概率: p ( x y ) p(x|y)
  • NMT一般由两个部分组成: e n c o d e r d e c o d e r encoder+decoder e n c o d e r encoder 部分读入源句子输出该句子的表示 (representation S S ), d e c o d e r decoder 部分接受 e n c o d e r encoder 部分的输出+ d e c o d e r decoder 已经输出的目标词作为输入并输出一个目标词。因此条件概率可以分解为: l o g p ( y x ) = x = 1 m l o g p ( y j y < j , s ) logp(y|x) = \sum_{x=1}^mlogp (y_j|y_{<j},s)
  • d e c o d e r decoder 去模拟该条件概率,因此可以进一步写作: l o g p ( y x ) = s o f t m a x ( g ( h j ) ) logp(y|x)=softmax(g(h_j)) g g 函数的输出向量的维数=词汇表的大小; h j h_j 是RNN隐藏状态向量,其公式如下: h j = f ( h j 1 , s ) h_j = f(h_{j−1}, s) f f 是RNN的单元可以是:标准的RNN单元、GRU单元和LSTM单元。
  • 模型图:
  • 这篇论文使用的模型是多层的LSTM+Attention机制;损失函数(目标函数): J t = ( x , y ) D l o g p ( y x ) J_t =\sum_{(x,y)∈D} − log p(y|x) D D 是语料库

【2】Attention-based Models

  • 论文中讲了两种模型: g l o b a l global l o c a l local ;两个模型图如下:

  • Global Attention:

    • 模型图正如上图所示现在解释一下里面的变量:
      • c t c_t :上下文向量;生成它是需要考虑 e n c o d e r encoder 的所有隐藏层状态向量 h t \overline{h_t}
      • a t a_t :对其向量(alignment vector);它的长度是可变的,其长度等于源句子的长度,计算公式如下: a t ( s ) = a l i g n ( h t , h s ) = e x p ( s o c r e ( h t , h s ) ) s e x p ( s c o r e ( h t , h s ) ) a_t(s)=align(h_t,\overline{h_s})=\frac{exp(socre(h_t,\overline{h}_s))}{\sum_{s'}exp(score(h_t,\overline{h_{s'}}))} s c o r e score 函数有三种形式: s o c r e ( h t , h s ) ) = { h t T h s d o t h t T W a h s g e n e r a l h a T t a n h ( W a [ h t ; h s ] ) c o n c a t socre(h_t,\overline{h}_s))= \begin{cases} h_t^T\overline{h_s} \qquad &dot \\ h_t^TW_a\overline{h_s} \qquad & general \\ h_a^Ttanh(W_a[h_t;\overline{h_s}]) \qquad & concat \end{cases}
  • 对于每个输入词 w s \overline{w}_s 经过 e n c o d e r encoder 都会产一个隐藏状态向量 h s \overline{h}_s ,当 d e c o d e r decoder 在翻译第 t t 个词 w t w_t 时, d e c o d e r decoder 先产生当前的隐藏状态向量 h t h_t ,然后根据 a t ( s ) a_t(s) 公式,为每个输入词 w s \overline{w}_s 计算出一个权值 a s a_s (实数),所有输入词的权值拼接成一个向量 a a ;即对齐向量(alignment vector);这也是为啥对齐向量长度为什么等于源句子长度的原因。对其向量的本质就是每个输入词的权重,这样我们根据该权重向量将输入词的隐藏状态向量 h s \overline{h}_s 进行加权平均得到上下文向量 C t C_t

  • 注意: 模型是使用多层的LSTM网络,上面所用的隐藏状态向量都是最顶层的LSTM的隐藏状态向量。

  • Local Attention:

    • L o c a l A t t e n t i o n Local Attention :选择性地关注输入句子中的一小窗口;这样可以减少计算量。
    • 第一步:在原句子中找到一个关注中心点 p t p_t
    • 第二步:确定关注区间 [ p t D , p t + D ] [p_t-D,p_t+D] ;D是事先被设置的常数。
    • 第三步:按照 G l o b a l A t t e n t i o n Global Attention 同样方法计算对其向量 a a 和上下文向量 c t c_t ,区别在于在 L o c a l A t t e n t i o n Local Attention 中只对窗口 [ p t D , p t + D ] [p_t-D,p_t+D] 中的输入进行计算,而 G l o b a l A t t e n t i o n Global Attention 对整个输入句子进行计算。由此可知 L o c a l A t t e n t i o n Local Attention 对其向量 a a 长度是固定的,其长度为窗口长度 D + 1 D+1 .
    • 确定中心点 p t p_t 有两种方法:
      • Monotonic alignment (local-m) :假设输入句子与输入句子单调对齐,直接令 p t = t p_t=t
      • Predictive alignment (local-p):先按照下面公式预测 p t p_t 点: p t = S s i g m o i d ( v p T t a n h ( W p h t ) ) p_t=S*sigmoid(v_p^Ttanh(W_ph_t)) 其中 W p W_p v p v_p 是可学习的参数; S S 是源句子的长度;则 p t [ 0 , S ] p_t\in{[0,S]} 是个实数; 这种情况下我们对齐向量 a a 的计算方式也有所不同: a t ( s ) = a l i g n ( h t , h s ) e x p ( ( s p t ) 2 2 σ 2 ) a_t(s) = align(h_t, \overline{h}_s) exp (−\frac{(s-p_t)^2}{2σ^2}) 其中 σ = D 2 σ=\frac{D}{2} s s 是窗口 [ p t S , p t + S ] [p_t-S,p_t+S] 内的整数。
  • Input-feeding Approach:

    • 模型图:
    • Input-feeding Approach 将上一步注意力向量(attentional vectors) h t ~ \tilde{h_t} 与当前步的输入进行拼接 [ t i l d e h t ; x ] [tilde{h_t};x] 作为当前时刻 d e c o d e r decoder 的输入以产生当前位置的目标词 y y
    • 这种做法的效果:
      • 使得当前模型充分考虑以前的对齐选择(alignment choices)。
      • 这使得我们可以创建了一个非常深的网络,并且横跨水平和垂直方向。
  • g l o b a l global l o c a l local 两个模型的公共部分

    • G l o b a l A t t e n t i o n GlobalAttention L o c a l A t t e n t i o n Local Attention 不同之处在于:上下文向量 c t c_t 的生成;其余部分都是相同的。
    • 注意力向量 h ~ t \tilde{h}_t 的生成: h ~ t = t a n h ( W c [ c t ; h t ] ) \tilde{h}_t = tanh(W_c[c_t; h_t]) 相对先将向量 c t c_t h t h_t 进行拼接,再将其输入到一个全连接的前馈神经网络中产生了注意力向量 h ~ t \tilde{h}_t .
    • 目标词 y t y_t 的生成:将 h ~ t \tilde{h}_t 输入到softmax层产生当前目标词 y t y_t 的条件概率分布: p ( y t y < t , x ) = s o f t m a x ( W s h ~ t ) p(y_t|y_{<t},x)= softmax(W_s\tilde{h}_t)
发布了105 篇原创文章 · 获赞 60 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/ACM_hades/article/details/104284606
今日推荐