Losses
Objectives for training variational Bayesian models.
Variational Loss
bensemble.losses.VariationalLoss
VariationalLoss(likelihood_model: Module, alpha: float = 1.0, num_batches: int = 1, kl_weight: float = 1.0)
Bases: Module
Variational loss supporting ELBO and Rényi alpha-divergence.
Initializes the VariationalLoss module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
likelihood_model | Module | Module computing negative log-likelihood p(y|x). | required |
alpha | float | Rényi alpha parameter. When alpha=1.0, standard ELBO is used. Defaults to 1.0. | 1.0 |
num_batches | int | Total number of training batches in an epoch (for KL mini-batch scaling). Defaults to 1. | 1 |
kl_weight | float | Additional scaling factor for KL divergence term. Defaults to 1.0. | 1.0 |
Source code in bensemble/losses.py
forward
Computes variational objective loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds | Tensor | Predictions tensor of shape (K, batch_size, ...) or (batch_size, ...). | required |
target | Tensor | Target tensor of shape (batch_size, ...). | required |
kl_divergence | Tensor | Total KL divergence of model parameters. | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: Scalar loss value. |
Source code in bensemble/losses.py
Gaussian Likelihood
bensemble.losses.GaussianLikelihood
Bases: Module
Gaussian Likelihood with learnable homoscedastic uncertainty.
Learns a global standard deviation (sigma) for data noise and computes negative log-likelihood via Gaussian NLL.
Initializes the GaussianLikelihood layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
init_log_sigma | float | Initial value for log standard deviation. Defaults to -2.0. | -2.0 |
Source code in bensemble/losses.py
forward
Computes Gaussian negative log-likelihood loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds | Tensor | Predicted tensor of shape (*). | required |
target | Tensor | Target tensor of shape (*). | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: Element-wise negative log-likelihood tensor matching target shape. |