Skip to content

Losses API

Losses consume a `ModelOutput` and a labeled `Batch`, returning a scalar (or elementwise, when supported) tensor for optimization. Select a loss with its `LOSSES` registry key in YAML; direct construction is useful for tests and custom loops. See Losses for choosing a task-appropriate objective.

Contract

chimera_ml.losses.base.BaseLoss

Bases: ABC

Loss contract used by Trainer.

Implementations receive a ModelOutput and labeled Batch and return a differentiable tensor consumed by the optimizer.

Regression

`MSELoss` and `MAELoss` delegate to PyTorch reductions and expect predictions and targets with matching shapes.

chimera_ml.losses.regression.MSELoss

Bases: BaseLoss

Mean squared error for matching prediction and target shapes.

Parameters:

Name Type Description Default
reduction str

PyTorch reduction mode: mean, sum, or none.

'mean'

chimera_ml.losses.regression.mse_loss

mse_loss(**params)

chimera_ml.losses.regression.MAELoss

Bases: BaseLoss

Mean absolute error for matching prediction and target shapes.

Parameters:

Name Type Description Default
reduction str

PyTorch reduction mode: mean, sum, or none.

'mean'

chimera_ml.losses.regression.mae_loss

mae_loss(**params)

Classification

Cross entropy and focal loss consume class-index targets. Focal loss additionally supports focusing, alpha weighting, reduction, and label smoothing.

chimera_ml.losses.classification.CrossEntropyLoss

Bases: BaseLoss

Cross-entropy loss for class logits and integer class targets.

Parameters:

Name Type Description Default
label_smoothing float

Amount of uniform label smoothing passed to PyTorch.

0.0

chimera_ml.losses.classification.cross_entropy_loss

cross_entropy_loss(**params)

chimera_ml.losses.focal.FocalLoss

Bases: BaseLoss

Multi-class focal loss for logits and integer class targets.

Parameters:

Name Type Description Default
gamma float

Focusing exponent; larger values down-weight easy examples.

2.0
alpha float | list[float] | None

Optional scalar or per-class weighting factor.

None
reduction str

mean, sum, or none.

'mean'
label_smoothing float

Optional smoothing applied to the selected class probability.

0.0

chimera_ml.losses.focal.focal_loss

focal_loss(**params)

Multilabel and agreement objectives

`BCEWithLogitsLoss` expects multi-hot targets. `CCCLoss` optimizes one minus Lin's concordance correlation coefficient across flattened output dimensions.

chimera_ml.losses.multilabel.BCEWithLogitsLoss

Bases: BaseLoss

Binary cross-entropy loss for multi-label logits and targets.

Parameters:

Name Type Description Default
pos_weight Tensor | None

Optional positive-class weighting tensor.

None
reduction str

PyTorch reduction mode, typically mean, sum, or none.

'mean'

chimera_ml.losses.multilabel.bce_with_logits_loss

bce_with_logits_loss(**params)

chimera_ml.losses.ccc.CCCLoss

Bases: BaseLoss

Concordance Correlation Coefficient loss for regression.

The loss is 1 - mean(CCC) across output dimensions.

output.preds and batch.targets must have the same shape: (B,), (B, D), or (B, ...). Inputs are flattened to (B, Dflat).

chimera_ml.losses.ccc.ccc_loss

ccc_loss(**params)