blackjax.sgmcmc.sghmc#

Public API for the Stochastic gradient Hamiltonian Monte Carlo kernel.

Functions#

init(→ blackjax.types.ArrayLikeTree)

build_kernel(→ Callable)

Stochastic gradient Hamiltonian Monte Carlo (SgHMC) algorithm.

as_top_level_api(→ blackjax.base.SamplingAlgorithm)

Implements the (basic) user interface for the SGHMC kernel.

Module Contents#

init(position: blackjax.types.ArrayLikeTree) blackjax.types.ArrayLikeTree[source]#
build_kernel(alpha: float = 0.01, beta: float = 0) Callable[source]#

Stochastic gradient Hamiltonian Monte Carlo (SgHMC) algorithm.

as_top_level_api(grad_estimator: Callable, num_integration_steps: int = 10, alpha: float = 0.01, beta: float = 0) blackjax.base.SamplingAlgorithm[source]#

Implements the (basic) user interface for the SGHMC kernel.

The general sghmc kernel builder (blackjax.sgmcmc.sghmc.build_kernel(), alias blackjax.sghmc.build_kernel) can be cumbersome to manipulate. Since most users only need to specify the kernel parameters at initialization time, we provide a helper function that specializes the general kernel.

Example

To initialize a SGHMC kernel one needs to specify a schedule function, which returns a step size at each sampling step, and a gradient estimator function. Here for a constant step size, and data_size data samples:

grad_estimator = blackjax.sgmcmc.gradients.grad_estimator(logprior_fn, loglikelihood_fn, data_size)

We can now initialize the sghmc kernel and the state. Like HMC, SGHMC needs the user to specify a number of integration steps.

sghmc = blackjax.sghmc(grad_estimator, num_integration_steps)

Assuming we have an iterator batches that yields batches of data we can perform one step:

step_size = 1e-3
minibatch = next(batches)
new_position = sghmc.step(rng_key, position, minibatch, step_size)

Kernels are not jit-compiled by default so you will need to do it manually:

step = jax.jit(sghmc.step)
new_position, info = step(rng_key, position, minibatch, step_size)
Parameters:

grad_estimator – A function that takes a position, a batch of data and returns an estimation of the gradient of the log-density at this position.

Return type:

A SamplingAlgorithm.