BCEWithPEsigmaLoss
BCEWithPEsigmaLoss is the production
loss used in the O3b run. It extends the simpler regression loss with
heteroscedastic uncertainty estimation: the network predicts both the mean
\(\mu\) and the log-variance \(\log \sigma^2\) for each parameter, and
the loss penalises the negative log-likelihood under a Gaussian model.
This is the loss paired with the
MSCNN1D_2DResNetCBAM_Heteroscedastic network.
from sage.architecture.custom_losses import BCEWithPEsigmaLoss
loss_function = BCEWithPEsigmaLoss(
regression_weight=0.005,
coupling_weight=0.005,
)
Three-term formula
Classification loss — standard binary cross-entropy:
Heteroscedastic NLL regression loss — Gaussian negative log-likelihood:
The second sum is a variance regulariser (weight λ_v = 1e-3) that penalises
sigma explosion — without it the network can trivially minimise the NLL by making
\(\sigma\) very large.
The confidence weighting uses \(p_i^2 = \sigma(\hat{y}_i)^2\) (squared
probability), making the curriculum stricter than in BCEWithPEregLoss: only
high-confidence signal predictions receive strong regression gradient.
Coupling loss — prevents the network from escaping the classification task by exploiting the regression uncertainty head:
where \(\bar{\sigma}_i = \sqrt{\text{mean}(\sigma_i^2)}\) is the average predicted uncertainty across all PE targets for sample \(i\). This term penalises any combination of high predicted uncertainty and high predicted signal probability, forcing the network to either be uncertain (low ranking stat) or confident (low sigma). It prevents the degenerate solution where the network classifies everything as a signal while predicting large uncertainty.
Parameters
Parameter |
Description |
|---|---|
|
Weight \(\lambda_r\) for the heteroscedastic NLL term. Very small values (0.005) are typical in production — the NLL gradient is much larger than the BCE gradient for well-separated classes. |
|
Weight \(\lambda_c\) for the coupling term. Values in |
|
Small constant added to \(\exp(\log \sigma^2)\) for numerical stability. |
Outputs
loss = loss_function(model_output, targets)
# loss: torch.Tensor, shape (num_pe + 2,)
# loss[0] = total_loss
# loss[1] = bce_loss
# loss[2] = reg_loss (heteroscedastic NLL + variance regulariser)
# loss[3] = coupling_loss
loss_function.num_components equals len(cfg.do_point_estimate) + 2.
The log-variance is clamped to [-10, 6] before exponentiation to prevent
numerical overflow (exp(6) ≈ 400) and underflow (exp(-10) ≈ 5e-5).
When to use
Use BCEWithPEsigmaLoss when:
You want per-prediction uncertainty estimates for downstream ranking and parameter recovery diagnostics.
You are using the heteroscedastic network
MSCNN1D_2DResNetCBAM_Heteroscedastic.You are doing a production run aimed at matched-filter-competitive performance.
For a quick experiment or ablation where uncertainty is not needed, prefer BCEWithPEregLoss with its simpler tuning surface.