IsotonicRegressionScaler#

class torch_uncertainty.post_processing.IsotonicRegressionScaler(model=None, eps=1e-06, device=None)[source]#

Isotonic Regression post-processing for calibrated probabilities (Zadrozny & Elkan, 2002).

A non-parametric calibration method that fits a piecewise-constant, monotonically non-decreasing mapping \(f\) from the uncalibrated probabilities to the calibrated ones by minimising the mean squared error:

\[\min_{f \text{ non-decreasing}} \sum_{i=1}^{N} \left( y_i - f(\hat{p}_i) \right)^2.\]

Compared to HistogramBinningScaler, the bins are not pre-defined and the resulting mapping is smoother. Multi-class calibration is handled with a one-vs-rest approach: one isotonic regressor is fit per class and the calibrated probabilities are renormalised to sum to one.

Parameters:
  • model (Module | None) – Model to calibrate. Defaults to None.

  • eps (float) – Small value for stability when converting probs back to logits. Defaults to 1e-6.

  • device (Union[Literal['cpu', 'cuda'], device, None]) – Device to use for tensor operations. Defaults to None.

References

[1] Zadrozny, B., & Elkan, C. (2002). Transforming classifier scores into accurate multiclass probability estimates. KDD 2002.

Note

This implementation uses scikit-learn’s IsotonicRegression as the underlying solver.

Warning

Isotonic regression requires a sufficient amount of calibration data to avoid overfitting the step function, especially in multi-class scenarios.

fit(dataloader, progress=True)[source]#

Fit the isotonic regression models to the calibration data.

For binary classification, a single isotonic regressor is fit. For multiclass classification, a One-vs-Rest (OvR) approach is used: one regressor is trained per class to predict the probability of that class versus all others.

Parameters:
  • dataloader (DataLoader) – Dataloader providing the calibration data (logits and targets).

  • progress (bool) – Whether to show a progress bar during data extraction. Defaults to True.

Return type:

None

forward(inputs)[source]#

Apply the fitted Isotonic Regression and return calibrated logits.

The forward pass transforms the input logits into probabilities, applies the isotonic mapping, and then converts the resulting probabilities back into the logit space for compatibility with downstream loss functions or metrics.

Parameters:

inputs (Tensor) – Input logits to be calibrated.

Returns:

Calibrated logits.

Return type:

Tensor

set_model(model)#

Attach a model to the post-processing module.

Return type:

None