Skip to content

MCMC API

deepuq.methods.mcmc

MCMC utilities based on Stochastic Gradient Langevin Dynamics (SGLD).

SGLDOptimizer

Bases: Optimizer

Stochastic Gradient Langevin Dynamics optimizer.

This optimizer performs an SGD-like update with additive Gaussian noise calibrated by the step size, following Welling & Teh (2011).

step

step()

Apply one SGLD parameter update in-place.

collect_posterior_samples

collect_posterior_samples(
    model: Module,
    data_loader,
    n_steps=1000,
    lr=0.0001,
    weight_decay=0.0001,
    burn_in=0.2,
    loss_fn=None,
    device="cpu",
)

Run SGLD and collect posterior parameter snapshots.

Parameters:

Name Type Description Default
model Module

Neural network to sample.

required
data_loader

Iterable of mini-batches.

required
n_steps

Total SGLD updates.

1000
burn_in

Fraction of updates to skip before collecting snapshots.

0.2

predict_with_samples

predict_with_samples(
    model: Module,
    samples,
    x,
    apply_softmax=True,
    device="cpu",
)

Predictive mean and variance from stored parameter samples.

predict_with_samples_uq

predict_with_samples_uq(
    model: Module,
    samples,
    x,
    apply_softmax=True,
    device="cpu",
) -> UQResult

Predictive uncertainty summary from posterior samples.