DiScoFormer: One transformer for density and score, across distributions

DiScoFormer: One transformer for density and score, across distributions

DiScoFormer (Density and Score Transformer) is a transformer-based model designed to estimate the density and score of a distribution from a given set of data points. Unlike traditional methods, it provides both estimates in a single forward pass without requiring the model to be retrained for every new distribution it encounters.

Technical Architecture and Mechanism

DiScoFormer uses a shared transformer backbone with two specialized output heads: one for density and one for the score. This architecture leverages the mathematical relationship where the score is the gradient of the log-density.

Cross-Attention and KDE Generalization

The model utilizes cross-attention to evaluate density and score at any arbitrary point, not just at the locations of existing data points. The researchers analytically show that the weights of a single attention head are nearly a Gaussian kernel, making the transformer architecture a strict generalization of Kernel Density Estimation (KDE). While KDE uses a single fixed bandwidth for all points, DiScoFormer learns multiple scales and adapts them to the data.

Consistency Loss for Out-of-Distribution Adaptation

Because the score head must match the gradient of the log-density head, any discrepancy between the two serves as a label-free consistency loss. During inference, the model can adapt to out-of-distribution inputs by taking gradient steps on this consistency loss while holding the context fixed, requiring no ground-truth density or score labels.

Training Methodology

DiScoFormer was trained using Gaussian Mixture Models (GMMs). GMMs were chosen because they are universal density approximators and provide closed-form densities and scores for exact supervision. To provide the model with virtually unlimited examples, a new GMM was drawn for every training batch.

Performance and Benchmarks

DiScoFormer outperforms Kernel Density Estimation (KDE) across all tested metrics, with the performance gap widening as dimensionality increases.

  • High-Dimensional Accuracy: In 100 dimensions, DiScoFormer reduces score error by approximately 6.5x and density error by more than 37x compared to the best hand-tuned KDE.
  • Generalization: The model remains accurate on distributions it did not see during training, including non-Gaussian shapes like Student-t and Laplace distributions, as well as mixtures with more modes than encountered during training.
  • Efficiency: While KDE remains faster for very small datasets, DiScoFormer scales better with more samples and does not suffer from the same memory limitations as KDE in high dimensions.

Implications for Machine Learning and Science

Score estimation is a critical component in generative modeling (such as diffusion-based image generators), Bayesian inference, and scientific computing (e.g., plasma simulations). DiScoFormer provides a pretrained, plug-in estimator that removes the need to retrain models for every specific problem, potentially reducing the computational cost across these diverse fields.

Sources