JaxSGMC: Modular stochastic gradient MCMC in JAX

Stephan Thaler, Paul Fuchs, Ana Cukarska, Julija ZavadlavSoftwareX 2024 · OpenReview

TL;DR

This paper introduces JaxSGMC, a new Python library built on JAX for performing Stochastic Gradient Markov Chain Monte Carlo (SG-MCMC) sampling. JaxSGMC aims to make advanced Bayesian uncertainty quantification (UQ) techniques, crucial for trustworthy deep learning, more accessible by providing a modular and extensible framework for implementing and using SG-MCMC samplers.

What Problem Does This Paper Address?

Deep learning models have achieved remarkable success, but they often act as "black boxes," making it difficult to assess the reliability of their predictions, especially when encountering data outside their training distribution. Uncertainty Quantification (UQ) is key to building trust in these models, particularly for safety-critical applications. While Bayesian methods offer a principled approach to UQ, traditional Markov Chain Monte Carlo (MCMC) methods are computationally prohibitive for large datasets and complex neural networks. Stochastic Gradient MCMC (SG-MCMC) offers a scalable alternative by using mini-batches of data to estimate gradients. However, the landscape of UQ libraries is fragmented, and implementing novel SG-MCMC methods can be challenging, leading to a slow adoption of these powerful techniques.

Methods

JaxSGMC provides a modular framework for SG-MCMC in JAX. Its core innovation lies in its design, which breaks down SG-MCMC samplers into reusable building blocks. This allows users to:

  • Switch between state-of-the-art samplers: JaxSGMC offers implementations of popular SG-MCMC algorithms like SGLD, SGHMC, and AMAGOLD.
  • Build custom samplers: Users can combine these building blocks to create novel SG-MCMC schemes tailored to their specific problems.
  • Leverage JAX's capabilities: The library fully utilizes JAX's automatic differentiation and just-in-time (jit) compilation for efficient computation.
  • Handle large datasets: It incorporates efficient data loading and batching mechanisms, crucial for big data applications where the entire dataset cannot fit into memory.

Datasets

The paper uses the following datasets for evaluation:

  • CIFAR-10: Used for image classification tasks. The authors split it into training, validation, and test sets.
  • Synthetic dataset: Used for a linear regression example.

Key Insights and Ideas

  • Modularity is key for SG-MCMC adoption: By breaking down SG-MCMC into standard components, JaxSGMC significantly lowers the barrier to entry for researchers and practitioners.
  • SG-MCMC offers a powerful alternative to traditional UQ: Unlike methods that focus on single posterior modes (like some variational inference or dropout techniques), SG-MCMC, especially with multiple chains, can capture multiple modes and the volume of the posterior, leading to more comprehensive UQ.
  • JAX is a suitable backend for scalable Bayesian inference: The library's reliance on JAX highlights its potential for efficient, hardware-accelerated Bayesian deep learning.
  • Bridging the gap between optimization and inference: JaxSGMC allows users to transition from stochastic optimization to SG-MCMC sampling with minimal changes to their existing JAX models.

Key Results

  • Linear Regression Example: The paper demonstrates building a custom pSGLD sampler for a linear regression problem. The sampled parameters were compared to a gold-standard Hamiltonian Monte Carlo (HMC) scheme, showing good agreement.
  • Image Classification on CIFAR-10: A pSGLD sampler was used for image classification with a MobileNetV1 architecture.
  • The pSGLD training achieved a test accuracy of 57.32%, comparable to a deterministic model.
  • The SG-MCMC approach enabled UQ by visualizing prediction distributions, revealing instances of overconfident predictions.
  • The computational cost of SG-MCMC sampling was found to be comparable to stochastic optimization, a significant advantage over full-batch HMC.
  • Accuracy vs. Certainty: For the image classification task, increasing prediction certainty (based on ensemble agreement) led to higher test set accuracy, demonstrating the utility of the UQ estimates.

Limitations and Future Work

  • Exploration of Posterior Volume: The authors note that even with SG-MCMC, fully exploiting the theoretical advantage of exploring the posterior volume might require more sophisticated samplers than the basic pSGLD used in the examples.
  • Further Research: They envision JaxSGMC accelerating research into novel SG-MCMC schemes with enhanced posterior exploration capabilities, promoting uncertainty-aware ML and active learning in various fields.
  • Broader Applications: While the examples focus on ML benchmarks, the library is designed to be domain-independent and applicable to other areas like physics-based modeling.

Summary updated

Abstract

We present JaxSGMC, an application-agnostic library for stochastic gradient Markov chain Monte Carlo (SG-MCMC) in JAX. SG-MCMC schemes are uncertainty quantification (UQ) methods that scale to large datasets and high-dimensional models, enabling trustworthy neural network predictions via Bayesian deep learning. JaxSGMC implements several state-of-the-art SG-MCMC samplers to promote UQ in deep learning by reducing the barriers of entry for switching from stochastic optimization to SG-MCMC sampling. Additionally, JaxSGMC allows users to build custom samplers from standard SG-MCMC building blocks. Due to this modular structure, we anticipate that JaxSGMC will accelerate research into novel SG-MCMC schemes and facilitate their application across a broad range of domains.
BibTeX
@inproceedings{thaler2024jaxsgmc,
  author = {Stephan Thaler and Paul Fuchs and Ana Cukarska and Julija Zavadlav},
  title = {{JaxSGMC: Modular stochastic gradient MCMC in JAX}},
  booktitle = {SoftwareX},
  year = {2024},
  url = {https://openreview.net/forum?id=Py2B6w8ISb},
}
Figure