sage.factory.loss_adapters
Loss adapters — a uniform protocol so SageVanillaTraining can combine an
arbitrary main loss with auxiliary losses under a
GradientNormBalancer.
Different losses have different call signatures (BCEWithPEsigmaLoss takes a
(ranking, point_estimates) tuple and sliced targets; ConsistencyNLLLoss
takes per-detector tensors + a mask read from the batch context). An adapter
wraps a loss so the trainer can call every loss the same way —
adapter(out, targets, ctx) -> stacked components — and declares which
component is the primary (reference / BCE) term and which are the auxiliary
terms to balance.
Classes
Base loss adapter. |
|
Wraps |
|
Wraps |
Module Contents
- class LossAdapter[source]
Base loss adapter.
Subclasses implement
components()(the underlying loss call, returning a stacked component tensor whose[0]is the loss’s own total) and set:- primary_indexint or None
Index of the primary (reference) component, e.g. BCE.
Noneif this adapter contributes only auxiliary terms.- aux_indicestuple[int]
Indices of the auxiliary components to balance against the primary.
- class MergedLossAdapter(loss)[source]
Bases:
LossAdapterWraps
BCEWithPEsigmaLoss(classification + merged heteroscedastic PE).Returns
[total, bce, pe_reg, coupling]; the primary is BCE and the aux terms arepe_regandcoupling.targets[:, :mw]is the merged-width slice (PE params + class column), matching the loss’s existing contract.
- class ConsistencyLossAdapter(loss)[source]
Bases:
LossAdapterWraps
ConsistencyNLLLoss(per-detector tc / mchirp NLL).Returns
[total, cons_tc, cons_mc]— both are auxiliary terms (no primary). Reads the per-detector supervisionper_det_maskfromctx(populated by the masking callback) and slices the per-detector tc / mc targets from the appended target columns.tcis window-normalised (/ tc_scale) to match the model’s window-normalisedmu_tc.