Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF013 · Algorithm
Stochastic gradient Langevin dynamics (SGLD)
Adds appropriately scaled Gaussian noise to minibatch log-posterior gradient updates to approach Langevin posterior sampling.
In CodeSplash
Documented in the Academy only. There's no Set up button.
- Availability
- Explained here
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Adds appropriately scaled Gaussian noise to minibatch log-posterior gradient updates to approach Langevin posterior sampling. It addresses large-data gradient costs and differs from both deterministic SGD optimization and ordinary full-data Metropolis-adjusted sampling.
Question / hypotheses
Can unbiased stochastic gradients and a controlled decreasing step schedule approximate the posterior diffusion rather than converge only to a point estimate?
Architecture / computational structure
Sample a minibatch, construct an unbiased log-posterior gradient, take a drift step, and inject independent Gaussian noise. There is no ordinary MH correction in basic SGLD. The component uses a normal-mean model and a supplied standard-normal innovation to expose both scaling terms.
Assumptions
The minibatch gradient must estimate the full log posterior with correct prior and N/B likelihood scaling. Asymptotic convergence requires regularity and a suitable step schedule; a common stochastic-approximation condition is sum epsilon_t=infinity and sum epsilon_t^2<infinity. Constant-step implementations usually retain discretization bias.
Formula / computational definition
Parameters and notation
The iterates are intended posterior states; model priors determine regularization probabilistically. Hyperparameters include initial step, decay exponent, batch size, burn-in, collection schedule, and parameterization. The Gaussian noise standard deviation is sqrt(epsilon), not epsilon, for the displayed half-gradient convention.
Fitting, fine-tuning, and adaptation
A pretrained checkpoint can initialize sampling, but initial point-estimate quality does not establish posterior exploration. Define the sampled parameter subset and its prior before starting. If the dataset or objective changes, update scaling and re-establish the sampling regime; simply adding arbitrary noise to an optimizer is insufficient.
How to use and calibrate
On tractable models, compare moments across step sizes with exact posterior values. Plot traces, running averages, and sensitivity to schedule and batch size. Evaluate posterior predictive calibration separately from training loss, and acknowledge bias/mixing tradeoffs under finite compute.
Example application
Approximate posterior uncertainty in a Bayesian classifier head when the training set is too large for a full likelihood gradient at every iteration.
Interpretation / pitfalls
Watch out
Noisy minibatch gradients alone do not provide the correct diffusion. Arbitrary adaptive optimizers require additional theory or corrections. The original asymptotic argument does not certify a practical short constant-step neural run. Injected noise is not, by itself, a differential-privacy guarantee.
Python and R code examples
Scope: One correctly scaled SGLD update for a normal-mean likelihood with a standard-normal prior; a supplied innovation replaces RNG for cross-language arithmetic comparison.
Implementation route: Native NumPy/base R demonstrate the update. A full neural implementation needs an autodiff gradient, careful loss reduction, explicit priors, and a documented schedule; no generic optimizer with a similar name is assumed equivalent.
Dependencies and validation: Python — executed successfully on the supplied synthetic inputs; imports identify NumPy/SciPy dependencies. R — base R, reviewed but not executed because Rscript is unavailable. Random-number streams differ between languages; matching seed integers do not imply matching draws.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — One correctly scaled SGLD update for a normal-mean likelihood with a standard-normal prior; a supplied innovation replaces RNG for cross-language arithmetic comparison. · needs numpy
import numpy as np
theta=.2; batch=np.array([1.,.5,1.2,.8]); N=100; eps=.001; innovation=.3
gradient=-theta+N/len(batch)*np.sum(batch-theta) # both variances are one
next_theta=theta+.5*eps*gradient+np.sqrt(eps)*innovation
print('full-log-posterior gradient estimate',gradient,'new state',next_theta)
Component demo, not a trained model — One correctly scaled SGLD update for a normal-mean likelihood with a standard-normal prior; a supplied innovation replaces RNG for cross-language arithmetic comparison.
theta <- .2; batch <- c(1,.5,1.2,.8); N <- 100; eps <- .001; innovation <- .3
gradient <- -theta+N/length(batch)*sum(batch-theta)
next.theta <- theta+.5*eps*gradient+sqrt(eps)*innovation
print(c(gradient=gradient,new.state=next.theta))
Sources
- Primary/official source 1stats.ox.ac.uk
From the CodeSplash reference, entry INF013.