Learning Randomized Algorithms with Transformers

Johannes von Oswald, Seijin Kobayashi, Yassir Akram, Angelika Steger

TL;DR

Researchers have demonstrated for the first time that Transformer models can be trained to implement randomized algorithms—strategies that use randomness to outperform deterministic ones in adversarial or resource-constrained settings. By optimizing against a "worst-case" adversarial objective rather than just average performance, Transformers learn to leverage an input random seed to become significantly more robust and accurate through techniques like majority voting.

The Problem: Deterministic Brittle Models

In classical computer science, randomized algorithms (like Quicksort or Monte Carlo simulations) are often more efficient or robust than their deterministic counterparts. This is especially true in adversarial settings: if an algorithm is deterministic, a clever opponent can predict its behavior and provide an input that makes it fail.

Current deep learning models are typically trained using Empirical Risk Minimization (ERM), which focuses on performing well on average. This often results in models that are "brittle"—they might perform perfectly on 99% of data but fail catastrophically on specific "worst-case" inputs. Furthermore, even if you give a standard Transformer a random seed as input, it usually learns to ignore it, collapsing into a deterministic strategy. The authors set out to see if a Transformer could be forced to "embrace the chaos" and learn to use randomness as a tool.

Methods: Forcing the Model to Use Randomness

The authors introduced two key components to enable learned randomization:

  1. Random Seed Encoding (RSE): They provide the Transformer with a "seed"—a vector of random bits or noise—concatenated to the input tokens. This gives the model the source of randomness it needs.
  2. Adversarial Training Objective ($q$-norm): Instead of standard ERM (which minimizes average loss), they use a $q$-norm relaxation of a min-max objective. By increasing the hyperparameter $q$, the training process starts to ignore easy examples and focuses heavily on the "worst-case" inputs where the model currently fails.

They tested this setup on three conceptual tasks:

  • Associative Recall: Remembering $N$ items when the model only has memory for $M$ ($M < N$).
  • Graph Coloring: Assigning colors to nodes in a cycle such that no two neighbors share a color, using limited local communication.
  • Grid World Exploration: An agent searching for a hidden treasure where an adversary chooses the treasure's location to foil the agent.

Key Insights and Ideas

  • ERM Leads to Deterministic Collapse: If you train a model on standard average loss, it will eventually ignore the random seed. Randomization is "expensive" in terms of average error, so the model chooses a safe, deterministic path unless forced otherwise.
  • The Power of $q$: There is a "phase transition" as the adversarial strength $q$ increases. At low $q$, the model is deterministic. At a certain threshold, the model suddenly starts utilizing the random seed to diversify its strategies.
Phase transition of predictive variance as q increases
  • Majority Voting is a Superpower: Once a Transformer learns a randomized strategy, you can run the same model multiple times with different seeds and take a majority vote of the outputs. This "amplifies" the success probability, often taking a model from mediocre worst-case performance to near-perfection.
  • Beating the "Needle in a Haystack": In the recall task, a deterministic model with limited memory will always miss certain items. A randomized model learns to store a random subset of items; while it might miss any specific item on one run, it will eventually recall it if you re-run the model with different seeds.

Key Results

  • Associative Recall: On worst-case sequences, deterministic models dropped to 0% success as sequence length increased. The randomized Transformer, combined with majority voting (e.g., 30 seeds), maintained near 100% success even when memory was significantly smaller than the number of items.
Associative recall performance and success probability amplification
  • Graph Coloring: The randomized model learned to avoid "collisions" (neighbors picking the same color) by using the seed to break symmetry. Majority voting boosted performance to nearly optimal levels compared to deterministic baselines that were easily "foiled" by specific node orderings.
  • Grid World: When trained with evolutionary strategies, the Transformer learned a randomized exploration policy. Unlike the deterministic baseline, which would get stuck in a single path, the randomized agent covered the grid more uniformly, making it impossible for an adversary to hide the treasure effectively.

Limitations and Future Work

  • Computational Cost: The primary drawback is that using $m$ random seeds to perform majority voting increases inference time and memory usage by a factor of $m$.
  • Optimization Complexity: Training with high $q$-norms is more difficult and less stable than standard ERM.
  • Scaling to LLMs: While demonstrated on conceptual tasks, it remains to be seen how this approach scales to massive Large Language Models (LLMs) or real-world "needle in a haystack" problems in long-context processing.
  • Refined Sampling: The authors suggest that future work could investigate more sophisticated ways to sample seeds during training to improve convergence.

Summary updated

BibTeX
@inproceedings{oswald2025learning,
  author = {Johannes von Oswald and Seijin Kobayashi and Yassir Akram and Angelika Steger},
  title = {{Learning Randomized Algorithms with Transformers}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=UV5p3JZMjC},
}
Figure