[Paper Review] Transformers Can Do Bayesian Inference
This paper introduces Prior-Data Fitted Networks (PFNs), a method that uses Transformers to perform Bayesian inference by leveraging in-context learning on set-valued inputs of data points drawn from a prior distribution. PFNs approximate posterior predictive distributions with up to 200× speedup over MCMC and NUTS, achieving near-perfect mimicry of Gaussian processes and strong performance across diverse tasks including tabular regression, few-shot image classification, and Bayesian neural networks.
Currently, it is hard to reap the benefits of deep learning for Bayesian methods, which allow the explicit specification of prior knowledge and accurately capture model uncertainty. We present Prior-Data Fitted Networks (PFNs). PFNs leverage in-context learning in large-scale machine learning techniques to approximate a large set of posteriors. The only requirement for PFNs to work is the ability to sample from a prior distribution over supervised learning tasks (or functions). Our method restates the objective of posterior approximation as a supervised classification problem with a set-valued input: it repeatedly draws a task (or function) from the prior, draws a set of data points and their labels from it, masks one of the labels and learns to make probabilistic predictions for it based on the set-valued input of the rest of the data points. Presented with a set of samples from a new supervised learning task as input, PFNs make probabilistic predictions for arbitrary other data points in a single forward propagation, having learned to approximate Bayesian inference. We demonstrate that PFNs can near-perfectly mimic Gaussian processes and also enable efficient Bayesian inference for intractable problems, with over 200-fold speedups in multiple setups compared to current methods. We obtain strong results in very diverse areas such as Gaussian process regression, Bayesian neural networks, classification for small tabular data sets, and few-shot image classification, demonstrating the generality of PFNs. Code and trained PFNs are released at https://github.com/automl/TransformersCanDoBayesianInference.
Motivation & Objective
- Address the challenge of applying Bayesian methods in low-data regimes by enabling efficient posterior approximation with deep learning.
- Overcome the intractability of exact Bayesian inference in complex models like Gaussian processes and Bayesian neural networks.
- Develop a general-purpose framework that allows any prior distribution from which data can be sampled to be used for posterior approximation.
- Enable scalable, differentiable, and flexible Bayesian inference using only a sampling mechanism for the prior, without requiring analytical forms or complex approximations.
- Demonstrate the method's effectiveness across diverse tasks, including small-tabular data, few-shot learning, and regression, with strong calibration and uncertainty quantification.
Proposed method
- PFNs reframe posterior approximation as a supervised classification problem with set-valued inputs: for each training task, sample a function from the prior, collect a set of (x, y) pairs, mask one label, and train the model to predict the masked label probabilistically.
- The input to the PFN is a set of (x, y) pairs, with one label masked; the model learns to predict the distribution over the missing label using attention mechanisms in a Transformer architecture.
- The PFN is trained end-to-end via maximum likelihood on the masked prediction objective: minimize −∑ log qθ(y_test|x_test, D_train) over multiple sampled tasks.
- During inference, a real dataset D_train and a test point x_test are fed into the trained PFN, which outputs a full predictive distribution qθ*(y_test|x_test, D_train) in a single forward pass.
- A novel predictive distribution is introduced for regression tasks, enabling proper uncertainty estimation in continuous outputs.
- The method supports flexible priors—any distribution from which data can be sampled can be used, including intractable or complex priors such as Bayesian neural networks or Gaussian processes.
Experimental results
Research questions
- RQ1Can Transformers be effectively used to approximate posterior predictive distributions in Bayesian inference without requiring analytical forms of the posterior?
- RQ2To what extent can in-context learning in Transformers be leveraged to perform Bayesian inference across diverse, low-data machine learning tasks?
- RQ3How does the performance of PFNs compare to established Bayesian baselines like MCMC with NUTS and SVI with Bayes-by-Backprop in terms of accuracy, speed, and calibration?
- RQ4Can PFNs generalize across different types of priors, including those that are intractable or difficult to approximate with standard methods?
- RQ5What is the impact of architectural choices (e.g., attention heads, position encoding, activation functions) on the performance of PFNs in posterior approximation?
Key findings
- PFNs achieve near-perfect mimicry of Gaussian process predictions, with a mean AUC of 0.981 on the Dionis dataset and 0.996 on the jannis dataset, outperforming all baselines.
- The PFN-BNN model achieves a mean AUC of 0.855 across 21 tabular datasets with 30 training samples, significantly outperforming T-BNN (0.654) and other baselines in both accuracy and calibration.
- PFNs deliver up to 200× speedup in inference time compared to MCMC with NUTS, with inference taking only 13 seconds on GPU versus 12+ hours for NUTS on the same benchmark.
- The method achieves strong calibration, with an Expected Calibration Error (ECE) of 0.025 for PFN-BNN, significantly lower than the 0.157 ECE of the baseline logistic regression model.
- In few-shot image classification on Omniglot, PFNs achieve a mean AUC of 0.981, outperforming KNN (0.871) and CatBoost (0.945), demonstrating strong generalization to few-shot settings.
- The PFN framework generalizes across diverse priors: it successfully approximates posteriors for Gaussian processes, Bayesian neural networks, and other intractable models using only a sampling mechanism for the prior.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.