On Position Embeddings in BERT

Benyou Wang, Lifeng Shang, Christina Lioma, Xin Jiang, Hao Yang, Qun Liu, Jakob Grue Simonsen

Introduction

Transformers are inherently "bag-of-words" models; they possess no built-in sense of sequence or word order. To resolve this, researchers inject Position Embeddings (PEs) into the input to give the model a spatial understanding of the text. While many types of PEs exist—ranging from the fixed sine waves of the original Transformer to the fully learnable vectors in BERT—their development has historically relied on trial-and-error.

This paper provides the first formal framework to systematically analyze Position Embeddings in Transformer models. By defining key mathematical properties, the authors reveal why different architectures excel at different tasks. Ultimately, they demonstrate that while classification tasks favor learnable absolute positions, span-prediction tasks perform significantly better with translation-invariant relative positions.

Formalizing Position Embeddings

To move beyond empirical guesswork, the authors propose three formal properties (desiderata) to characterize and evaluate PEs:

  1. Monotonicity: Closer words should generally exhibit higher positional similarity than words located further apart.
  2. Translation Invariance: The similarity between two words should depend strictly on their relative distance, not their absolute position within the sentence.
  3. Symmetry: The positional similarity between position $i$ and $j$ should be identical to the similarity between $j$ and $i$.

To measure how well models adhere to these properties, the researchers introduce Identical Word Probing. By feeding a model a sequence composed entirely of a single repeated word, they effectively neutralize the influence of semantic meaning. This isolates the position embeddings, allowing the authors to observe exactly how positional biases affect the attention mechanism.

Building on this framework, the authors propose two new PE variants: learnable sinusoidal APE (Absolute Position Embeddings) and learnable sinusoidal RPE (Relative Position Embeddings). Unlike standard sinusoidal PEs with fixed frequencies, these variants allow the model to learn the optimal "wavelengths" for capturing positional relationships.

Key Insights

By applying their framework to BERT models pre-trained from scratch on BooksCorpus and English Wikipedia, the authors uncovered several counterintuitive insights about how Transformers process position:

  • The [CLS] Paradox: Fully learnable APEs perform best on classification tasks (like those in GLUE). This occurs because classification relies heavily on the [CLS] token, which is always anchored at position zero. Learnable APEs can memorize this specific position as a special case, whereas translation-invariant models attempt to treat every position equally.
  • Symmetry is a Bug, Not a Feature: While dot-products are naturally symmetric, the authors found that violating symmetry is actually beneficial. Models equipped with "direction awareness"—the ability to distinguish between "the word before me" and "the word after me"—perform better on downstream tasks.
  • The 50-Token Limit: Fixed sinusoidal PEs only maintain monotonicity for approximately 50 tokens. Beyond this window, the model becomes effectively "blind" to distance. Learnable frequencies allow the model to expand this window, explaining why the proposed learnable sinusoidal PEs outperform their fixed counterparts.
  • Task-Specific Needs: Span prediction (e.g., SQuAD) is a "local" task that benefits immensely from translation invariance and monotonicity. Conversely, classification is a "global" task where absolute anchors (like the [CLS] token) matter more.

Performance and Generalization

The theoretical findings align closely with empirical results across major benchmarks:

  • SQuAD Dominance: RPEs significantly outperformed standard BERT-style learnable APEs on span prediction. On SQuAD V1.1, RPE achieved a 90.50 F1 score compared to 89.44 F1 for APE.
  • GLUE Stability: The standard BERT-style learnable APE remained the strongest performer for classification, achieving a mean GLUE score of 82.2. However, the authors' novel learnable sinusoidal RPE was highly competitive, matching the 82.2 score.
  • Sequence Generalization: Models utilizing translation-invariant PEs (like sinusoidal or RPE) generalized much better to sequences longer than those seen during training (e.g., 512 tokens). Fully learnable APEs struggled in these scenarios because they lacked trained embeddings for higher position indices.

Limitations and Future Directions

The authors note that their Identical Word Probing technique primarily analyzes the first layer of the Transformer. While this offers a clear view of the "raw" positional bias, it does not account for how positional information is transformed, diluted, or amplified in deeper layers of the network.

Future work could explore how these mathematical properties evolve throughout the network's depth. Additionally, researchers could investigate whether "direction awareness" can be elegantly integrated into absolute position schemes without sacrificing their ability to leverage special tokens. Finally, testing these properties on larger-scale models (such as GPT-3 or RoBERTa) will be crucial to confirm if these positional dynamics hold true as parameter counts scale exponentially.

Summary updated

BibTeX
@inproceedings{wang2021position,
  author = {Benyou Wang and Lifeng Shang and Christina Lioma and Xin Jiang and Hao Yang and Qun Liu and Jakob Grue Simonsen},
  title = {{On Position Embeddings in BERT}},
  booktitle = {International Conference on Learning Representations},
  year = {2021},
}
Figure