Peter Shaw, Jakob Uszkoreit, and Ashish Vaswani. 2018. Self-Attention with Relative Position Representations. Proceedings of the 2018 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, March. arXiv: 1803.02155.
非遞歸的模型(attention,CNN等)并沒有考慮輸入序列中元素的順序,因此在很多任務(wù)中可能需要顯式地編碼位置信息。這篇文章將元素與元素之間的相對位置表示引入了self-attention機(jī)制。在兩個機(jī)器翻譯的任務(wù)中,引入相對位置表示的self-attention比絕對位置編碼的self-attention有明顯的提升。
背景知識
-
Attention Mechanism
這篇文章把普通的self-attention的輸出表示為:
注:
的計(jì)算方式采用的是Scaled Dot-Product,詳見Attention Mechanism
模型
Relation-aware Self-Attention
在普通self-attention的基礎(chǔ)上,這篇文章引入了兩個與相對位置相關(guān)的向量:,之所以采用
維向量的表示形式,主要是為了套用原來self-attention的計(jì)算公式。
也就是說,如果attention的目標(biāo)詞是的話,那么在計(jì)算
對
的注意力特征的時候,需要額外考慮
對
的兩個與位置相關(guān)的向量。
引入這兩個向量之后,上述self-attention的計(jì)算可以修改為:
Relative Position Representations
Relative Position Representations的目標(biāo)是給出的計(jì)算方式。作者假設(shè)如果序列中兩個元素的距離超過
,則這兩元素之間的位置信息就沒有意義了。同時,
應(yīng)該只跟相對位置有關(guān),而與
沒有關(guān)系。作者直接將
定義為了可訓(xùn)練的向量,本質(zhì)上是訓(xùn)練
和
: