Transformers are Bayesian Networks
Abstract
Transformers are the dominant architecture in AI, yet why they work remains poorly understood. This paper offers a precise answer: a transformer is a Bayesian network. We establish this in five ways. First, we prove that every sigmoid transformer with any weights implements weighted loopy belief propagation on its implicit factor graph. One layer is one round of BP. This holds for any weights -- trained, random, or constructed. Formally verified against standard mathematical axioms. Second, we give a constructive proof that a transformer can implement exact belief propagation on any declared knowledge base. On knowledge bases without circular dependencies this yields provably correct probability estimates at every node. Formally verified against standard mathematical axioms. Third, we prove uniqueness: a sigmoid transformer that produces exact posteriors necessarily has BP weights. There is no other path through the sigmoid architecture to exact posteriors. Formally verified against standard mathematical axioms. Fourth, we delineate the AND/OR boolean structure of the transformer layer: attention is AND, the FFN is OR, and their strict alternation is Pearl's gather/update algorithm exactly. Fifth, we confirm all formal results experimentally, corroborating the Bayesian network characterization in practice. We also establish the practical viability of loopy belief propagation despite the current lack of a theoretical convergence guarantee. We further prove that verifiable inference requires a finite concept space. Any finite verification procedure can distinguish at most finitely many concepts. Without grounding, correctness is not defined. Hallucination is not a bug that scaling can fix. It is the structural consequence of operating without concepts. Formally verified against standard mathematical axioms.
Figures & tables
| Graph | BP exact | Transformer | Max error |
|---|---|---|---|
| 0 | [0.7349, 0.4366] | [0.7338, 0.4346] | 0.0021 |
| 1 | [0.4097, 0.4036] | [0.4096, 0.4031] | 0.0005 |
| 4 | [0.6459, 0.8298] | [0.6436, 0.8297] | 0.0023 |
| 9 | [0.4084, 0.5523] | [0.4084, 0.5526] | 0.0003 |
| Dims | Content |
|---|---|
| 0 | own belief (initialized to 0.5) |
| 1–4 | factor table entries |
| 5 | node type (0 = variable, 1 = factor) |
| 6 | own index |
| 7 | neighbor index |
| Job | Input | Semantics | |
|---|---|---|---|
| Softmax 1 | Routing | Q/K dot products | Differentiable argmax |
| Softmax 2 | Inference | Neighbor beliefs | Bayesian posterior |
| Softmax 3 | Generation | Final hidden state | Token distribution |
| Experiment | Vars | Loops | Converged | Avg KL | Avg MAE |
|---|---|---|---|---|---|
| Triangle | 3 | 1 | 100/100 | 0.000045 | 0.002091 |
| Square | 4 | 1 | 100/100 | 0.000002 | 0.000382 |
| Dating graph | 5 | 1 | 100/100 | 0.000102 | 0.003590 |
| Two loops | 4 | 2 | 100/100 | 0.000086 | 0.003111 |
| QBBN chain | 6 | 1 | 100/100 | 0.000021 | 0.001055 |
| Work | Method | Result | Relation |
|---|---|---|---|
| Giannou et al. 2023 | Constructive | TF Turing complete | Same conclusion, different construction |
| Akyürek et al. 2022 | Constructive | TF implements GD | Same method; BP exact, GD approximate |
| Xie et al. 2021 | Statistical | ICL Bayesian | Statistical version; we give mechanistic |
| Jung et al. 2022 | Formal | Attention is BP (latent) | Complementary; any weights vs. trained |
| Scarselli et al. 2009 | Structural | GNN implements BP | Extends to TF without hardwired topology |
| Elhage et al. 2021 | Empirical | Circuits in trained models | We give target circuit for interpretability |
Appendix figures & tables1 asset
Supplementary material from the paper’s appendix.
Appendix
| Repo | What it proves / shows |
| universal-lean | transformer_is_turing_complete |
| https://github.com/gregorycoppola/universal-lean | |
| sigmoid-transformer-lean | every_sigmoid_transformer_is_bayesian_network |
| uniqueness : exact posteriors force BP weights | |
| https://github.com/gregorycoppola/sigmoid-transformer-lean | |
| transformer-bp-lean | transformer_implements_bp |