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等)并没有考虑输入序列中元素的顺序,因此在很多任务中可能需要显式地编码位置信息。这篇文章将元素与元素之间的相对位置表示引入了self-attention机制。在两个机器翻译的任务中,引入相对位置表示的self-attention比绝对位置编码的self-attention有明显的提升。
背景知识
-
Attention Mechanism
这篇文章把普通的self-attention的输出表示为:
注:
的计算方式采用的是Scaled Dot-Product,详见Attention Mechanism
模型
Relation-aware Self-Attention
在普通self-attention的基础上,这篇文章引入了两个与相对位置相关的向量:,之所以采用
维向量的表示形式,主要是为了套用原来self-attention的计算公式。
也就是说,如果attention的目标词是的话,那么在计算
对
的注意力特征的时候,需要额外考虑
对
的两个与位置相关的向量。
引入这两个向量之后,上述self-attention的计算可以修改为:
Relative Position Representations
Relative Position Representations的目标是给出的计算方式。作者假设如果序列中两个元素的距离超过
,则这两元素之间的位置信息就没有意义了。同时,
应该只跟相对位置有关,而与
没有关系。作者直接将
定义为了可训练的向量,本质上是训练
和
: