Transformers Can Do Arithmetic with the Right Embeddings

Sean McLeish, Arpit Bansal, Alex Stein, Neel Jain, John Kirchenbauer +6 more
5/27/2024
cs.LGcs.AI

No official code found. Best implementation: mcleish7/arithmetic (200 stars, MIT, last commit 2024-05-28).

Abstract

The poor performance of transformers on arithmetic tasks seems to stem in large part from their inability to keep track of the exact position of each digit inside of a large span of digits. We mend this problem by adding an embedding to each digit that encodes its position relative to the start of the number. In addition to the boost these embeddings provide on their own, we show that this fix enables architectural modifications such as input injection and recurrent layers to improve performance even further. With positions resolved, we can study the logical extrapolation ability of transformers. Can they solve arithmetic problems that are larger and more complex than those in their training data? We find that training on only 20 digit numbers with a single GPU for one day, we can reach state-of-the-art performance, achieving up to 99% accuracy on 100 digit addition problems. Finally, we show that these gains in numeracy also unlock improvements on other multi-step reasoning tasks including sorting and multiplication.

View on arXivView PDF

Code Implementations(1)

mcleish7/arithmeticCommunity100%
MIT

Code to reproduce "Transformers Can Do Arithmetic with the Right Embeddings", McLeish et al (NeurIPS 2024)

20016Shell, PythonMay 27, 20242 years agoMIT

Similar Papers

Cite this paper

@article{mcleish2024transformers,
  title  = {Transformers Can Do Arithmetic with the Right Embeddings},
  author = {Sean McLeish and Arpit Bansal and Alex Stein and Neel Jain and John Kirchenbauer and Brian R. Bartoldson and Bhavya Kailkhura and Abhinav Bhatele and Jonas Geiping and Avi Schwarzschild and Tom Goldstein},
  year   = {2024},
  eprint = {2405.17399},
  archivePrefix = {arXiv},
  doi    = {10.48550/arXiv.2405.17399},
  url    = {http://arxiv.org/abs/2405.17399v2},
  journal = {NEURIPS 2024 2024}
}

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/2405.17399.svg)](https://codeofpaper.com/paper/2405.17399)

Discussion