Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF014 · Algorithm
Stochastic gradient Hamiltonian Monte Carlo (SGHMC)
Uses momentum, friction, and calibrated noise to counter the extra diffusion introduced by stochastic gradients.
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
Uses momentum, friction, and calibrated noise to counter the extra diffusion introduced by stochastic gradients. Its mechanism differs from simply replacing HMC’s exact gradients with minibatch estimates, which generally disrupts the target distribution.
Question / hypotheses
How can an underdamped stochastic dynamics account for gradient noise while retaining a desired posterior stationary law in the idealized limit?
Architecture / computational structure
The displayed position-first discretization advances position, evaluates the minibatch potential gradient there, then damps momentum and adds calibrated stochastic forcing. A matrix-valued implementation requires a covariance factor rather than elementwise square roots. The example uses scalar friction/noise and explicitly checks positive noise variance.
Assumptions
The stochastic-gradient diffusion approximation and friction/noise correction must be suitable for the problem. The residual covariance C-Bhat must be positive semidefinite. Practical finite-step discretizations, inaccurate noise estimates, and non-Gaussian minibatch noise introduce error; original stationary-law arguments are not automatic finite-run guarantees.
Formula / computational definition
Parameters and notation
Theta is a sampled model state and p auxiliary momentum. Hyperparameters include step epsilon, friction C, minibatch size, refresh schedule, and gradient-noise estimate Bhat. In the displayed diffusion convention B=epsilon*V/2, with V the stochastic-gradient covariance; B is not simply the raw gradient variance.
Fitting, fine-tuning, and adaptation
Initialize from a fitted model if useful, specify priors and sampled modules, and tune dynamics on a pilot run with reference checks. Re-estimate relevant gradient-noise behavior when batch size or data changes. Momentum/state reuse after a target change is an initialization choice, not preservation of old posterior draws.
How to use and calibrate
Compare posterior moments and predictive distributions against full-gradient inference on a small problem. Examine sensitivity to friction, step size, and batch size, along with mixing per unit compute. Report implementation convention and whether gradient-noise covariance was estimated or approximated.
Example application
Sample approximate neural-network parameter uncertainty using minibatches while using momentum to move more efficiently than an overdamped Langevin update.
Interpretation / pitfalls
Watch out
Setting an arbitrary momentum optimizer’s noise variance does not make it SGHMC. If Bhat underestimates injected gradient noise, stationary behavior can be wrong. Ordinary HMC’s acceptance correction is generally unavailable without a full-data calculation; do not imply exactness at finite step.
Python and R code examples
Scope: One scalar identity-mass position-first SGHMC update with supplied friction/noise parameters and innovation; not a verified stationary sampler.
Implementation route: Both components use native arithmetic. Neural use requires a carefully specified custom autodiff implementation or a verified library implementation, with attention to its velocity, step-size, and noise conventions.
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 scalar identity-mass position-first SGHMC update with supplied friction/noise parameters and innovation; not a verified stationary sampler.
import math
theta=.2; momentum=.3; step=.01; friction=.4; Bhat=.1; innovation=-.2
variance=2*step*(friction-Bhat); assert variance>=0
theta_new=theta+step*momentum; grad_U=theta_new # U(theta)=theta^2/2
momentum_new=(1-step*friction)*momentum-step*grad_U+math.sqrt(variance)*innovation
print('noise variance',variance,'momentum',momentum_new,'position',theta_new)
Component demo, not a trained model — One scalar identity-mass position-first SGHMC update with supplied friction/noise parameters and innovation; not a verified stationary sampler.
theta <- .2; momentum <- .3; step <- .01; friction <- .4; Bhat <- .1; innovation <- -.2
variance <- 2*step*(friction-Bhat); stopifnot(variance>=0)
theta.new <- theta+step*momentum; grad.U <- theta.new
momentum.new <- (1-step*friction)*momentum-step*grad.U+sqrt(variance)*innovation
print(c(noise.variance=variance,momentum=momentum.new,position=theta.new))
Sources
- Primary/official source 1proceedings.mlr.press
- Primary/official source 2arxiv.org
From the CodeSplash reference, entry INF014.