前言
本文來講一講應用於seq2seq模型的兩種attention機制:Bahdanau Attention和Luong Attention。文中用公式+圖清晰地展示了兩種注意力機制的結構,最後對兩者進行了對比。seq2seq傳送門:click here.
文中爲了簡潔使用基礎RNN進行講解,當然現在一般都是用LSTM,這裏並不影響,用法是一樣的。另外同樣爲了簡潔,公式中省略掉了偏差。
第一種attention結構:Bahdanau Attention
兩種機制基於上篇博客 第一種seq2seq結構。Encoder生成的語義向量 c {\color{Red} {c}} c 會傳給Decoder的每一時刻,傳給每一時刻的語義向量都是同一個 c {\color{Red} {c}} c ,這是不合理的。比如翻譯一句話,I like watching movie.
翻譯成:我喜歡看電影。
,其中喜歡
基本上是由like
得來的,I like watching movie.
中每個詞對翻譯成喜歡
的影響是不同的。所以,在Decoder中,每個時刻的語義向量 c t {\color{Red} {c_t}} c t 都應該是不同的。
該模型來自於Bahdanau et.al(2014) ,模型框架如下圖:
計算公式如下更方便理解。
Encoder:
h i = t a n h ( W [ h i − 1 , x i ] ) o i = s o f t m a x ( V h i )
\begin{aligned}
h_i &=tanh(W[h_{i-1},x_i])\\
o_i &=softmax(Vh_i) \\
\end{aligned}
h i o i = t a n h ( W [ h i − 1 , x i ] ) = s o f t m a x ( V h i )
Decoder:
分爲兩步:
第一步,生成該時刻語義向量:
c t = ∑ i = 1 T α t j h i α t i = e x p ( e t i ) ∑ k = 1 T e x p ( e t k ) e t i = V a ⊤ t a n h ( w a [ s i − 1 , h i ] )
\begin{aligned}
{\color{Red} {c_t}} &=\sum ^T_{i=1} \alpha_{tj}h_i\\
\alpha_{ti} &=\frac{exp(e_{ti})}{\sum^T_{k=1}exp(e_{tk})}\\
e_{ti} &=V_a^{\top}tanh(w_a[s_{i-1},h_i])\\
\end{aligned}
c t α t i e t i = i = 1 ∑ T α t j h i = ∑ k = 1 T e x p ( e t k ) e x p ( e t i ) = V a ⊤ t a n h ( w a [ s i − 1 , h i ] )
其中 c t {\color{Red} {c_t}} c t 是 t 時刻的語義向量;e t i e_{ti} e t i 是Encoder中 i 時刻 Encoder隱層狀態 h i h_i h i 對Decoder中 t 時刻隱層狀態 s t s_t s t 的影響程度;通過softmax函數(第二個式子)將 e t i e_{ti} e t i 概率歸一化爲 α t i \alpha_{ti} α t i 。
第二步,傳遞隱層信息並預測:
s t = t a n h ( W [ s t − 1 , y t − 1 , c t ] ) o t = s o f t m a x ( V s t )
\begin{aligned}
s_t &=tanh(W[s_{t-1},y_{t-1},{\color{Red} {c_t}}])\\
o_t &=softmax(Vs_t) \\
\end{aligned}
s t o t = t a n h ( W [ s t − 1 , y t − 1 , c t ] ) = s o f t m a x ( V s t )
第二種attention結構:Luong Attention
該模型來自於Luong et.al(2015) ,模型框架如下圖:
與第一種attention結構區別在Decoder部分 ,Encoder部分完全相同。Decoder還是分兩步,與前者的區別部分在公式中用綠色字體標出:
第一步,生成該時刻語義向量:
c t = ∑ i = 1 T α t j h i α t i = e x p ( e t i ) ∑ k = 1 T e x p ( e t k ) e t i = s t ⊤ W a h i
\begin{aligned}
{\color{Red} {c_t}} &=\sum ^T_{i=1} \alpha_{tj}h_i\\
\alpha_{ti} &=\frac{exp(e_{ti})}{\sum^T_{k=1}exp(e_{tk})}\\
e_{ti} &={\color{Green} {s_t^{\top}W_ah_i}}\\
\end{aligned}
c t α t i e t i = i = 1 ∑ T α t j h i = ∑ k = 1 T e x p ( e t k ) e x p ( e t i ) = s t ⊤ W a h i
可以看出區別在計算影響程度 e t i e_{ti} e t i 這個公式,這裏我只寫出了最優公式,有興趣可以研讀下論文 。
第二步,傳遞隱層信息並預測:
s t = t a n h ( W [ s t − 1 , y t − 1 ] ) s ~ t = t a n h ( W c [ s t , c t ] ) o t = s o f t m a x ( V s ~ t )
\begin{aligned}
s_t &=tanh(W[s_{t-1},y_{t-1}])\\
{\color{Green} {\widetilde{s}_t}} &=tanh(W_c[{\color{Green} {s_t}},{\color{Red} {c_t}}]) \\
o_t &=softmax(V{\color{Green} {\widetilde{s}_t}}) \\
\end{aligned}
s t s t o t = t a n h ( W [ s t − 1 , y t − 1 ] ) = t a n h ( W c [ s t , c t ] ) = s o f t m a x ( V s t )
先計算出初始的隱層狀態 s t s_t s t ,再計算注意力層的隱層狀態 s ~ t {\color{Green} {\widetilde{s}_t}} s t ,最後送入softmax層輸出預測分佈。
總結
Bahdanau Attention與Luong Attention兩種注意力機制大體結構一致,區別在於計算影響程度的對齊函數。在計算t t t 時刻的影響程度時,前者使用 h i h_i h i 和 s t − 1 s_{t-1} s t − 1 來計算,後者使用 h i h_i h i 和 s t s_{t} s t 來計算。從邏輯來看貌似後者更合邏輯,但兩種機制現在都有在用,TensorFlow中兩者都有對應的函數 ,效果應該沒有很大差別。
References:
[1] Bahdanau et.al (2014) Neural Machine Translation by Jointly Learning to Align and Translate
[2] Luong et.al (2015) Effective Approaches to Attention-based Neural Machine Translation