ELBOLoss#
- class torch_uncertainty.losses.ELBOLoss(model, inner_loss, kl_weight, num_samples, dist_family=None)[source]#
The (negative) Evidence Lower Bound (ELBO) loss for Bayesian Neural Networks.
Combines an inner data-fit loss (e.g., cross-entropy or a distribution NLL) with the Kullback-Leibler regulariser \(\mathrm{KL}[q_\phi(\mathbf{w}) \| p(\mathbf{w})]\), estimated by Monte Carlo over
num_samplesweight samples:\[\mathcal{L}_{\text{ELBO}} = \frac{1}{S} \sum_{s=1}^{S} \mathcal{L}_\text{inner}\!\left(f_{\mathbf{w}^{(s)}}(\mathbf{x}), y\right) + \beta_\text{KL} \cdot \mathrm{KL}\!\left[ q_\phi(\mathbf{w}) \;\|\; p(\mathbf{w}) \right],\]with \(\mathbf{w}^{(s)} \sim q_\phi\). The KL weight \(\beta_\text{KL}\) is typically set to the inverse of the number of training points (or a manually annealed schedule).
- Parameters:
model (
Module|None) – The Bayesian Neural Network to compute the loss for.inner_loss (
Module) – The data-fit loss to use during training.kl_weight (
float) – The weight \(\beta_\text{KL}\) of the KL-divergence term.num_samples (
int) – The number of weight samples \(S\) used to estimate the expectation.dist_family (
str|None) – The distribution family to use for the output of the model.Nonemeans point-wise prediction. Defaults toNone.
Note
Set
modeltoNonewhen usingELBOLossinside aClassificationRoutine— it will be filled in automatically.