Towards Understanding Transformers in Learning Random Walks

Wei Shi, Yuan Cao

Abstract

While transformers have achieved remarkable success across various domains, their internal learning mechanisms remain theoretically opaque. This paper provides a mechanistic "look under the hood" by analyzing a one-layer transformer model trained on random walks. The authors prove that transformers can achieve optimal prediction accuracy by evolving into a highly interpretable "selector-transitor" mechanism. Specifically, the attention layer learns to act as a token selector for the most recent state, while the value matrix learns the underlying probability transition map.

Introduction

Despite their widespread adoption, transformers are often treated as "black boxes." We lack a comprehensive theoretical understanding of how they learn specific statistical structures or why they occasionally fail on seemingly trivial tasks. To bridge this gap, the authors investigate a one-layer transformer tasked with learning a fundamental statistical model: random walks on a circle.

A random walk on a circle with $K$ nodes, where the walker moves clockwise with probability $p$ and counter-clockwise with $1-p$.

In this controlled environment, a "walker" moves between $K$ nodes, transitioning clockwise with probability $p$ and counter-clockwise with $1-p$. By tracking how gradient descent shapes the model's internal weights during training, the researchers can mathematically describe the emergence of predictive capabilities.

Methodology

The study focuses on a one-layer transformer architecture using softmax self-attention and a value matrix $V$. To facilitate theoretical analysis, the authors use a reparameterization where a single matrix $W$ represents the product of Query and Key matrices. Orthogonal positional embeddings are employed to help the model distinguish the temporal order of steps.

The model is trained using gradient descent starting from zero initialization. The researchers use induction to characterize the training dynamics, proving that for any $p \in (0, 1)$, the model converges to the optimal predictor.

The Selector-Transitor Mechanism

The most significant finding is the emergence of a precise two-step algorithm, termed the "Selector-Transitor" mechanism.

The Selector-Transitor mechanism: the attention layer (token selector) focuses on the most recent token, while the value matrix $V$ mirrors the ground-truth transition matrix.
  1. Token Selector: The attention mechanism learns to ignore the distant past and concentrates nearly all its weight on the very last token in the sequence (the direct parent of the next state).
  2. Probability Transitor: Simultaneously, the value matrix $V$ evolves to mirror the ground-truth transition matrix of the random walk.

This combination allows the transformer to effectively "read" the current state and "apply" the learned transition probabilities to predict the next node.

Key Results and the Paradox of Determinism

The model achieves the theoretical maximum accuracy of $\max\{p, 1-p\}$ for non-deterministic walks ($0 < p < 1$), with a convergence rate of $O(T^{-1/2})$.

However, a counter-intuitive finding is that the model fails spectacularly on "easier" deterministic walks where $p=0$ or $p=1$. In these cases, the model's accuracy remains at $1/K$ (random guessing). This failure is attributed to unbreakable symmetry: with zero initialization, the gradient for attention weights remains zero because the average of all previous tokens in a deterministic path is uninformative. While small random initializations can break this symmetry, the learning process remains significantly slower than for truly random walks.

Conclusion and Future Work

This research provides a rigorous theoretical foundation for understanding how simple transformers learn Markovian structures. While the one-layer model is a simplification, it reveals fundamental insights into the "Selector-Transitor" logic.

Future research directions include:

  • Initialization Strategies: Extending the theory to non-zero starting points to better align with practical training regimes.
  • Architectural Complexity: Investigating how multi-layer hierarchies and multi-head attention interact to solve more complex tasks or overcome the symmetry issues found in deterministic paths.
  • General Graphs: Moving beyond circular topologies to general Markov chains or Bayesian networks to test the limits of the selector-transitor mechanism.

Summary updated

BibTeX
@inproceedings{shi2025towards,
  author = {Wei Shi and Yuan Cao},
  title = {{Towards Understanding Transformers in Learning Random Walks}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=ZacSviw4em},
}
Figure