Skip to content

feat(metrics): add Expected and Maximum Calibration Error metrics#3799

Open
rpp5501 wants to merge 3 commits into
pytorch:masterfrom
rpp5501:add-ece-metrics
Open

feat(metrics): add Expected and Maximum Calibration Error metrics#3799
rpp5501 wants to merge 3 commits into
pytorch:masterfrom
rpp5501:add-ece-metrics

Conversation

@rpp5501

@rpp5501 rpp5501 commented Jul 1, 2026

Copy link
Copy Markdown

Fixes #1009

Description

Adds ExpectedCalibrationError and MaximumCalibrationError to ignite.metrics,
implementing the model-calibration metrics requested in #1009 ("as coded in Baal").

Both metrics share a fixed-size, per-bin accumulator (sum of confidences, correct
count, and sample count per bin) updated in place with index_add_, so the retained
state is O(num_bins) regardless of dataset size. This avoids the O(N)
torch.cat growth of the prior attempt (#3132), which OOMs on large datasets.
compute() is fully vectorized (no Python loop over bins) and uses
@reinit__is_reduced / @sync_all_reduce for correct reduction under distributed
training. y_pred is expected to be probabilities; binary (B,)/(B, 1) and
multiclass (B, C)/(B, C, ...) inputs are supported.

Registers both metrics in ignite.metrics, documents them in
docs/source/metrics.rst, and adds unit and distributed tests validated against a
NumPy reference of the Baal binning.

Check list:

  • New tests are added
  • New doc strings: description and/or example code are in RST format
  • Documentation is updated

Add ExpectedCalibrationError and MaximumCalibrationError to ignite.metrics,
implementing the model-calibration metrics requested in pytorch#1009 ("as coded in
Baal").

Both metrics share a fixed-size, per-bin accumulator (sum of confidences,
correct count, and sample count per bin) updated in place with index_add_, so
the retained state is O(num_bins) regardless of dataset size. This avoids the
O(N) torch.cat growth of the prior attempt (pytorch#3132), which OOMs on large
datasets. compute() is fully vectorized (no Python loop over bins) and uses
@reinit__is_reduced / @sync_all_reduce for correct reduction under distributed
training. y_pred is expected to be probabilities; binary (B,)/(B, 1) and
multiclass (B, C)/(B, C, ...) inputs are supported.

Register both metrics in ignite.metrics, document them in
docs/source/metrics.rst, and add unit and distributed tests validated against a
NumPy reference of the Baal binning.

Closes pytorch#1009
Copilot AI review requested due to automatic review settings July 1, 2026 03:52
@github-actions github-actions Bot added docs module: metrics Metrics module labels Jul 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds model calibration metrics to ignite.metrics by introducing Expected Calibration Error (ECE) and Maximum Calibration Error (MCE), with an O(num_bins) accumulator design suitable for large datasets and distributed training.

Changes:

  • Introduces ExpectedCalibrationError and MaximumCalibrationError metrics with per-bin accumulators and distributed reduction support.
  • Adds unit and distributed tests validated against a NumPy reference implementation.
  • Registers the metrics in ignite.metrics and includes them in the metrics documentation list.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
tests/ignite/metrics/test_calibration_error.py Adds reference-based unit tests and distributed integration tests for ECE/MCE.
ignite/metrics/calibration_error.py Implements shared bin-accumulation logic and the ECE/MCE metric computations.
ignite/metrics/init.py Exposes the new metrics at the ignite.metrics package level.
docs/source/metrics.rst Lists the new metrics in the documentation’s “Complete list of metrics”.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ignite/metrics/calibration_error.py Outdated
Examples:
To use with ``Engine`` and ``process_function``, simply attach the metric instance to the engine.
The output of the engine's ``process_function`` needs to be in the format of
``(y_pred, y)`` or ``{'y_pred': y_pred, 'y': y, ...}``. If not, ``output_tranform`` can be added
Comment on lines +109 to +112
n_iters = y.shape[0] // batch_size + 1
for i in range(n_iters):
idx = i * batch_size
metric.update((y_pred[idx : idx + batch_size], y[idx : idx + batch_size]))
rpp5501 and others added 2 commits July 1, 2026 19:49
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Expected Calibration Error metrics and derived

2 participants