# Effective Approaches to Attention-based Neural Machine Translation
###### tags: `paper notes` `deep learning`
[Paper Link](https://arxiv.org/pdf/1508.04025.pdf)
---
## Intro
* 發表這篇的同年NMT已經達到神經翻譯領域的SOTA, 但這篇又破了紀錄
* SOTA的模型會先持續吃source words直到EOS Token,吃到EOS之後就開始一次infer一個word,這篇也一樣

* **"Attention"機制**因為可以讓模型在兩個不同的情況下同時學習alignment因此大紅
* **Ex:** between **image objects and agent actions in the dynamic control problem** (Mnih et al., 2014)
* between **speech frames and text in the speech recognition task**
* between **visual features of a picture and its text description in the image caption generation task** (Xu et al., 2015).
* 這篇的新穎想法: **Global Attention & Local Attention**
* Global attention對所有source word做attention
* Local attention只對soruce word的subset做attention
* Global可看成是[Bahdanau et al.2015]的簡化版
* Local可看成是hard attention和soft attention的混合(Xu et al., 2015), 這樣做可以把計算量壓到比global attention和soft attention model還低, 同時因為soft attention可以微分會讓她更好訓練
* task是英文轉德文, 在WMT'14,WMT'15上獲得SOTA成績
## Neural Machine Translation
* 每篇神經翻譯論文都有的段落, 總之就是介紹神經翻譯是怎麼做的
* Encoder計算每個source sentece的representation
* Decoder一次吃一個Encoder算出來的Representation之後產生target word和條件機率

* 模型通常都會用RNN, 但大家模型架構不同
* [Kalchbrenner and Blunsom,2013]用CNN
* [Sutskever et al. (2014)] and Luong et al. (2015) 用多層LSTM
* [Cho et al. (2014), Bahdanau et al.] (2015)和[Jean et al. (2015)]用GRU


* $f$可以是Vanilla RNN, LSTM, GRU等等的RNN模型
* $g()$是transformation function(Alignment model in [Bahdanau et al., 2015](https://hackmd.io/RCmuRmqfTpKp0MVh4kEa9Q))
* $h_j$是hidden unit
* $s$是source representation, 只會被用來初始化decoder hidden state
* 這篇論文的$f$是用(Sutskever et al., 2014和Luong et al., 2015)的多層LSTM
多層LSTM的Traning object:
---

---
## Attention-based Models
* 剛剛說過有兩種attention: Global 跟 Local
* 兩者是根據attention要apply在整個source sentence還是一小部分的source sentence來分類
* 兩者的共同點在於計算context vector $c_t$的時候都把最上層LSTM的hidden state $h_t$作為輸入
* 這樣做的目的在於讓 $c_t$ 可以幫助decoder predict 現在的target word $y_t$
* 兩種attention的$c_t$來源不太一樣但是步驟順序是一樣的
有了$c_t$和$h_t$之後就可以算出attention vector $\bar{h}_t$

把attention vector乘上Weight在過softmax就是predictive distribution

剛剛說過$h_t$是LSTM的hidden state, 那麼$c_t$是怎麼算出來的呢?
### Global Attention
* Global的想法是在產生$c_t$的時候考慮所有的hidden state of Encoder
* $a_t$是variable-length alignment vector (size和source side的time step長度一樣)
* $a_t$由$h_t$和source sentence的hidden state $\overline{h_s}$算出

* **score** is referred as a content-based function for which we consider three different alternative:

---
* 除此之外他們有提到在做這個研究的時候,早期階段有曾經用過一個**location-based function**作為score func
* 它的alignment score $a_t$是只從$h_t$算出來, 沒有考慮source sentece那邊的$\overline{h_s}$

模型架構圖:

計算流程: $$h_t \rightarrow a_t \rightarrow c_t \rightarrow \widetilde{h_t}$$
---
Comparsion with [Bahdanau et al., 2015]
---
1. Encoder和Decoder用的hidden state都是最上層LSTM的hidden state,[Bahdanau]用的是雙向GRU cancat後的hidden state還有decoder中的target hidden state
2. 這篇的計算流程是$$h_t \rightarrow a_t \rightarrow c_t \rightarrow \widetilde{h_t}$$
[Bahdanau]的計算流程是$$h_{t-1} \rightarrow a_t \rightarrow c_t \rightarrow h_t$$
3. [Bahdanau]只有實驗一種score function,這篇用了其他的而且表現更好
## Local Attention
* global attention的drawback是在產生每一個target word的時候都得考慮到每一個source side的word, 這不僅太花計算資源而且在遇到long sentence的時候無法實作
* Local就是為了改善Global的deficiency而提出,它在產生每一個target word的時候只會focus on small subset of source sentence position
* Local啟發於soft-attention和hard-attention model by [Xu et al. (2015)]
* 在Xu那篇中的soft attention提到一個global attention approach in which weights are placed “softly” over all patches in the source image
* 至於hard attention則是selects one patch of the image to attend to at a time
* **雖然計算量變少但hard attention不可微因此比較難train**
* 不同於Xu, 他們的local attention選擇性的專注於small window of context而且是可微的
* 這樣不僅有soft attention的表現又有hard attention的少計算量,但又比hard attention好訓練(因為可微)
Concrete concept of local attention
---
* 模型首先產生一個alignment position $p_t$
* 接著在以 $p_t$為中心的window $[p_t-D,p_t+D]$上做Weighted sum產生$c_t$
* $D$是empirically selected
* 不同於global, local的alignment vecotr $a_t$長度是fixed-dimensional的 ($2D+1$的dimension)
模型有兩個變型:
1. **Monotonic alignment (local-m)**
* local-m直接把$p_t$設為$t$ ($p_t=t$), 假設所有source和target都是單調對齊(Monotonic alignment)的
2. **Predictive alignment (local-p)**
* local-p則是根據下面這個公式算出$p_t$

* $W_P$和$v_p$是要在預測位置的時候學習的模型參數
* $S$是source sentece的長度
---
* 經過sigmoid之後,為了更好的讓$p_t$附近的word可以對齊, 又以$p_t$為中心加上了Gaussian distribution
* local的alignment function和global的$align(h_t,\overline{h_s})$是一樣的,

只是local又加上$\sigma=D/2$的Gaussian

模型架構圖:

Comparison to (Gregor et al., 2015)
---
* (Gregor et al., 2015)也**選擇性**的attention機制用來產生圖片,和這篇類似
* 但他們在選擇的時候是可以選擇不同位置和zoom的image, 而這篇的"zoom"in every target position是一樣, 因此同時可以降低計算成本又不會降低表現
## Input-feeding Approach
* 在這篇之中,global或者是local attention是**independent decision**,而這不太好, 如果能將兩個attention資訊互相交換會更好

* 因此才提出Input-feeding將$\widetilde{h_t}$和下一個time step的input concat起來
這樣做的目的有兩個:
1. 希望模型可以完全知道前面的alignment choices
2. 希望這個DNN不僅在horizontally擴張,在vertically也擴張
Comparsion to [Bahdanau et al. (2015)] in $c_t$
---
* 在Bahdanau et al. (2015)之中的context vector $c_t$和這篇的相似, 都可以做到"Coverage effect"
* 但卻對這些Coverage effect沒有做相關的研究看它是否真的有效
* 這篇的方法會更加general, 並不會只限於NMT上
Comparsion to [Xu et al. (2015)] in Coverage effect
* Xu用了雙重attention來確保產生圖片的時候是對所有image做attention, 而這也可以用來capture the coverage effect in NMT
* 但因為可以提供**模型彈性**, 因此這篇用Input-feeding
## Experiments

---

---

## Analysis

---

## Conclusion
* Two simple and effective apporach:Global & Local
* New SOTA in NMT
* attention-based NMT models > nonattentional ones in many cases