Loss Elbo Gmm#
elbo_gmm
#
| CLASS | DESCRIPTION |
|---|---|
ELBOGMMLoss |
ELBO loss for Variational Autoencoder with Gaussian Mixture Model prior and |
| FUNCTION | DESCRIPTION |
|---|---|
elbo_gmm_loss |
Compute ELBO loss for GMM-VAE with categorical latent variables (functional API). |
Classes#
ELBOGMMLoss(n_mc_samples: int = 1, reduce: Literal['sum', 'mean'] = 'mean', sign: float = 1.0, eps: float = torch.finfo(torch.float32).eps, context_prefix: str | None = None, **kwargs)
#
Bases: Loss
ELBO loss for Variational Autoencoder with Gaussian Mixture Model prior and categorical latent variables.
This loss function combines reconstruction loss with KL divergence terms for both continuous and categorical latent variables, enabling clustering in the latent space via a Gaussian mixture prior.
The loss is defined as:
\(L = \mathbb{E}_{q(z,c|x)}[- \log p(x|z)] + D_{KL}(q(z|x,c) \| p(z|c)) + D_{KL}(q(c|x) \| p(c))\)
where:
- \(p(x|z)\) is the reconstruction likelihood (Gaussian)
- \(q(z|x,c)\) is the posterior over continuous latent given component
- \(p(z|c)\) is the GMM component-conditional prior
- \(q(c|x)\) is the posterior over component assignments
- \(p(c)\) is the categorical prior (mixture weights)
The KL divergence for continuous latent \(z\) is computed via Monte Carlo estimation as there is no closed form for \(D_{KL}(N(\mu, \sigma^2) \| GMM)\).
| PARAMETER | DESCRIPTION |
|---|---|
n_mc_samples
|
Number of Monte Carlo samples for KL divergence estimation. More samples provide better gradient estimates but increase computation.
TYPE:
|
reduce
|
Reduction operation for loss aggregation.
TYPE:
|
sign
|
Loss sign multiplier.
TYPE:
|
eps
|
Regularization term for numerical stability in log computations.
TYPE:
|
**kwargs
|
Additional keyword arguments.
DEFAULT:
|
Examples:
>>> import torch
>>> from spectre.loss import ELBOGMMCategoricalLoss
>>>
>>> loss_fn = ELBOGMMCategoricalLoss(n_mc_samples=1, reduce="mean")
>>>
>>> # Build context dictionary with required keys
>>> context = {
... "X": torch.randn(32, 784), # Input data
... "Y": torch.randn(32, 784), # Reconstructed data
... "mean": torch.randn(32, 2), # Encoder mean
... "logvar": torch.randn(32, 2), # Encoder log-variance
... "z_sample": torch.randn(32, 2), # Latent sample
... "categorical_logits": torch.randn(32, 3), # Component logits
... "gmm_means": torch.randn(3, 2), # GMM component means
... "gmm_covariances": torch.randn(3, 2), # GMM covariances (diag)
... "gmm_weights": torch.softmax(torch.randn(3), dim=0), # Mixing weights
... "covariance_type": "diag",
... }
>>>
>>> loss = loss_fn(context)
>>> loss.shape
torch.Size([])
Notes
- Supports all covariance types: "diag", "spherical", "full", "tied"
- KL divergence for categorical is computed in closed form
- KL divergence for continuous latent uses Monte Carlo estimation
- Numerical stability via log-sum-exp trick for GMM probabilities
See Also
ELBOGaussianLoss: Standard VAE ELBO loss
| METHOD | DESCRIPTION |
|---|---|
forward |
Compute ELBO loss with GMM prior and categorical latent variables. |
Source code in spectre/loss/elbo_gmm.py
Functions#
forward(context: dict, **kwargs) -> torch.Tensor
#
Compute ELBO loss with GMM prior and categorical latent variables.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
Dictionary containing required keys:
TYPE:
|
kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar ELBO loss value. |
Source code in spectre/loss/elbo_gmm.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
Functions#
elbo_gmm_loss(X: torch.Tensor, Y: torch.Tensor, mean: torch.Tensor, logvar: torch.Tensor, z_sample: torch.Tensor, categorical_logits: torch.Tensor, gmm_means: torch.Tensor, gmm_covariances: torch.Tensor, gmm_weights: torch.Tensor, covariance_type: Literal['diag', 'spherical', 'full', 'tied'], weights: torch.Tensor | None = None, n_mc_samples: int = 1, reduce: str = 'mean', sign: float = 1.0, eps: float = torch.finfo(torch.float32).eps) -> torch.Tensor
#
Compute ELBO loss for GMM-VAE with categorical latent variables (functional API).
The loss combines reconstruction error with two KL divergence terms:
- KL divergence for continuous latent: \(D_{KL}(q(z|x,c) \| p(z|c))\)
- KL divergence for categorical: \(D_{KL}(q(c|x) \| p(c))\)
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
Y
|
Reconstructed data of shape (n_samples, in_features).
TYPE:
|
mean
|
Encoder mean of shape (n_samples, z_dim).
TYPE:
|
logvar
|
Encoder log-variance of shape (n_samples, z_dim).
TYPE:
|
z_sample
|
Latent samples of shape (n_samples, z_dim).
TYPE:
|
categorical_logits
|
Component logits of shape (n_samples, n_components).
TYPE:
|
gmm_means
|
GMM component means of shape (n_components, z_dim).
TYPE:
|
gmm_covariances
|
GMM covariances (shape depends on covariance_type).
TYPE:
|
gmm_weights
|
GMM mixing weights of shape (n_components,).
TYPE:
|
covariance_type
|
Type of covariance parameterization.
TYPE:
|
weights
|
Sample weights of shape (n_samples,).
TYPE:
|
n_mc_samples
|
Number of Monte Carlo samples for KL estimation.
TYPE:
|
reduce
|
Reduction operation: "sum" or "mean".
TYPE:
|
sign
|
Loss sign multiplier.
TYPE:
|
eps
|
Regularization for numerical stability.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar ELBO loss value. |
Source code in spectre/loss/elbo_gmm.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |