aleximmer/Laplace

Laplace approximations for Deep Learning.

What is Laplace?

Laplace is a Python library that brings Bayesian uncertainty‑aware inference to any PyTorch neural network. It implements the Laplace approximation – a way of turning a trained deterministic model into a probabilistic one by fitting a Gaussian distribution around the network’s parameters. With the library you can:

  • Approximate the posterior over all weights, a chosen sub‑network, or just the last layer.
  • Estimate the marginal likelihood (useful for model comparison).
  • Produce predictive distributions for classification or regression, giving calibrated uncertainty estimates.

The package is tied to the research paper Laplace Redux – Effortless Bayesian Deep Learning (NeurIPS 2021) and the authors provide a separate repo with the experiments from that paper.


How do you use it?

  1. Install – the core package is on PyPI under the name laplace-torch:
    pip install laplace-torch
    
    (An optional backend can be added with a second pip install command.)
  2. Wrap a trained model – after you have a PyTorch model, create a Laplace object:
    from laplace import Laplace
    la = Laplace(model, "classification",
                 subset_of_weights="all",
                 hessian_structure="diag")
    
    You tell the class what task you’re solving, which parameters to treat probabilistically, and how to approximate the Hessian (e.g., diagonal).
  3. Fit the approximation – call fit with your training data loader. The library computes the (approximate) Hessian of the loss at the MAP point.
  4. Tune the prior – the Gaussian prior’s precision is a hyper‑parameter. Laplace provides a few automatic schemes, such as a grid‑search cross‑validation:
    la.optimize_prior_precision(method="gridsearch",
                                pred_type="glm",
                                link_approx="probit",
                                val_loader=val_loader)
    
  5. Predict with uncertainty – once fitted, you can obtain predictive probabilities (or means/variances for regression) using a chosen predictive approximation:
    pred = la(x, pred_type="glm", link_approx="probit")
    
    The result is a distribution rather than a single point estimate, which you can use for calibrated confidence, out‑of‑distribution detection, model selection, etc.

The README points to extensive documentation and a developer guide for deeper customisation.


Who might find this useful?

  • Researchers exploring Bayesian deep learning, model‑selection, or continual‑learning scenarios who need a ready‑made, well‑tested Laplace implementation.
  • Practitioners who already have a PyTorch model and want to add uncertainty quantification without retraining a full Bayesian neural network.
  • Students learning about posterior approximations; the library’s API mirrors the mathematical steps (fit → optimise prior → predict), making experiments straightforward.

Limitations & practical notes

  • The approximation is not fully automatic – you need to choose a Hessian structure (diag, kron, full, etc.), a prior‑precision optimisation method, and a predictive approximation (GLM, MC, etc.). The README warns that performance can vary widely with these choices.
  • Computing a full Hessian is expensive; most users rely on diagonal or Kronecker‑factored approximations.
  • The library currently targets PyTorch only; other frameworks are not supported out of the box.

Quick start checklist

  1. pip install laplace-torch
  2. Train or load a PyTorch model.
  3. Wrap it with Laplace(...) specifying task and Hessian style.
  4. Call fit(train_loader).
  5. Tune the prior with optimize_prior_precision (gridsearch is a safe default).
  6. Use la(x, ...) to get predictive distributions.

Where to learn more?

Related

  • Project
  • Project
  • Project
  • Project