ICML 2025

How Transformers Learn Structured Data:
Insights From Hierarchical Filtering
J´erˆome Garnier-Brun 1 2 Marc M´ezard 1 Emanuele Moscato 1 Luca Saglietti 1
Abstract
Understanding the learning process and the em-
bedded computation in transformers is becom-
ing a central goal for the development of inter-
pretable AI. In the present study, we introduce
a hierarchical filtering procedure for data mod-
els of sequences on trees, allowing us to hand-
tune the range of positional correlations in the
data. Leveraging this controlled setting, we pro-
vide evidence that vanilla encoder-only trans-
formers can approximate the exact inference al-
gorithm when trained on root classification and
masked language modeling tasks, and study how
this computation is discovered and implemented.
We find that correlations at larger distances, cor-
responding to increasing layers of the hierarchy,
are sequentially included by the network dur-
ing training. By comparing attention maps from
models trained with varying degrees of filtering
and by probing the different encoder levels, we
find clear evidence of a reconstruction of corre-
lations on successive length scales corresponding
to the various levels of the hierarchy, which we
relate to a plausible implementation of the exact
inference algorithm within the same architecture.
1. Introduction
Transformer-based large language models have revolution-
ized natural language processing, and have notably demon-
strated their capacity to perfectly assimilate the grammati-
cal rules of the languages they are trained on. While this ev-
idence shows that transformers can handle and exploit the
subtle long-range correlations that emerge in natural lan-
guage, their inner workings remain largely unclear.
1Department of Computing Sciences,
Universit`a Boc-
coni, Milan, Italy
2Department of Finance, Universit`a Boc-
coni, Milan, Italy.
Correspondence to: J´erˆome Garnier-Brun
<jerome.garnier@unibocconi.it>.
Proceedings of the 42 nd International Confer...