optax_bayes¶
The Bayesian Learning Rule as optax
GradientTransformations.
Train with a normal optax loop, then read an approximate Gaussian posterior straight out of the optimizer state. Implements the Bayesian Learning Rule (Khan & Rue, 2023) in diagonal, full-rank, and low-rank variants, plus IVON (Shen et al., 2024) and Newton's method.
New here? Start with the Diagonal BLR Walkthrough, then browse the API Reference for the full surface. The Mathematical Background derives every update, and Vision, Architecture, Scope and Decisions cover the design.
Installation¶
pip install optax_bayes # slim core: diagonal BLR + IVON
pip install "optax_bayes[gaussx]" # + full-rank / low-rank structured backend
pip install "optax_bayes[all]" # everything
Or with uv:
The core package depends only on jax, optax, jaxtyping, and lineax;
the full-rank and low-rank transforms need the optional
gaussx extra.
Quickstart¶
import jax
import optax
from optax_bayes import blr_diagonal_for_loss, get_posterior_diagonal
opt = blr_diagonal_for_loss(learning_rate=1e-3, prior_precision=1e-4)
state = opt.init(params)
for batch in dataloader:
grads = jax.grad(loss_fn)(params, batch)
updates, state = opt.update(grads, state)
params = optax.apply_updates(params, updates)
mean, variance = get_posterior_diagonal(state) # q(theta) = N(mean, diag(variance))
Everything composes like a normal optax transform:
API Notes¶
A few conventions worth calling out up front:
- The
*_for_losstransforms accept standard loss gradients (likeoptax.adam); the bareblr_*transforms expect log-likelihood gradients. - Optimizer state is stored in natural-parameter form and initialises its
mean at the params passed to
opt.init;prior_mean/prior_precisionanchor every update. - Full-rank and low-rank entry points raise an informative
ImportErrorunless thegaussxextra is installed.