Self-Attention with Relative Position Representations

Peter Shaw, Jakob Uszkoreit, Ashish Vaswani

Abstract

Relying entirely on an attention mechanism, the Transformer introduced by Vaswani et al. (2017) achieves state-of-the-art results for machine translation. In contrast to recurrent and convolutional neural networks, it does not explicitly model relative or absolute position information in its structure. Instead, it requires adding representations of absolute positions to its inputs. In this work we present an alternative approach, extending the self-attention mechanism to efficiently consider representations of the relative positions, or distances between sequence elements. On the WMT 2014 English-to-German and English-to-French translation tasks, this approach yields improvements of 1.3 BLEU and 0.3 BLEU over absolute position representations, respectively. Notably, we observe that combining relative and absolute position representations yields no further improvement in translation quality. We describe an efficient implementation of our method and cast it as an instance of relation-aware self-attention mechanisms that can generalize to arbitrary graph-labeled inputs.

Introduction

The Transformer architecture revolutionized machine translation by relying entirely on self-attention mechanisms. However, unlike recurrent or convolutional networks, the self-attention mechanism is inherently order-invariant. To account for the sequential nature of language, the original Transformer adds absolute position encodings (sinusoidal signals) to the input embeddings. While effective, absolute encodings treat a sentence like a "bag of words" with indices, potentially failing to capture the relative structural dependencies—such as the distance between a pronoun and its referent—that characterize human language. In this work, we propose an extension to self-attention that explicitly models the relative positions or distances between sequence elements.

Relation-aware Self-Attention

We introduce a "relation-aware" self-attention mechanism. Instead of treating the input as a simple sequence of vectors, we consider it a graph where the relationship between any two elements $i$ and $j$ is represented by a learned vector. We modify the standard attention equations to incorporate these relative representations into two key components: the compatibility score (Keys) and the weighted sum (Values).

The proposed method models relative positions as learned representations $a^K$ and $a^V$ between elements.

Specifically, for a sequence of $n$ elements, we learn relative position representations $a_{ij}^K$ and $a_{ij}^V$. These are integrated into the attention mechanism such that the model considers the distance between words when determining how much attention to pay to a specific neighbor and how to aggregate its information. To ensure the model generalizes well and remains computationally efficient, we employ a clipping distance $k$. We hypothesize that precise relative position information becomes less critical beyond a certain threshold; thus, distances greater than $k$ are mapped to a single representation (e.g., "16+ spaces away").

Experimental Results

We evaluated our approach on the WMT 2014 English-to-German (EN-DE) and English-to-French (EN-FR) translation tasks. Our results demonstrate that relative position representations consistently outperform absolute ones across different model scales.

Relative position representations yield significant BLEU score improvements over absolute baselines, particularly for larger models.

As shown in the table above, the "Transformer (big)" model using relative representations achieved 29.2 BLEU on EN-DE, a 1.3 point improvement over the absolute position baseline. On the larger EN-FR dataset, we observed a 0.3 BLEU improvement. Notably, we found that combining both relative and absolute representations did not yield further gains, suggesting that relative information is a sufficient and superior replacement for absolute encodings in these tasks.

Analysis and Insights

Our investigation revealed several key properties of relative position representations:

  • The Horizon of Context: The clipping distance $k$ is remarkably robust. While we utilized $k=16$ for our primary results, experiments showed that even a very small $k=2$ (knowing if a word is immediately adjacent or slightly further) captures the majority of performance gains. This suggests that the model primarily benefits from local structural context.
  • Key vs. Value Contributions: Ablation studies indicated that incorporating relative information into the Keys (the compatibility score) is the most vital component. Adding it to the Values provided only marginal improvements for translation, though it may be more significant for other sequence-to-sequence tasks where output ordering is more sensitive.
  • Efficiency: Despite the $O(n^2)$ nature of pairwise relationships, our optimized implementation—which shares representations across attention heads and uses tensor reshaping—resulted in only a modest 7% decrease in training speed, making it a practical addition to the Transformer architecture.

Conclusion

By extending self-attention to consider relative distances, we provide the Transformer with a more linguistically inductive bias. This approach not only improves translation quality but also offers a framework for generalizing Transformers to arbitrary graph-labeled inputs. Future work will explore the application of this relation-aware mechanism to non-linear data structures, effectively turning the Transformer into a more powerful graph neural network.

Summary updated

BibTeX
@inproceedings{shaw2018self,
  author = {Peter Shaw and Jakob Uszkoreit and Ashish Vaswani},
  title = {{Self-Attention with Relative Position Representations}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2018},
  url = {https://openreview.net/forum?id=S1b5NX-dbr},
}
Figure