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

Transformers Simulate MLE for Sequence Generation in Bayesian Networks

Yuan Cao, Yihan He, Dennis Wu, Hong-Yu Chen, Jianqing Fan, Han Liu · DOI

Abstract

While Transformers have revolutionized sequence generation, the theoretical mechanisms behind their ability to capture complex data dependencies remain poorly understood. This paper bridges the gap between Transformers and classical probabilistic graphical models by demonstrating that a two-layer Transformer can theoretically implement the Maximum Likelihood Estimation (MLE) algorithm. We show that these models can learn the structure of a Bayesian Network (BN) in-context and generate new sequences accordingly. Our findings are validated through extensive experiments on both synthetic and real-world datasets, positioning Transformers as effective in-context statistical estimators.

Introduction

Transformers have become the backbone of modern sequence generation, yet our theoretical understanding of their generative capabilities is limited. While previous research has shown that Transformers can learn simple Markov chains, real-world data often follows more complex dependency structures. This paper addresses a more ambitious question: Can Transformers handle Bayesian Networks (BNs)? In a BN, the probability of a variable depends on a specific set of "parent" variables rather than just the immediate predecessor. The authors investigate if a Transformer can perform In-Context Learning (ICL) for BNs—inferring underlying rules from a context of independent observations to sample new, valid sequences.

Methodology

Theoretical Construction

The authors prove the existence of a two-layer Transformer that simulates the MLE algorithm through two distinct functional stages:

  • Layer 1 (Parent Selection): This layer uses self-attention to identify which variables in the sequence are the "parents" of the current variable of interest, effectively trimming the input to focus on relevant causal dependencies.
  • Layer 2 (MLE Estimation): This layer performs in-context "frequency counting." It examines provided examples, counts the occurrences of specific parent-child combinations, and calculates the resulting conditional probability distribution.

Empirical Training and Sampling

To test if these models can be learned via gradient descent, the authors trained a 6-layer Transformer using a standard next-token prediction objective. The training utilized a data-level curriculum, starting with simple graphs and increasing complexity as the model's loss decreased. Specialized positional embeddings ($p$ and $p_q$) were employed to signal which variable was currently being predicted.

During inference, the model generates sequences using an autoregressive sampling procedure. This process involves taking observations as input and iteratively sampling variables based on the model's predicted conditional distributions.

Algorithm 1: Autoregressive Sampling procedure for sequence generation.

Experimental Setup

Datasets

  1. Synthetic Bayesian Networks: The authors generated 50,000 random graphs for three structures: Chains, Trees, and General Graphs, using binary variables.
  2. ACSIncome (Real-World): Derived from the American Community Survey, this task involves predicting whether an individual's income exceeds $50,000 based on 10 categorical features. Different US states and years were used as distinct "contexts" to evaluate in-context adaptation.

Key Results and Insights

  • Transformers as Statisticians: The core insight is that the Transformer architecture is naturally suited to implementing classical statistical algorithms like MLE. It does not merely memorize patterns; it implements the logic of frequency counting and conditional probability estimation.
  • Structure Awareness: Even without explicit graph structure information, the trained Transformer learned to ignore irrelevant variables. It outperformed "Naive Inference" (which assumes every previous variable is a parent), demonstrating successful identification of sparse dependency structures.
  • Sample Efficiency: In synthetic tests, the Transformer's accuracy closely tracked the "Oracle MLE" (the performance of an estimator with prior knowledge of the graph structure).
  • Zero-Shot Adaptation and Robustness: The models generalized well to varying numbers of examples ($N$). Unlike classical MLE baselines, which often fail when encountering unseen parent-child combinations, the Transformer utilized a "learned prior" from its training phase to make sensible predictions on novel data.

Conclusion

This research establishes that Transformers can act as in-context statisticians, simulating MLE to generate sequences from complex Bayesian Networks. While a 2-layer model is theoretically sufficient, deeper models provide the robustness necessary for practical training. Future work will look toward extending these findings to continuous variables and more dense, overlapping graph structures.

Summary updated

BibTeX
@misc{cao2025transformers,
  author = {Yuan Cao and Yihan He and Dennis Wu and Hong-Yu Chen and Jianqing Fan and Han Liu},
  title = {{Transformers Simulate MLE for Sequence Generation in Bayesian Networks}},
  year = {2025},
  eprint = {2501.02547},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2501.02547},
}
Figure