Skip to content

Metrics API

Metrics are stateful epoch accumulators. `Trainer` calls `reset`, feeds each model output and batch to `update`, then calls `compute` to obtain scalar log values. Configure metric factories through the `METRICS` registry. See Metrics for configuration examples.

Contract

chimera_ml.metrics.base.BaseMetric

Bases: ABC

Stateful metric extension contract.

Trainer resets a metric at the start of an epoch, calls update for each labeled batch, and merges the mapping returned by compute into logs. Implementations should keep only epoch-local state.

reset abstractmethod

reset()

Clear all accumulated epoch state.

update abstractmethod

update(output, batch)

Accumulate predictions and targets from one batch.

Parameters:

Name Type Description Default
output ModelOutput

Model predictions for the batch.

required
batch Batch

Inputs and labeled targets for the batch.

required

compute abstractmethod

compute()

Return metric names and values for the accumulated epoch.

Regression metrics

Regression metrics flatten outputs to `(N, D)` and support uniform or raw multi-output aggregation. R² additionally supports variance-weighted output.

chimera_ml.metrics.regression_metric.RegressionBaseMetric dataclass

Bases: BaseMetric

Base class for regression metrics.

Accumulates y_true and y_pred during epoch, then computes metric at epoch end. Flattens tensors to (N, D) where D is output dimension.

chimera_ml.metrics.regression_metric.MAEMetric dataclass

Bases: RegressionBaseMetric

Mean absolute error with raw or uniform multi-output aggregation.

chimera_ml.metrics.regression_metric.mae_metric

mae_metric(**params)

chimera_ml.metrics.regression_metric.MSEMetric dataclass

Bases: RegressionBaseMetric

Mean squared error with raw or uniform multi-output aggregation.

chimera_ml.metrics.regression_metric.mse_metric

mse_metric(**params)

chimera_ml.metrics.regression_metric.RMSEMetric dataclass

Bases: RegressionBaseMetric

Root mean squared error with raw or uniform aggregation.

chimera_ml.metrics.regression_metric.rmse_metric

rmse_metric(**params)

chimera_ml.metrics.regression_metric.R2Metric dataclass

Bases: RegressionBaseMetric

Coefficient of determination with optional variance weighting.

chimera_ml.metrics.regression_metric.r2_metric

r2_metric(**params)

Classification metrics

`PRFMetric` reports precision, recall, and F1 with micro, macro, or weighted averaging. `ConfusionMatrixMetric` stores the class matrix and exposes its accuracy summary through `compute` and the matrix through `value`.

chimera_ml.metrics.prf_metric.PRFMetric dataclass

Bases: BaseMetric

Compute precision, recall, and F1 from accumulated class predictions.

Attributes:

Name Type Description
average Literal['micro', 'macro', 'weighted']

micro, macro, or weighted aggregation.

zero_division int

Value used when a class has no positive denominator.

The compute method returns keys prefixed with the selected average, for example macro_precision, macro_recall, and macro_f1.

chimera_ml.metrics.prf_metric.prf_micro_metric

prf_micro_metric(**params)

chimera_ml.metrics.prf_metric.prf_macro_metric

prf_macro_metric(**params)

chimera_ml.metrics.prf_metric.prf_weighted_metric

prf_weighted_metric(**params)

chimera_ml.metrics.confusion_matrix_metric.ConfusionMatrixMetric dataclass

Bases: BaseMetric

Accumulate class predictions and expose a confusion matrix.

Attributes:

Name Type Description
normalize str | None

None for counts, or true, pred, or all for normalized matrix values.

value ndarray | None

Latest matrix after compute; None before any data is computed.

The compute method returns a cm_acc scalar; use value to retrieve the matrix.

chimera_ml.metrics.confusion_matrix_metric.confusion_matrix_metric

confusion_matrix_metric(**params)