NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

How Transformers Learn Structured Data: Insights From Hierarchical Filtering

Jerome Garnier-Brun, Marc Mezard, Emanuele Moscato, Luca Saglietti

Introduction

While Transformers have revolutionized artificial intelligence, their inner workings remain a notorious black box. We know they excel at capturing the hierarchical nature of human language—where letters form words, words form clauses, and clauses form sentences—but we lack a complete understanding of how the attention mechanism implements this hierarchy.

Previous research often relied on complex Context-Free Grammars (CFGs) to study this phenomenon. However, CFGs can be messy and ambiguous. To address this, researchers have introduced a more controlled environment: a tree-based generative model where correlations can be precisely "filtered" or truncated. This novel approach demonstrates that vanilla Transformers do not merely "memorize" data; they spontaneously approximate Belief Propagation (BP), the mathematically optimal algorithm for inference on trees. By using this tunable model, the authors show that Transformers learn to resolve data structure from the bottom up, sequentially mastering short-range correlations before moving to long-range ones.

Methods: The Filtered Hierarchical Model

To understand how Transformers process structured data, the authors propose a Filtered Hierarchical Model. Imagine a binary tree where a "root" symbol at the top determines the symbols of its children, which in turn determine their children, continuing down to the "leaves" (the actual sequence the model observes).

Illustration of the hierarchical tree model with filtering parameter k=1.
  • Hierarchical Filtering ($k$): The researchers introduced a parameter $k$ that allows them to "turn off" correlations above a certain depth. For instance, if $k=0$, the entire tree is correlated. As $k$ increases, only local clusters of leaves remain related, effectively filtering out higher-level hierarchical dependencies.
  • Tasks: The study trained vanilla encoder-only Transformers on two primary tasks:
  • Root Classification: Predicting the root symbol from the observed leaves.
  • Masked Language Modeling (MLM): Predicting a missing leaf symbol based on the context provided by the other leaves.
  • The "Oracle" Comparison: The Transformer’s predictions were rigorously compared against Belief Propagation (BP), which serves as the exact mathematical solution (or "oracle") for this type of probabilistic graphical model.
  • Probing: To peek inside the black box, the team used "linear probes"—small, frozen-weight classifiers—to examine intermediate layers and determine what hierarchical information was being stored at each stage of the network.

Datasets

The study relies on synthetic datasets generated directly from their hierarchical tree model, allowing for precise control over the data's underlying structure.

  • Parameters: Experiments typically utilized a tree depth of $\ell=4$ (resulting in sequences of 16 leaf tokens) and a vocabulary size of $q=4$.
  • Scale: The number of training samples ($P$) was varied from $10^3$ to $10^6$ to observe how sample complexity interacts with the discovery of data structure.
  • Diversity: The researchers tested various "realizations" of the transition matrices (the "grammar" defining the tree) to ensure their findings were robust and not artifacts of a specific data configuration.

Key Insights and Ideas

KL divergence over epochs showing the staircase-like sequential discovery of hierarchical correlations.
  • Sequential Discovery: Transformers learn hierarchical data in a distinct "staircase" fashion. During training, they first resolve the correlations between immediate neighbors (short-range dependencies). Once these are mastered, the model slowly "climbs" the tree to discover and integrate correlations between increasingly distant parts of the sequence (long-range dependencies).
  • Spontaneous Calibration: Surprisingly, the Transformer doesn't just learn to guess the right answer; its output probabilities (logits) almost perfectly match the theoretical probabilities calculated by the exact BP algorithm, even on unseen data.
  • Attention Mirrors Architecture: In the MLM task, the learned attention maps are far from random. They form distinct, localized blocks that perfectly mirror the branching structure of the underlying generative tree.
  • A "Constructive Proof" of BP: The authors provide a theoretical framework demonstrating that a Transformer's architecture is mathematically capable of implementing Belief Propagation. Specifically, the attention layers can perform the necessary "message passing" between nodes, while the MLP blocks handle the non-linear probability updates.

Key Results

  • Optimal Performance: Given sufficient training data, Transformers successfully reach the theoretical maximum accuracy—the "Bayes optimal" limit—defined by the BP oracle.
  • Pre-training Synergy: The paper quantifies exactly why MLM pre-training is so effective. MLM builds the necessary hierarchical "scaffolding" within the attention layers. This pre-existing structure then allows the model to solve downstream tasks, such as root classification, with significantly fewer labeled examples.
  • Layer-Wise Information: Probing experiments revealed that information about the "ancestors" in the tree is reconstructed layer-by-layer. Lower layers primarily contain information about immediate parents, while higher layers successfully aggregate this to predict the root.

Limitations and Future Work

While these findings provide a significant step forward in Transformer interpretability, the study has limitations:

  • Fixed Topology: The current study uses a fixed, static tree structure. In real-world language, the underlying "parsing tree" of a sentence is latent and varies dynamically from one sentence to the next.
  • Small Depth: The experiments were limited to relatively shallow trees ($\ell=4$). It remains to be seen whether these exact learning dynamics hold for much deeper hierarchies, such as those found in long documents or complex codebases.
  • Non-Ambiguous Rules: The transition tensors used were mostly "non-ambiguous" (where one pair of children has only one possible parent). The authors suggest that exploring "ambiguous" grammars—where the same sequence could validly map to multiple different trees—is a crucial next step for understanding how Transformers handle real-world uncertainty and nuance.

Summary updated

BibTeX
@inproceedings{garnierbrun2025how,
  author = {Jerome Garnier-Brun and Marc Mezard and Emanuele Moscato and Luca Saglietti},
  title = {{How Transformers Learn Structured Data: Insights From Hierarchical Filtering}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=AVXApuBCvN},
}
Figure