Looped Transformers are Better at Learning Learning Algorithms

Liu Yang, Kangwook Lee, Robert D Nowak, Dimitris Papailiopoulos

Introduction

Large Language Models (LLMs) have demonstrated a remarkable ability to perform in-context learning (ICL). Recent work has even shown that transformers can be trained from scratch to solve continuous data-fitting problems, like linear regression, purely in-context.

However, there is a structural mismatch: traditional machine learning solves these problems using iterative algorithms (like gradient descent), whereas standard transformers execute a fixed, non-recursive forward pass. Because they lack an inherent iterative structure, standard transformers must approximate these algorithms by spreading the computation across their layers.

This paper asks: Can we build a transformer that explicitly uses an iterative "looping" structure to emulate these learning algorithms more naturally, efficiently, and with fewer parameters? The authors introduce a "looped transformer" architecture with a novel training methodology that repeatedly applies a single transformer layer to emulate iterative optimization. The looped model matches or beats standard 12-layer transformers on various regression and classification tasks while using less than 10% of the parameters.

Methodology: The Looped Transformer

To address the structural mismatch between fixed-depth transformers and iterative algorithms, the authors propose replacing a standard multi-layer decoder transformer with a looped transformer—a single-layer transformer whose output is repeatedly fed back into itself.

Looped Transformer Architecture

To make this architecture work effectively, they introduce a specific training methodology:

  • Input Injection: Instead of just passing the output of loop $t$ into loop $t+1$ (standard weight-tying), they inject the original input prompt $P$ at every step: $Y_{t+1} = M(Y_t + P)$. This is crucial; without it, the model "forgets" the input and its predictions diverge into noise as iterations increase.
  • Truncated Loss Window: To save compute and stabilize training, the loss isn't calculated over every single loop. Instead, it is computed only over a truncated window of the final $T$ loop iterations, up to a maximum of $b$ loops.
  • Loop Scheduling: During training, the maximum number of loop iterations ($b$) is gradually increased. This acts as a curriculum, helping the model learn to stabilize its representations over longer horizons.

Datasets and Evaluation

The paper evaluates the models on both synthetic and real-world datasets. Following existing benchmarks for ICL, they generate on-the-fly datasets for Linear Regression, Sparse Linear Regression, Decision Trees (depth 4), and 2-layer ReLU Neural Networks. To test practical applicability, they also evaluate the models on 10 real-world binary classification datasets with numerical features from OpenML (e.g., abalone, bank8FM).

Key Insights and Results

The looped transformer yields several fascinating insights into how these models learn and process information:

  • Convergence to a Fixed-Point: Thanks to input injection and the truncated training strategy, the looped transformer learns a stable "fixed-point" algorithm. During inference, the loop can be run for many more iterations than it saw during training, and the performance remains stable rather than diverging.
  • Iterative Refinement vs. Layer Handoff: Probing the internal representations reveals a difference in how the models think. Standard transformers calculate the optimal solution in their middle layers and then shift focus to other statistics in later layers. In contrast, looped transformers continuously and steadily refine their guess toward the optimal solution with every single iteration.
  • Inductive Bias Toward Simplicity: The looped architecture exhibits a strong inductive bias toward simpler, sparser solutions. When tackling sparse linear regression, this bias allows the looped model to outperform standard transformers and even traditional Lasso regression in low-data regimes.
Convergence and Performance Comparison

Ultimately, the 1-layer looped transformer (0.79M parameters) matches the performance of a standard 12-layer transformer (9.48M parameters) across linear regression, decision trees, and 2-layer NNs. This represents a >90% reduction in parameters. Furthermore, it requires seeing fewer distinct training prompts to master a function class and achieves comparable or slightly better test accuracy on the OpenML datasets.

Limitations and Future Work

While the looped transformer emulates iterative algorithms well within its training distribution, it struggles with severe Out-of-Distribution (OOD) prompts (e.g., massively scaled inputs), sometimes underperforming standard transformers in these edge cases. It learns an algorithm tailored to its training data, not a universally generalizing one.

Additionally, the model requires careful tuning of the loop iterations ($b$) and the truncated window size ($T$) to balance memory, compute, and stability. Future work may explore adaptive looping strategies where the model dynamically decides when to stop looping based on the token or task difficulty, and further investigate the theoretical expressiveness of looped decoder architectures.

Summary updated

BibTeX
@inproceedings{yang2024looped,
  author = {Liu Yang and Kangwook Lee and Robert D Nowak and Dimitris Papailiopoulos},
  title = {{Looped Transformers are Better at Learning Learning Algorithms}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=HHbRxoDTxE},
}
Figure