Loss Elbo#
elbo
#
| CLASS | DESCRIPTION |
|---|---|
ELBOGaussianLoss |
Loss for Gaussian ELBO (Evidence Lower BOund). |
| FUNCTION | DESCRIPTION |
|---|---|
elbo_gaussian_loss |
Compute Gaussian ELBO loss for variational autoencoders. |
Classes#
ELBOGaussianLoss(reduce: Literal['sum', 'mean'] = 'mean', sign: float = 1.0, context_prefix: str | None = None, **kwargs)
#
Bases: Loss
Loss for Gaussian ELBO (Evidence Lower BOund).
Computes the negative ELBO for variational autoencoders, combining Kullback-Leibler (KL) divergence between posterior and prior with reconstruction loss.
The loss function is defined as \(L = \| X - Z \|^2 + \frac{1}{2} \sum_k (\exp(\log \sigma^2_k) + \mu_k^2 - 1 - \log \sigma^2_k)\), where:
- \(\| X - Z \|^2\) is the MSE reconstruction loss
- The sum term is the KL divergence \(\mathrm{KL}(N(\mu, \sigma^2) \| N(0, I))\)
- \(\mu\) is mean, \(\sigma^2\) is variance
| PARAMETER | DESCRIPTION |
|---|---|
reduce
|
Reduction operation for KL divergence.
TYPE:
|
sign
|
Loss sign multiplier, by default 1.0.
TYPE:
|
**kwargs
|
Additional keyword arguments.
DEFAULT:
|
| METHOD | DESCRIPTION |
|---|---|
forward |
Compute Gaussian ELBO loss. |
Source code in spectre/loss/elbo.py
Functions#
forward(context: dict, **kwargs) -> torch.Tensor
#
Compute Gaussian ELBO loss.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
Dictionary containing the input data and model outputs.
TYPE:
|
kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The computed ELBO loss. |
Source code in spectre/loss/elbo.py
Functions#
elbo_gaussian_loss(X: torch.Tensor, Y: torch.Tensor, mean: torch.Tensor, logvar: torch.Tensor, weights: Optional[torch.Tensor] = None, reduce: str = 'mean', sign: float = 1.0) -> torch.Tensor
#
Compute Gaussian ELBO loss for variational autoencoders.
The ELBO loss combines KL divergence between posterior q(z|x) and prior p(z) with reconstruction loss between input and output.
The loss function is defined as \(L = \| X - Y \|^2 + \frac{1}{2} \sum_k (\exp(\log \sigma^2_k) + \mu_k^2 - 1 - \log \sigma^2_k)\), where:
- \(\| X - Y \|^2\) is the MSE reconstruction loss
- The sum term is the KL divergence \(\mathrm{KL}(N(\mu, \sigma^2) \| N(0, I))\)
- \(\mu\) is mean, \(\sigma^2\) is variance
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data tensor.
TYPE:
|
Y
|
Reconstructed data tensor (same shape as X).
TYPE:
|
mean
|
Mean of posterior Gaussian distribution q(z|x).
TYPE:
|
logvar
|
Log variance of posterior Gaussian distribution (same shape as mean).
TYPE:
|
weights
|
Sample weights of shape (n_batches,) or (n_batches, 1).
TYPE:
|
reduce
|
Reduction operation for KL divergence. Options: "sum", "mean".
TYPE:
|
sign
|
Loss sign multiplier.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
ELBO loss (KL divergence + MSE reconstruction loss). |