Self-Attention with Relative Position Representations

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有明顯的提升。

背景知識

注:e_{ij}的計(jì)算方式采用的是Scaled Dot-Product,詳見Attention Mechanism

模型

Relation-aware Self-Attention

在普通self-attention的基礎(chǔ)上,這篇文章引入了兩個與相對位置相關(guān)的向量:a _ { i j } ^ { V } , a _ { i j } ^ { K } \in \mathbb { R } ^ { d _ { a } },之所以采用d_a維向量的表示形式,主要是為了套用原來self-attention的計(jì)算公式。
也就是說,如果attention的目標(biāo)詞是x_i的話,那么在計(jì)算x_jx_i的注意力特征的時候,需要額外考慮x_jx_i的兩個與位置相關(guān)的向量。
引入這兩個向量之后,上述self-attention的計(jì)算可以修改為:
\begin{aligned} z _ { i } &= \sum _ { j = 1 } ^ { n } \alpha _ { i j } \left( x _ { j } W ^ { V } + a _ { i j } ^ { V } \right) \\ \alpha _ { i j } &= \frac { \exp e _ { i j } } { \sum _ { k = 1 } ^ { n } \exp e _ { i k } } \\ e _ { i j } &= \frac { x _ { i } W ^ { Q } \left( x _ { j } W ^ { K } + a _ { i j } ^ { K } \right) ^ { T } } { \sqrt { d _ { z } } }\\ \end{aligned}

Relative Position Representations

Relative Position Representations的目標(biāo)是給出a _ { i j } ^ { V } , a _ { i j } ^ { K }的計(jì)算方式。作者假設(shè)如果序列中兩個元素的距離超過k,則這兩元素之間的位置信息就沒有意義了。同時,a _ { i j } ^ { V } , a _ { i j } ^ { K }應(yīng)該只跟相對位置有關(guān),而與x_i, x_j沒有關(guān)系。作者直接將a _ { i j } ^ { V } , a _ { i j } ^ { K }定義為了可訓(xùn)練的向量,本質(zhì)上是訓(xùn)練w ^ { K } = \left( w _ { - k } ^ { K } , \ldots , w _ { k } ^ { K } \right)w ^ { V } = \left( w _ { - k } ^ { V } , \ldots , w _ { k } ^ { V } \right)
\begin{aligned} a _ { i j } ^ { K } & = w _ { \operatorname { clip }( j - i , k ) } ^ { K } \\ a _ { i j } ^ { V } & = w _ { \operatorname { clip } ( j - i , k ) } ^ { V } \\ \operatorname { clip } ( x , k ) & = \max ( - k , \min ( k , x ) ) \end{aligned}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容