Randomized Positional Encodings Boost Length Generalization of Transformers

Anian Ruoss, Grégoire Delétang, Tim Genewein, Jordi Grau-Moya, Róbert Csordás +3 more
5/26/2023
cs.LGcs.AIcs.CL

No official code found. Best implementation: google-deepmind/randomized_positional_encodings (83 stars, Apache-2.0, last commit 2024-03-14).

Abstract

Transformers have impressive generalization capabilities on tasks with a fixed context length. However, they fail to generalize to sequences of arbitrary length, even for seemingly simple tasks such as duplicating a string. Moreover, simply training on longer sequences is inefficient due to the quadratic computation complexity of the global attention mechanism. In this work, we demonstrate that this failure mode is linked to positional encodings being out-of-distribution for longer sequences (even for relative encodings) and introduce a novel family of positional encodings that can overcome this problem. Concretely, our randomized positional encoding scheme simulates the positions of longer sequences and randomly selects an ordered subset to fit the sequence's length. Our large-scale empirical evaluation of 6000 models across 15 algorithmic reasoning tasks shows that our method allows Transformers to generalize to sequences of unseen length (increasing test accuracy by 12.0% on average).

View on arXivView PDF

Code Implementations(1)

Apache-2.0

Randomized Positional Encodings Boost Length Generalization of Transformers

832PythonJun 16, 20232 years agoApache-2.0

Similar Papers

Cite this paper

@article{ruoss2023randomized,
  title  = {Randomized Positional Encodings Boost Length Generalization of Transformers},
  author = {Anian Ruoss and Grégoire Delétang and Tim Genewein and Jordi Grau-Moya and Róbert Csordás and Mehdi Bennani and Shane Legg and Joel Veness},
  year   = {2023},
  eprint = {2305.16843},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2305.16843v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2305.16843.svg)](https://codeofpaper.com/paper/2305.16843)

Discussion