Diagonal BLR¶
The workhorse variant: a diagonal Gaussian \(q(\theta) = \mathcal{N}(m, \operatorname{diag}(1/s))\) with Adam-like \(O(d)\) cost per step. Works on arbitrary parameter pytrees, so it drops into any optax training loop unchanged.
Transforms¶
Most users want blr_diagonal_for_loss
(standard loss gradients, like optax.adam); blr_diagonal is the inner
log-likelihood-convention transform; blr_with_schedule runs the loss
wrapper under an optax learning-rate schedule via optax.inject_hyperparams.
blr_diagonal_for_loss(learning_rate: float = 0.01, prior_precision: float = 0.0001, prior_mean: float = 0.0, hessian_estimator: str = 'ggn_diag', damping: float = 1e-06) -> optax.GradientTransformation
¶
Diagonal BLR accepting standard loss gradients.
Drop-in replacement for optax.adam / optax.sgd: wraps
blr_diagonal and converts the loss
gradient into the log-likelihood gradient it expects,
\(\nabla_\theta \log p(\mathcal{D} \mid \theta) =
-\nabla_\theta L(\theta)\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
Step size rho in (0, 1]. |
0.01
|
prior_precision
|
float
|
Scalar prior precision s0 = 1/v0. |
0.0001
|
prior_mean
|
float
|
Scalar prior mean m0. |
0.0
|
hessian_estimator
|
str
|
One of |
'ggn_diag'
|
damping
|
float
|
Minimum precision after update. |
1e-06
|
Returns:
| Type | Description |
|---|---|
GradientTransformation
|
An |
Source code in src/optax_bayes/_src/wrappers.py
blr_diagonal(learning_rate: float = 0.01, prior_precision: float = 0.0001, prior_mean: float = 0.0, hessian_estimator: str = 'ggn_diag', damping: float = 1e-06) -> optax.GradientTransformation
¶
Diagonal-Gaussian BLR as an optax transform.
Implements the Bayesian Learning Rule (Khan & Rue, 2023) with a diagonal Gaussian variational family \(q(\theta) = \mathcal{N}(m, \operatorname{diag}(1/s))\). Optimizer state is stored in natural-parameter form (\(\eta = s \odot m\)) and each step applies
with \(g_t\) the log-likelihood gradient and \(h_t \le 0\) the diagonal
Hessian estimate. The emitted optax update is \(m_{t+1} - m_t\), so
applying updates keeps the user's params equal to the variational
mean. The state initialises its mean at the params passed to
init (standard optax drop-in semantics); prior_mean and
prior_precision anchor every update through \(\eta_0 = s_0 m_0\).
This API expects log-likelihood gradients. For standard loss
minimisation, use
blr_diagonal_for_loss instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
Step size rho in (0, 1]. |
0.01
|
prior_precision
|
float
|
Scalar prior precision s0 = 1/v0, broadcast across all parameters. |
0.0001
|
prior_mean
|
float
|
Scalar prior mean m0, broadcast across all parameters. |
0.0
|
hessian_estimator
|
str
|
One of |
'ggn_diag'
|
damping
|
float
|
Minimum precision after update (numerical safeguard). |
1e-06
|
Returns:
| Type | Description |
|---|---|
GradientTransformation
|
An |
Raises:
| Type | Description |
|---|---|
ValueError
|
If hessian_estimator is not a recognised string. |
Source code in src/optax_bayes/_src/diagonal.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
blr_with_schedule(schedule_fn: optax.Schedule, prior_precision: float = 0.0001, prior_mean: float = 0.0, hessian_estimator: str = 'ggn_diag', damping: float = 1e-06) -> optax.GradientTransformation
¶
Diagonal BLR with a time-varying learning rate.
Wraps blr_diagonal_for_loss with optax.inject_hyperparams
so that the step size rho follows an optax schedule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schedule_fn
|
Schedule
|
An |
required |
prior_precision
|
float
|
Scalar prior precision s0 = 1/v0. |
0.0001
|
prior_mean
|
float
|
Scalar prior mean m0. |
0.0
|
hessian_estimator
|
str
|
One of |
'ggn_diag'
|
damping
|
float
|
Minimum precision after update. |
1e-06
|
Returns:
| Type | Description |
|---|---|
GradientTransformation
|
An |
Source code in src/optax_bayes/_src/schedules.py
Posterior extraction & sampling¶
get_posterior_diagonal(state: BLRDiagState) -> tuple[optax.Params, optax.Params]
¶
Extract the approximate posterior from diagonal BLR state.
Returns the mean and variance of \(q(\theta) = \mathcal{N}(m, \operatorname{diag}(v))\) where
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
BLRDiagState
|
A |
required |
Returns:
| Type | Description |
|---|---|
tuple[Params, Params]
|
Tuple |
Source code in src/optax_bayes/_src/posterior.py
sample_posterior_diagonal(state: BLRDiagState, key: jax.Array) -> optax.Params
¶
Draw one reparameterised sample from the diagonal posterior.
Samples \(\theta \sim \mathcal{N}(m, \operatorname{diag}(1/s))\) via
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
BLRDiagState
|
A |
required |
key
|
Array
|
PRNG key. |
required |
Returns:
| Type | Description |
|---|---|
Params
|
Sampled parameter pytree. |
Source code in src/optax_bayes/_src/sampling.py
State¶
BLRDiagState
¶
Bases: NamedTuple
Natural-parameter state for \(q(\theta) = \mathcal{N}(m, \operatorname{diag}(1/s))\).
Attributes:
| Name | Type | Description |
|---|---|---|
precision |
Params
|
Precision pytree, \(s = 1/v\). Mirrors the parameter tree. |
nat_mean |
Params
|
Natural mean pytree, \(\eta = s \odot m\). Mirrors the parameter tree. |
count |
Array
|
Scalar step counter. |