Publications
arXiv 2024

Transformers are Expressive, But Are They Expressive Enough for Regression?

Swaroop Nath, Harshad Khadilkar, Pushpak Bhattacharyya

Computer Science and Engineering, IIT Bombay

Transformers approximate functions with piecewise-constant steps, so they struggle to regress smooth functions.

Interactive Demo

Pick a function and change the resolution factor δ, the size of each constant piece. Functions that change quickly need smaller δ to keep the error under ε, and smaller δ means many more layers.

Function
Error d₁(f, f)
Largest δ with error ≤ ε
Layer growth, m · (1/δ)dm
Show values per piece

Illustration of the paper’s argument in one dimension (m = n = d = 1 in the plot, p = 1, ε = 0.1 as in the paper’s example). As in the proof of Theorem 4.1, f takes the value of f at the centre of each piece. δ is limited to 1/N so the pieces tile [0, 1). The layer term is an order of growth, not an exact layer count.

Abstract

Transformers have become pivotal in Natural Language Processing, demonstrating remarkable success in applications like Machine Translation and Summarization. Given their widespread adoption, several works have attempted to analyze the expressivity of Transformers. Expressivity of a neural network is the class of functions it can approximate. A neural network is fully expressive if it can act as a universal function approximator. We attempt to analyze the same for Transformers. Contrary to existing claims, our findings reveal that Transformers struggle to reliably approximate smooth functions, relying on piecewise constant approximations with sizable intervals. The central question emerges as: “Are Transformers truly Universal Function Approximators?” To address this, we conduct a thorough investigation, providing theoretical insights and supporting evidence through experiments. Theoretically, we prove that Transformer Encoders cannot approximate smooth functions. Experimentally, we complement our theory and show that the full Transformer architecture cannot approximate smooth functions. By shedding light on these challenges, we advocate a refined understanding of Transformers’ capabilities. Code Link: https://github.com/swaroop-nath/transformer-expressivity.

The Argument

  1. Yun et al. (2020) show that Transformers approximate a function f by first replacing it with a piecewise-constant f, whose smallest piece has size δ (the resolution factor). Fitting f then takes layers, for input length m and embedding dimension d.
  2. We bound δ by how fast f changes (Theorem 4.1). If f is to stay within ε of f in the dp distance:
  3. Together: a smooth f needs a small δ, and so a number of layers exponential in m. For example, with ε = 0.1, p = 1, d = 1, a covering of about 10 points and a 1-Lipschitz f, δ ≤ 0.2, so layers grow as .

The theory covers the Transformer encoder; the experiments test the full encoder–decoder Transformer.

Results

On synthetic data (4 inputs, 3 outputs; logarithmic, exponential and polynomial terms), Expt-I trains the Transformer to generate the outputs directly (regression), and Expt-II quantizes them into 5 classes (classification). failure-rate is the fraction of test points whose ground truth is not the nearest neighbour of the prediction (1 − accuracy for Expt-II). Each setting is run 5 times; bands and bars show the standard deviation.

Citation

@misc{nath2024transformers,
  title         = {Transformers are Expressive, But Are They Expressive Enough for Regression?},
  author        = {Swaroop Nath and Harshad Khadilkar and Pushpak Bhattacharyya},
  year          = {2024},
  eprint        = {2402.15478},
  archivePrefix = {arXiv},
  primaryClass  = {cs.LG},
  url           = {https://arxiv.org/abs/2402.15478}
}