Gaussian Loss Functions¶
← Base Classes | Beta-NLL →
Gaussian (or Normal) loss functions are the workhorses of regression. They range from the standard Mean Squared Error (MSE) to sophisticated multivariate models that capture complex correlations between targets.
Mathematical Background¶
In the probabilistic framework, regression is viewed as maximizing the likelihood of the data under a Gaussian noise model \(p(y \mid x) = \mathcal{N}(\mu(x), \sigma^2(x))\). Minimising the Negative Log-Likelihood (NLL) is equivalent to:
- The Residual Term: \((y - \mu)^2 / 2\sigma^2\) penalises predictions far from the truth, scaled by the uncertainty.
- The Penalty Term: \(\frac{1}{2}\log(2\pi\sigma^2)\) prevents the model from simply predicting infinite variance to "cheat" the residual term.
Comparison of Gaussian Losses¶
| Loss | Covariance Type | Outputs | API Reference | Best For |
|---|---|---|---|---|
WeightedMSELoss |
Fixed (\(\sigma^2=1\)) | \(\mu\) | WeightedMSELoss | Homoscedastic, clean data |
GaussianNLLLoss |
Diagonal | \((\mu, \log\sigma^2)\) | GaussianNLLLoss | Heteroscedastic, independent targets |
BetaNLLLoss |
Diagonal | \((\mu, \log\sigma^2)\) | BetaNLLLoss | Same head as NLL; detached variance rescaling (β-NLL) |
FaithfulGaussianLoss |
Diagonal | \((\mu, \log\sigma^2)\) | FaithfulGaussianLoss | MSE on \(\mu\) + NLL on variance with detach(\(\mu\)) in residual |
GaussianWassersteinBoundLoss |
Configurable | \(\mu\), cov / Cholesky / root | GaussianWassersteinBoundLoss | Supervise mean + covariance vs labels or pseudo-labels |
MultivariateGaussianLoss |
Full | \((\mu, \Sigma)\) | MultivariateGaussianLoss | Correlated multi-output (small \(k\)) |
LowRankGaussianLoss |
Low-Rank + Diag | \((\mu, W, d)\) | LowRankGaussianLoss | Correlated multi-output (large \(k\)) |
1. Univariate: GaussianNLLLoss¶
Used for standard regression where you want to estimate per-sample uncertainty.
from torchregress.losses import GaussianNLLLoss
# Model must output [batch, 2] -> [mean, log_var]
loss_fn = GaussianNLLLoss()
loss = loss_fn(y_pred, y_true)
Numerical Stability
Always predict log-variance (\(s\)) rather than raw variance (\(\sigma^2\)). This ensures positivity (\(e^s > 0\)) and provides a more stable loss landscape for optimization.
1b. Faithful heteroscedastic: FaithfulGaussianLoss¶
Joint Gaussian NLL couples gradients from the variance head into the mean through the residual \((y-\mu)^2/\sigma^2\). FaithfulGaussianLoss adds a direct MSE term on the mean and uses stop-gradient on \(\mu\) inside the NLL residual so variance calibration does not distort point prediction.
from torchregress.losses import FaithfulGaussianLoss
loss_fn = FaithfulGaussianLoss(mean_weight=1.0, variance_weight=1.0)
loss = loss_fn((mean, log_var), y_true)
Compare with BetaNLLLoss: β-NLL keeps a single joint NLL and rescales it with a detached variance; faithful loss explicitly splits mean vs variance terms.
2. Multivariate: Full Covariance (MultivariateGaussianLoss)¶
When targets are correlated (e.g., predicting \(x, y, z\) coordinates), use MultivariateGaussianLoss.
Limitations
- Computational Scaling: Solvers and determinants for a \(K \times K\) covariance matrix scale as \(\mathcal{O}(K^3)\) with target dimension, which makes full covariance impractical for high-dimensional outputs (\(K > 10\)).
- PSD Violations: Cholesky factors must be regularized/clamped on the diagonal to prevent non-positive semi-definite covariance matrices during optimization.
- FaithfulGaussianLoss tradeoff: The
stop_gradienton \(\mu\) inside the NLL residual decouples the variance head's gradients from the mean head — preventing variance miscalibration from distorting point predictions, but also meaning the mean head does not receive curvature information about heteroscedasticity. This can slow convergence on datasets where mean and variance are strongly coupled. - LowRankGaussianLoss rank selection: The rank \(r\) is a fixed hyperparameter. Start with \(r = \lfloor K/3 \rfloor\) and tune on validation NLL. If \(r\) is too small, the diagonal component must absorb residual correlation, inflating marginal variances. If too large, parameter count approaches the full-covariance case and computational benefits vanish.
from torchregress.losses import MultivariateGaussianLoss
# Requires full covariance matrix Σ (batch, k, k)
loss_fn = MultivariateGaussianLoss()
loss = loss_fn(y_mu, y_true, covariance_matrix)
3. Multivariate: Low-Rank Covariance (LowRankGaussianLoss)¶
For high-dimensional targets, a full covariance matrix has \(O(k^2)\) parameters. Low-Rank approximation reduces this to \(O(k \cdot r)\), where \(r\) is the rank.
from torchregress.losses import LowRankGaussianLoss
# W is factor matrix (batch, k, r), d is diagonal (batch, k)
loss_fn = LowRankGaussianLoss()
loss = loss_fn(mu, y_true, W, d)
Limitations¶
- Computational scaling of full covariance:
MultivariateGaussianLossrequires \(\mathcal{O}(K^3)\) operations per sample for determinants and solves, making it impractical beyond \(K \approx 10\). UseLowRankGaussianLosswith rank \(r = \lfloor K/3 \rfloor\) for higher-dimensional targets. - PSD violations: Cholesky factors must be regularised on the diagonal to prevent non-positive-semidefinite covariance matrices. Always use a minimum variance floor (
min_varianceor jitter \(\ge 10^{-6}\)). - LowRank rank selection: If \(r\) is too small, the diagonal component absorbs residual correlation, inflating marginal variances. If too large, parameter count approaches the full-covariance case. Tune \(r\) on validation NLL.
- Gaussian assumption: The NLL is only a proper scoring rule when \(Y \mid X\) is truly Gaussian. For heavy-tailed, multimodal, or bounded targets, consider robust alternatives (Robust losses), MDN, or Normalizing flows.
- Variance collapse: Without a minimum variance floor, the model can learn \(\sigma^2 \to 0\) to drive the NLL to \(-\infty\), especially on small datasets. Always set
min_varianceand monitor predicted variance during training.
Best Practices¶
Stable Training
- Initialise log-var to zero: This starts the model with \(\sigma^2 \approx 1\).
- Use a small
min_variance: Prevents the loss from blowing up if the model becomes too confident (default \(10^{-6}\)). - Weight Decay: Apply higher weight decay to the uncertainty head to prevent "explaining away" all error as aleatoric noise.
References¶
| # | Reference |
|---|---|
| 1 | Nix & Weigend. "Estimating the Mean and Variance of the Target Probability Distribution." ICNN, 1994. |
| 2 | Kendall & Gal. "What Uncertainties Do We Need in Bayesian Deep Learning?" NeurIPS, 2017. |
| 3 | Skafte et al. "Reliable Training and Estimation of Variance Networks." NeurIPS, 2019. |
Next steps¶
Continue with the Gaussian family:
- Beta-NLL — stabilised heteroscedastic training via detached variance rescaling
- Faithful Gaussian — decouple mean and variance gradients for cleaner calibration
- Gaussian Wasserstein bound — supervise covariance directly against labels or pseudo-labels
Or branch out:
- Robust losses — when Gaussian assumptions fail due to outliers
- Ensemble methods — decompose into aleatoric + epistemic
- Multivariate example — correlated multi-output in practice
Recommendations¶
- Start simple: Begin with
WeightedMSELoss(homoscedastic) orGaussianNLLLoss(heteroscedastic). Move toFaithfulGaussianLossonly if you observe variance inflation distorting mean predictions. - Low-rank for high-dimensional targets: For \(K > 5\) correlated targets, use
LowRankGaussianLoss. For \(K \le 5\),MultivariateGaussianLossis simpler and more interpretable. - Stable training recipe: Initialise
log_var\(\approx 0\) (variance \(\approx 1\)), setmin_variance = 1e-6, and apply higher weight decay to the variance head (2–5× the mean head). - Monitor with CRPS: Track
crps_gaussianalongside NLL during training. CRPS is in target units and less sensitive to tail events than NLL. See Distribution metrics. - Variance calibration: After training, validate with a PIT histogram or calibration error (ECE). If variance is systematically over- or under-estimated, apply post-hoc Variance Temperature Scaling. See FaithfulGaussianLoss demo.
- For coverage guarantees: Gaussian likelihood losses provide no coverage guarantees. For finite-sample prediction intervals, wrap with SplitConformal or CQR.