筆記:讀Attention Is All You Need
筆記:Attention Is All You Need
作者:Ashish Vaswani et al.,NIPS 2017.
目錄
- Motivation
- Model
- Attention
1 Motivation
本文使用self-attention代替RNN/CNN來實現encoder和decoder,原因有以下兩點:
- 使用注意力機制相比RNN並行度高
- 使用注意力機制相比RNN能夠更好的抓取長距離依賴
2 Model
本文模型架構總體上來看仍然是seq2seq架構,具體為encoder->decoder,如下圖Figure1所示為模型整理架構。
2.1 Encoder
使用多層堆疊而成如圖Figure1文中N=6,每層包括兩個子層,分別為Multi-Head Attention層和FC全連線層。對比RNN建模的encoder(Bahdanau,D et al.,ICLR 2015.),align model即attention 得分或相關性矩陣計算使用\(s_{i-1}和h_j\)
最後encoder(內部堆疊六層同樣的結構)的輸出作為decoder輸入的一部分即K,V。
2.2 Decoder
相比encoder有幾點不一樣,首先每個堆疊的層中多了一個子層,encoder-decoder attention層,其次輸入為output embedding即之前的decoder輸出,至於第一次的輸入以及其實輸入就是加了mask的監督資料的y,詳見參考。
經過其子層self-attention的輸出作為encoder-decoder attention的輸入即Q,相似度計算也是dot product。
2.4 小結
簡單記錄幾點,具體見參考的幾篇文章以及視訊,講的很細緻清晰易懂,我就不在這裡詳細記筆記了,記了也是複述大佬們的,至此程式碼層面都沒怎麼下功夫,有些點還是看看程式碼後理解的應該會更好一些。
3 Attention
詳細閱讀參考,自己想明白之後,再看下圖Figure 2很清晰。
參考
[1] Ashish Vaswani,Noam Shazeer,Niki Parmar,Jakob Uszkoreit,Llion Jones,Aidan N. Gomez,Łukasz Kaiser.Attention Is All You Need.NIPS 2017.
[2] Dzmitry Bahdanau,KyungHyun Cho,Yoshua Bengio.Neural Machine Translation by Jointly Learning to Align and Translate.ICLR 2015.
[3] 臺大李巨集毅21年機器學習課程 self-attention和transformer.https://www.bilibili.com/video/BV1Xp4y1b7ih?spm_id_from=333.1007.top_right_bar_window_custom_collection.content.click.
[4] 文哥的學習日記.一步步解析Attention is All You Need!.簡書 2018.https://www.jianshu.com/p/b1030350aadb.
[5] 後青春期的工程師.《attention is all you need》解讀.知乎 2019.https://zhuanlan.zhihu.com/p/34781297.
[6] soccer.Attention注意力機制與self-attention自注意力機制.知乎 2020.https://zhuanlan.zhihu.com/p/265108616.