Practice in Lab: Fit a positive heteroscedastic Gaussian likelihood →
Fit a positive heteroscedastic Gaussian likelihood
A regression model can learn both a conditional mean and input-dependent noise. Gaussian likelihood rewards a good mean but also penalizes excessive variance, unlike simply enlarging every interval. An unconstrained raw output is transformed into a positive variance before any logarithm or division. Very negative raw values test the numerical behavior at the floor. The learned quantity is aleatoric uncertainty under this likelihood assumption; it does not include model uncertainty or automatically provide calibrated prediction intervals.
Prepare offline
Choose Download entire course or Download entire category first. After completion, code and checks run locally on CPU. The independent example needs no account, remote service or API key.
import torch
from torch.nn import functional as F
raw = torch.tensor([-50., 0., 3.])
print(F.softplus(raw) + 1e-4)Review the evidence
Keep variance distinct from standard deviation. Evaluate log loss and interval coverage on held-out data, and inspect results by region; a good average likelihood can coexist with subgroup miscalibration.
Your task
Use variance=softplus(raw_variance)+floor and return mean(0.5*(log(2*pi)+log(variance)+(target-mean)**2/variance)). Preserve gradients through both mean and raw variance. Require aligned nonempty finite floating tensors and finite floor>0. The floor is part of the declared model; do not clamp a negative variance after taking its logarithm.
The explanation opens here once every check passes.