arXiv:2610.18725cs.LGOct 2026

Manuscript Offprint

arXiv:2610.18725

Learning Source Structure: Bayesian Prediction with Transformers

Meir Feder, Ruediger Urbanke

ABSTRACT

A transformer trained on sequences whose source parameters are drawn afresh each time can learn only what the sequences share: the source family and its prior. The Bayesian predictor that knows both, which we call informed Bayes, is the unique minimizer of the training loss and is computable from the family alone; we use it as a quantitative, falsifiable reference. Where earlier work compared transformers with Bayes on sources whose contexts can be learned independently, we build Markov sources with up to 64 symbols in which observations in one context inform prediction in another: product sources with known or hidden encodings, transition rows sharing an unknown base law and depth, and a mixture of three families. Informed Bayes is evaluated numerically from the row integrals of the layered simplex architecture. Our main result is that informed Bayes predicts, from the family and prior alone, how a trained transformer behaves in four tests that go beyond the learning curve: the loss that remains under a source outside the training family (predicted 2.237 bits, measured 2.309), the doubling of the extra loss when two independent components change rather than one (0.345 predicted, 0.350 measured), the benefit of other contexts when a token appears for the first time, and the depth at which copying an earlier continuation starts to pay. On the learning curves themselves, small transformers match informed Bayes when the shared structure is visible in the tokens, and hidden structure needs more layers and training, with gaps below 0.01 bits per token for the largest models. We explain why the imitation is possible and where it must fail: informed Bayes decomposes into sufficient statistics that attention selects and accumulates and an estimator, fixed by the prior, that the feed-forward network learns; in the architecture-as-prior framework, training pays the complexity of informed Bayes under the architecture's prior, and finite-memory universal prediction bounds how closely a bounded network can reach it.

1 INTRODUCTION

A transformer that is trained on many sequences can learn what their sources have in common. Suppose every sequence comes from a source with freshly drawn parameters. Then training cannot reveal the parameters of the next sequence. It can reveal the family of sources, and how the parameters within one source are related: each token may be a pair whose two parts follow independent chains, or all contexts may share a preference for certain tokens. A predictor that knows such a relation can pool observations across contexts. How well does a transformer learn to use this shared structure?

There is a natural yardstick. The informed Bayesian predictor, informed Bayes for short, is told the source family and the prior. It is not told the parameters of the sequence at hand; after each token it updates its posterior belief about them and averages the next-token prediction over this belief. When sequences are drawn from the family and the prior, no predictor that sees the same tokens has a smaller expected log loss. The transformer is trained to minimize this same loss and has the memory to approximate the informed Bayes. We ask two questions. How close does a transformer, which must learn the family from examples, get to informed Bayes, even in shared structure? And can this correspondence predict the transformer's behavior from what we can infer for informed Bayes?

Earlier work points out the following. The prediction over a new test sequence (prompt) was called in-context learning and has been explained as inference about a hidden source (Xie et al., 2022). Networks trained on samples from a prior learn to predict like the posterior (Müller et al., 2022). Regression experiments find Bayesian behavior within and across task families (Panwar et al., 2024). Information-theoretic work separates what is learned across tasks from what is inferred within a new task (Jeon et al., 2024). Meta-trained transformers and LSTMs were shown to reach the universal predictor of their training class (Grau-Moya et al., 2024). For sequential sources, the closest studies use Markov chains with independently drawn transition rows (Edelman et al., 2024; Bondaschi et al., 2026), smoothing across context orders (D'Angelo et al., 2026), variable-order context trees (Zhou et al., 2026), and hidden causal structures (Wei et al., 2026). None of this guarantees that a trained network reaches the Bayesian optimum; we measure in bits how close it gets when contexts are tied together (Appendix A compares these papers in detail).

In the experiments reported in this paper each of the sources tests one kind of shared structure. Coins test whether the prior on one unknown bias is learned. In product sources every token is a pair of independent coordinates. The table that maps pairs to token labels, the encoding, is either known or hidden. In the shared-base source all transition rows are built from one unknown base law and one unknown depth, so tokens seen in one context help in every other context. The uniform-base source, with a known uniform base law, is a control, and in pooled training the family itself is unknown. The Bayesian predictors are built from the row integrals of the layered simplex architecture (LSA) (Feder et al., 2026).

Our main finding is that informed Bayes predicts, in bits, how a trained transformer responds in four tests that deviate from the training conditions (Section 4; Table 1). In each test we first work out what informed Bayes does, from the family and prior alone, and then measure the transformer. Two tests change the test source: permuting the token labels of a product source, where Bayes predicts the loss that remains however long the sequence is, and replacing one or both transition matrices mid-sequence, where Bayes predicts that two new matrices cost twice as much as one. Two stay inside the training family: whether tokens seen in other contexts help when a token appears for the first time, and at which source depth copying an earlier continuation starts to pay. The transformer meets all four predictions.

The precondition for these predictions is that the transformers learn the shared structure at all, and Section 3 shows that they do. Two-layer models are close to informed Bayes when the structure is visible in the tokens: for coins, for the known encoding and for the uniform base. When the encoding or the base law is hidden, two layers are not enough, and eight-layer models trained on four times as many sequences close most of the remaining gap.

Section 5 asks why the transformer can imitate (approximately) the informed Bayes: the training objective is minimized by informed Bayes whose prediction structure is a function of some statistics of the prefix that attention can select and accumulate, leaving only a map from, e.g., counts to a forecast to be learned. In the framework where an architecture is a prior over predictors (Feder et al., 2025), training pays the complexity of informed Bayes under that transformer architecture's prior, while informed Bayes pays the complexity of the source under the family's prior. What the sources examples discussed here add is that these statistics are global rather than local, which explains which sources need more capacity and training, and where the residual gap sits.

References (50)