class
GaussianNLLLoss
extends
ModuleGaussianNLLLoss(full: bool = False, eps: float = 1e-06, reduction: str = 'mean')Gaussian negative log-likelihood loss for heteroscedastic regression.
Models the negative log-likelihood of a Gaussian distribution whose
mean and variance are both predicted by the network. For
predicted mean (x), target , and predicted
variance (var):
When full=True, the constant term is
also included. The variance is clamped below by eps to prevent
division by zero.
Parameters
fullbool= FalseIf
True, adds the constant.
Default False.epsfloat= 1e-06Minimum value for the variance. Default
1e-6.reductionstr= 'mean''none' | 'mean' (default) | 'sum'.Attributes
fullboolWhether the full Gaussian constant is included.
epsfloatVariance lower bound.
reductionstrThe reduction mode.
Notes
- x (mean) : — predicted means.
- target : — observed values.
- var : or — predicted variances (must be positive).
- Output : scalar for
'mean'/'sum'; for'none'.
- This loss is also called the heteroscedastic regression loss because the noise level is input-dependent (predicted by the model rather than fixed).
- Minimising this loss simultaneously trains the model to produce accurate predictions (via the residual term) and well-calibrated uncertainty estimates (via the log-variance term).
Examples
Predicting mean and variance simultaneously:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.GaussianNLLLoss()
>>> mean = lucid.tensor([1.5, 2.0, 0.5])
>>> var = lucid.tensor([0.5, 1.0, 0.2])
>>> y = lucid.tensor([1.0, 2.5, 0.3])
>>> loss = criterion(mean, y, var)
With full Gaussian constant:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.GaussianNLLLoss(full=True, eps=1e-4)
>>> mean = lucid.tensor([[0.0, 1.0], [2.0, 3.0]])
>>> var = lucid.tensor([[0.1, 0.2], [0.3, 0.4]])
>>> y = lucid.tensor([[0.1, 0.8], [2.2, 2.8]])
>>> loss = criterion(mean, y, var)Methods (3)
dunder
__init__
→None__init__(full: bool = False, eps: float = 1e-06, reduction: str = 'mean')Initialise the GaussianNLLLoss module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor, target: Tensor, var: Tensor)Compute the loss between predictions and targets.
Parameters
xTensorInput tensor.
targetTensorInput tensor.
varTensorInput tensor.
Returns
TensorScalar loss (or unreduced tensor depending on reduction).
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.