Paper commentary
Self-Attention with Relative Position Representations
Relative position representations let self-attention use distances between sequence elements
- Problem The Transformer has no recurrence or convolution, so it cannot directly see how far apart two elements are. The usual fix adds absolute position codes to the inputs.
- Method Relative position representations add learned vectors for clipped distances between element pairs to both the value sum and the attention scores, instead of adding absolute codes to the inputs.
- Results The big model scored 29.2 BLEU on English-to-German against 27.9 for absolute positions, a gain of 1.3 BLEU on WMT 2014.
Why the Transformer needs position information
Recurrent networks carry order in their state, since each hidden state is computed from the previous one, so position is part of the computation itself. Convolutional networks already see relative offsets inside each kernel, yet earlier work reports that they still gain from position encodings. The Transformer has neither recurrence nor convolution, so without extra input its output would not depend on the order of the elements.
Its standard remedy adds sinusoidal encodings to the inputs before the first layer, and the authors hypothesized that these would let the model attend by relative position and generalize to longer sequences.
The question this study addresses is whether self-attention can receive relative offsets directly, instead of relying only on absolute codes added to the inputs. The proposed change reaches into the attention computation itself, which is why it can also be read as a general way to attach relations between any two input elements.
Adding pair vectors to self-attention
In standard self-attention, each output is a weighted sum of linearly transformed value vectors, and the weights come from a softmax over compatibility scores between element pairs. The compatibility score is a scaled dot product of a query and a key, \( e_{ij} = \frac{(x_i W^Q)(x_j W^K)^T}{\sqrt{d_z}} \), and each output is \( z_i = \sum_{j=1}^{n} \alpha_{ij}(x_j W^V) \).
The change treats the input as a labeled, directed, fully connected graph, so each pair of elements \(x_i\) and \(x_j\) has two vectors \(a^V_{ij}\) and \(a^K_{ij}\) in \(\mathbb{R}^{d_a}\), with \(d_a = d_z\). The value vector is replaced by \( x_j W^V + a^V_{ij} \) in the weighted sum, which lets the pair's edge information flow into the output.
The key side of the compatibility score is changed to \( x_j W^K + a^K_{ij} \), so the score becomes \( e_{ij} = \frac{x_i W^Q (x_j W^K + a^K_{ij})^T}{\sqrt{d_z}} \). Two separate vectors are used because each one fits its place in the formula without an extra linear transformation, and the same vectors can be shared across attention heads.
Simple addition is chosen on purpose, because it keeps the computation efficient, as explained in the implementation section.
The edge vectors are not fixed. They are looked up from learned vectors \(w^K\) and \(w^V\) by the clipped offset, so \( a^K_{ij} = w^K_{\mathrm{clip}(j-i,k)} \) and \( a^V_{ij} = w^V_{\mathrm{clip}(j-i,k)} \), with \( \mathrm{clip}(x,k) = \max(-k, \min(k,x)) \). Offsets from \(-k\) to \(k\) each get their own learned vector, so the scheme has \(2k+1\) edge labels, and every offset beyond the clip shares the vector of the nearest boundary.
The base configuration used edge representations unique per layer and head, whereas the big configuration used them unique per layer.
The authors hypothesize that clipping keeps precise position information only for nearby elements and lets the model generalize to sequence lengths not seen in training. Relative representations are invariant to total sequence length, which the authors contrast with absolute representations.
Computing relative terms with batched products
Without relative terms, the Transformer computes all compatibility scores of a head and sequence in one matrix multiplication, because every position shares the same representation across all dot products. With relative terms, each pair of positions has its own representation, so one matrix multiplication cannot produce all scores, and broadcasting the representations would be costly.
The fix is to split the compatibility term into two parts, \( e_{ij} = \frac{x_i W^Q (x_j W^K)^T + x_i W^Q (a^K_{ij})^T}{\sqrt{d_z}} \). The first part is the ordinary dot product. The second part is computed with tensor reshaping as \(n\) parallel multiplications of \(bh \times d_z\) and \(d_z \times n\) matrices, and the two parts are then added after reshaping.
The same approach is used for the value term of the weighted sum.
Translation results against absolute positions
The comparison uses the same library and configuration for both sides, with a baseline Transformer that has sinusoidal position encodings, so that only the position representation changes. Results are reported on newstest2014 in BLEU for English-to-German and English-to-French, for the base and big configurations.
With the base configuration, relative position representations scored 26.8 on English-to-German, against 26.5 for absolute position representations, and 38.7 on English-to-French, against 38.2.
With the big configuration, relative position representations scored 29.2 on English-to-German against 27.9 for absolute positions, and 41.5 on English-to-French against 41.2. The corresponding improvements over the baseline are 1.3 BLEU on English-to-German and 0.3 BLEU on English-to-French for the big configuration. The big configuration used a clipping distance of 8, while the base used 16, and its dropout was 0.3 for English-to-German but 0.1 for English-to-French.
Adding sinusoidal position encodings on top of relative position representations gave no observed benefit in these experiments. The abstract's statement that combining both kinds of position representation gives no further improvement rests on this same comparison.
Clipping distance and the two edge terms
These sweeps use the base configuration without absolute position representations, trained for English-to-German and scored in BLEU on the newstest2013 development set. Across the clipping distances, k of 0 gave 12.5, k of 1 gave 25.5, and k from 2 to 256 gave between 25.8 and 25.9. At k equal to 0, every offset maps to the same edge vector, so the model has no relative position information, which matches the 12.5 obtained when both edge terms are removed.
Beyond k of 2, the scores are close, and the authors suggest that precise position information can still spread across the stacked encoder layers past the clipping distance. This is the authors' interpretation, and the sweep does not isolate it.
The ablation keeps or removes each edge term in all four combinations, and in each case the score is read against the base model with both terms. With the value-term edge removed and the compatibility-term edge kept, BLEU is 25.8, the same as with both kept. With the compatibility-term edge removed and the value-term edge kept, BLEU drops to 25.3, and with both removed it falls to 12.5.
On this development set, the compatibility term carries the gain, while the value term adds nothing measurable. The authors note that the value-term edges are presumably important for tasks that need edge-type information, and that machine translation may not need them, a point that further work would need to settle for other tasks.
Training-speed cost of the batched computation
In the machine translation experiments, the split computation reduced training steps per second by a modest 7%. The same model and batch sizes still fit on P100 GPUs as in the Transformer baseline. This is a single throughput measurement, so it describes the overhead of this implementation on the reported hardware, not a general cost of relative attention.
Training and decoding conditions
The English-German training data has about 4.5M sentence pairs, and the English-French data about 36M. Tokens are split into a 32,768 word-piece vocabulary, sentence pairs are batched by approximate length, and each batch holds roughly 25,000 source and 25,000 target tokens, with at most 4096 tokens per GPU. Label smoothing with value 0.1 is used in training, and decoding uses beam search with beam size 4 and length penalty 0.6.
The base configuration has 6 encoder and decoder layers, model dimension 512, \( d_z = 64 \), 8 attention heads, 1024 feed-forward dimensions, and dropout 0.1. It was trained for 100,000 steps on 8 K40 GPUs, without checkpoint averaging.
Limitations
The translation evidence covers two WMT 2014 language pairs and the newstest2014 test set. The clipping-distance sweep and the edge-term ablation use only the base configuration on English-to-German with the newstest2013 development set. The speed cost is one measurement on P100 GPUs. The reported experiments do not include sequences longer than those seen in training, so the generalization hypothesis is not tested here.
Extensions to arbitrary directed, labeled graphs and nonlinear compatibility functions are left as future work.
Paper info
- Authors
- Peter Shaw, Jakob Uszkoreit, Ashish Vaswani
- Venue
- arXiv · Preprint
- First published
- 2018-3-6
- Added
- 2026-11-12
- Updated
- 2018-4-12
- Version
- v2
- arXiv
- 1803.02155
- Category
- cs.CL
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Peter Shaw, Jakob Uszkoreit, Ashish Vaswani (2018). Self-Attention with Relative Position Representations. arXiv:1803.02155v2. https://arxiv.org/abs/1803.02155v2
BibTeX
@misc{shaw2018self,
title = {{Self-Attention with Relative Position Representations}},
author = {Peter Shaw and Jakob Uszkoreit and Ashish Vaswani},
year = {2018},
eprint = {1803.02155},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1803.02155v2}
}