Loss Decorrelation#
decorrelation
#
| CLASS | DESCRIPTION |
|---|---|
DecorrelationLoss |
Decorrelation loss for learning independent latent representations. |
| FUNCTION | DESCRIPTION |
|---|---|
decorrelation_loss |
Compute decorrelation loss for learning independent representations. |
Classes#
DecorrelationLoss(method: Literal['correlation', 'covariance', 'total_correlation'] = 'correlation', reduce: Literal['sum', 'mean'] = 'mean', sign: float = 1.0, eps: float = torch.finfo(torch.float32).eps, context_prefix: str | None = None, **kwargs)
#
Bases: Loss
Decorrelation loss for learning independent latent representations.
Penalizes statistical dependencies between latent dimensions to encourage learning of disentangled or decorrelated features.
Supports three decorrelation methods:
- "correlation": Penalizes off-diagonal correlation coefficients (default)
- "covariance": Penalizes off-diagonal covariance elements (faster)
- "total_correlation": Information-theoretic measure of dependence (advanced)
The correlation loss computes: \(L = \sum_{i \neq j} \rho_{ij}^2\), where \(\rho\) is the correlation matrix.
The covariance loss computes: \(L = \sum_{i \neq j} \text{Cov}(z_i, z_j)^2\)
The total correlation loss approximates: \(\text{TC}(Z) = \text{KL}(q(z) \| \prod_i q(z_i))\)
| PARAMETER | DESCRIPTION |
|---|---|
method
|
Decorrelation method to use.
TYPE:
|
reduce
|
Reduction method for off-diagonal elements.
TYPE:
|
sign
|
Loss sign multiplier. Use 1.0 to minimize correlation (typical).
TYPE:
|
eps
|
Small constant for numerical stability in variance/correlation computation.
TYPE:
|
**kwargs
|
Additional keyword arguments for the loss function.
DEFAULT:
|
Examples:
Basic usage with correlation penalty:
>>> from spectre.loss import DecorrelationLoss
>>> import torch
>>>
>>> loss_fn = DecorrelationLoss(method="correlation", reduce="mean")
>>> Z = torch.randn(100, 5) # 100 samples, 5 latent dimensions
>>> loss = loss_fn({"Z": Z})
With sample weights:
Notes
- For highly correlated inputs, use
method="correlation" - Total correlation requires larger sample sizes for accurate estimation
| METHOD | DESCRIPTION |
|---|---|
forward |
Compute decorrelation loss. |
Source code in spectre/loss/decorrelation.py
Functions#
forward(context: dict, **kwargs) -> torch.Tensor
#
Compute decorrelation loss.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
Context dictionary containing:
TYPE:
|
**kwargs
|
Additional keyword arguments).
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Decorrelation loss. |
Source code in spectre/loss/decorrelation.py
Functions#
decorrelation_loss(Z: torch.Tensor, weights: torch.Tensor | None = None, method: Literal['correlation', 'covariance', 'total_correlation'] = 'correlation', reduce: Literal['sum', 'mean'] = 'mean', sign: float = 1.0, eps: float = torch.finfo(torch.float32).eps) -> torch.Tensor
#
Compute decorrelation loss for learning independent representations.
Functional interface for DecorrelationLoss. Penalizes statistical dependencies between latent dimensions.
| PARAMETER | DESCRIPTION |
|---|---|
Z
|
Latent representation or features to decorrelate.
TYPE:
|
weights
|
Sample weights for weighted computation. If None, uniform weights used.
TYPE:
|
method
|
Decorrelation method.
TYPE:
|
reduce
|
Reduction method for off-diagonal elements.
TYPE:
|
sign
|
Loss sign multiplier.
TYPE:
|
eps
|
Numerical stability constant.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Decorrelation loss. |
Examples:
>>> import torch
>>> from spectre.loss import decorrelation_loss
>>>
>>> Z = torch.randn(100, 5)
>>> loss = decorrelation_loss(Z, method="correlation")
>>>
>>> # With weights
>>> weights = torch.rand(100)
>>> loss = decorrelation_loss(Z, weights=weights, method="covariance")