AI Glossary / Mathematics and optimization / Advanced
Stochastic gradient Langevin dynamics
Definition
Stochastic gradient Langevin dynamics, or SGLD, is a method for approximately sampling a Bayesian posterior distribution using mini-batch gradients and deliberately added Gaussian noise. It combines the low per-update data cost of stochastic gradient methods with Langevin sampling. Max Welling and Yee Whye Teh introduced the approach in 2011. Its purpose is to explore parameter uncertainty rather than simply return one optimized parameter vector.
What it means
Ordinary gradient optimization moves toward a high-scoring parameter setting. Posterior sampling instead seeks a collection of parameter values distributed according to a model's posterior probabilities. In SGLD, a gradient encourages movement toward high posterior density, while injected noise supports exploration. Mini-batch selection also creates noise, but that noise is not interchangeable with the deliberately calibrated Gaussian term. The target distribution depends on the likelihood, prior and scaling conventions, so adding arbitrary noise to an optimizer is not automatically SGLD.
How it works
For N observations and a uniformly sampled batch of size n, estimate the log-posterior gradient as the prior gradient plus (N/n) times the sum of batch log-likelihood gradients. In the original convention, update θ by adding (ε/2) times that estimate and independent Gaussian noise with covariance εI. Equivalently, the noise is √ε times a standard normal vector. The original method reduces step sizes over time under stated conditions to limit discretization and gradient-noise effects. It omits the ordinary full-data Metropolis - Hastings correction. Finite runs and fixed step sizes generally retain approximation error, and slow movement between posterior regions can remain a serious practical limitation.
Three practical examples
- A Bayesian logistic-regression experiment can use SGLD to approximate parameter draws when full-data gradient evaluations are expensive.
- A neural-network research workflow can investigate predictions averaged over sampled parameter states, while testing whether the sampling procedure explores meaningfully different states.
- A methods study can compare SGLD estimates with an analytically known posterior on a tiny model before applying the implementation to a complex problem.
Think of it this way
Think of exploring a landscape using both a slope guide and carefully sized random nudges. The guide favors some regions, while the nudges keep the explorer from simply settling at one peak. The analogy captures drift and diffusion. It does not mean the explorer covers every valley quickly, and only the correctly specified dynamics and assumptions connect the trajectory to the intended probability distribution.
Worked example
Let θ have prior N(0, 1), and let four observations [0, 1, 2, 3] be conditionally independent N(θ, 1). At θ = 1, the prior log-gradient is −1. Suppose the selected batch contains observations 1 and 3. Their likelihood gradients are 1 − 1 = 0 and 3 − 1 = 2. Scaling their sum by N/n = 4/2 gives 4, so the estimated log-posterior gradient is 3. Choose ε = 0.04 and an illustrative standard-normal value z = 0.5. The update is θnew = 1 + (0.04/2)(3) + √0.04(0.5) = 1.16. The full-data gradient at θ = 1 is instead −1 + (0 + 1 + 2 + 3 − 4) = 1. A particular batch estimate need not equal it; uniform batch sampling makes the estimate unbiased before the batch is observed. This model's exact posterior is N(6/5, 1/5), providing a reference for later sampling checks. One update to 1.16 does not demonstrate that the algorithm has sampled that posterior correctly, and the chosen z is a worked value rather than an executed random experiment.
Illustrative case study
In a fictional project, Spruce Learning tests an SGLD implementation on the small Gaussian model before using it in a larger classifier. Developers compare estimated posterior means and variances against the known answer and inspect sensitivity to step size and run length. They also compare separate chains rather than assuming a long sequence of highly correlated states supplies many independent samples. The case illustrates validation of a sampler, not evidence that its uncertainty estimates are calibrated for a deployed application.
Common misconceptions and mistakes
The Gaussian noise variance is ε in the stated convention, so its standard deviation is √ε; confusing the two changes the algorithm. A batch-average training loss requires consistent scaling before it represents the full posterior gradient. The prior term is not multiplied by N/n in the displayed update. Small changes between successive states do not prove adequate mixing. Posterior draws are generally correlated, and finite-step bias is distinct from ordinary Monte Carlo variation. An inaccurate prior or likelihood can produce misleading uncertainty even if the sampling computation is otherwise sound.
Business use
Use SGLD when approximate Bayesian sampling with mini-batches fits the modeling and computational goals. Record the likelihood, prior, batch scheme, step-size schedule, noise convention and retained-state policy. Validate implementation on a tractable model, monitor mixing and compare meaningful posterior summaries across runs. When the step size changes, use an estimator appropriate to that schedule rather than assuming every stored state has identical sampling properties. Report the limitations of finite sampling and approximate dynamics. Better optimization loss alone is not a suitable criterion for deciding that the posterior has been explored well.
People also ask
Is SGLD just stochastic gradient descent with extra noise?
That is an intuitive starting point, but correct posterior targeting requires specific gradient scaling, noise scaling and assumptions.
Does it need a known posterior normalizing constant?
No. Log-density gradients can be evaluated without that constant.
Are fixed-step implementations exact posterior samplers?
Generally no. Without additional corrections or special structure, a nonzero step leaves discretization and stochastic-gradient effects that must be assessed.
Check your understanding
With ε = 0.04 in the stated convention, what is the standard deviation of the injected one-dimensional Gaussian noise?
Answer: √0.04 = 0.2, not 0.04.
Related terms
- Markov chain Monte Carlo
- Mini-batch
- Effective sample size
- Bayesian posterior
- Langevin dynamics
- stochastic gradient
Learn next
derive the Gaussian posterior in the worked example and implement repeated updates with several decreasing step schedules. Compare estimated mean and variance with 1.2 and 0.2. Inspect autocorrelation and repeat runs before moving to a model without an exact reference answer.
More in Mathematics and optimization
- Absolute error loss - Absolute error loss measures a numerical prediction's error as the absolute difference between its predicted and reference values: L(y,y-hat)=|y-y-hat|.
- Active-set method - An active-set method is an optimization approach that works with a changing subset of constraints believed to be tight at the solution.
- Adjoint state method - The adjoint state method computes how a chosen objective changes when the parameters of a mathematical model change.
- Σ-algebra
- Algebraic connectivity - Algebraic connectivity is the second-smallest eigenvalue of a graph's Laplacian matrix.
- Algorithm IMED - Algorithm IMED, short for Indexed Minimum Empirical Divergence, is a method for choosing actions in a stochastic multi-armed bandit problem.
Sources
Sources: Max Welling and Yee Whye Teh, Bayesian Learning via Stochastic Gradient Langevin Dynamics, ICML 2011, especially equations 2–7, https://icml.cc/2011/papers/398_icmlpaper.pdf . The Gaussian posterior, mini-batch gradient and numerical update are original calculations using the paper's ε/2 drift and ε noise-covariance convention.
Previous: Stochastic gradient Hamiltonian Monte Carlo Next: Stochastic grammar