Stochastic Gradient Descent as Approximate Bayesian Inference

Stephan Mandt, Matthew D. Hoffman, David M. Blei

Abstract

Stochastic Gradient Descent (SGD) with a constant learning rate is typically viewed as an optimization algorithm that converges to a neighborhood of a local minimum. This paper reinterprets this behavior through the lens of Bayesian inference, demonstrating that SGD can be viewed as a Markov chain that samples from a stationary distribution. By approximating the discrete-time SGD process with a continuous-time Ornstein-Uhlenbeck process, the authors derive optimal hyperparameters—including learning rates and preconditioning matrices—that minimize the Kullback-Leibler divergence between the SGD stationary distribution and the true posterior. This framework provides a scalable, fast alternative to traditional MCMC methods, effectively turning a standard optimization tool into a powerful approximate inference algorithm.

Introduction

In modern machine learning, practitioners often face a stark choice between optimization and Bayesian inference. Optimization algorithms like SGD are the workhorses of the field, prized for their ability to scale to massive datasets and high-dimensional parameter spaces. However, they typically provide only a single point estimate, ignoring the uncertainty inherent in the model. Bayesian inference, which captures this uncertainty by sampling from a posterior distribution, offers a more robust framework but often relies on computationally expensive Markov Chain Monte Carlo (MCMC) methods.

Recent "scalable MCMC" methods, such as Stochastic Gradient Langevin Dynamics (SGLD), have attempted to bridge this gap by adding artificial noise to SGD. However, these methods often require decaying learning rates to ensure convergence to the correct distribution, which significantly slows down the exploration of the parameter space (mixing time). This paper asks whether the inherent noise of SGD minibatches—usually considered a nuisance—can be mathematically harnessed to perform approximate Bayesian inference without the need for artificial noise or decaying rates.

Theoretical Framework: SGD as an OU Process

The authors bridge the gap between optimization and sampling using the mathematics of continuous-time stochastic differential equations. They demonstrate that discrete-time SGD with a constant learning rate can be approximated as a continuous-time Ornstein-Uhlenbeck (OU) process.

This approximation allows for the analytical calculation of the "stationary distribution" of SGD—the region where the algorithm eventually settles and fluctuates. By assuming the true posterior is approximately Gaussian near the local optimum (a common assumption in Laplace approximations), the authors treat SGD's stationary distribution as a variational approximation.

Variational Interpretation and Optimal Tuning

To align the SGD stationary distribution with the true posterior, the authors minimize the Kullback-Leibler (KL) divergence between the two. This optimization yields exact formulas for:

  1. Optimal Learning Rates: The constant learning rate should be proportional to the ratio of the minibatch size to the total dataset size, and inversely proportional to the trace of the gradient noise covariance.
  2. Preconditioning Matrices: Similar to algorithms like AdaGrad or RMSProp, specific preconditioning matrices can be derived to force SGD to sample from the best possible approximation of the posterior.

Experimental Validation

The authors validated their theoretical framework across several regression and classification tasks, including the Wine Quality, Protein Tertiary Structure, and Skin Segmentation datasets. They also tested the scalability of the approach on MNIST and Covertype datasets.

Key Results

  • Competitive Approximations: Constant SGD, when tuned with the derived optimal preconditioning, matched or outperformed standard scalable MCMC methods (SGLD, SGFS) and Black-Box Variational Inference (BBVI) in approximating the true posterior.
  • Iterate Averaging (IASG): The authors proposed the Iterate Averaging Stochastic Gradient (IASG) sampler. In experiments, IASG converged to stable, accurate estimates of posterior variance significantly faster than the No-U-Turn Sampler (NUTS) and SGLD.
  • Efficient Hyperparameter Tuning: By treating constant SGD as a Variational EM algorithm, the authors successfully optimized hyperparameters (such as $L_2$ regularization) on the fly. This approach achieved validation losses comparable to BBVI while offering a more efficient alternative to traditional cross-validation.

Insights and Implications

  • Noise as a Feature: The random noise generated by subsampling data in SGD acts exactly like the artificial noise injected in traditional MCMC. If the learning rate and preconditioning are tuned correctly, extra noise is unnecessary.
  • Momentum Effects: Adding momentum to SGD does not change the fundamental shape of the stationary distribution; it merely scales its covariance. This implies that SGD with momentum is also a viable candidate for Bayesian inference.
  • Theoretical Limits: The analysis of Polyak iterate averaging suggests a theoretical "speed limit" for MCMC: under optimistic assumptions, no black-box stochastic-gradient MCMC method can generate more than one independent posterior sample per full pass over the dataset.

Limitations and Future Work

The primary limitation of this framework is the Local Gaussian Assumption. The mathematical derivations assume the loss landscape is locally quadratic and that the gradient noise covariance is constant near the optimum. While this holds in deep local minima, it may fail in the highly non-convex regions encountered during early training. Additionally, estimating the full noise covariance matrix for preconditioning can be computationally expensive for models with millions of parameters, though diagonal approximations have shown promise in practice.

Future research directions include exploring the similarities in noise characteristics between BBVI and Langevin MCMC, and further developing iterate averaging as a primary tool for Bayesian sampling in deep learning.

Summary updated

BibTeX
@article{mandt2017stochastic,
  author = {Stephan Mandt and Matthew D. Hoffman and David M. Blei},
  title = {{Stochastic Gradient Descent as Approximate Bayesian Inference}},
  journal = {J. Mach. Learn. Res.},
  year = {2017},
  url = {https://openreview.net/forum?id=S6fPdz5gPl9},
}
Figure