Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `TER` never shifting a block of 10 words in one step, which made it disagree with Tercom and sacrebleu ([#3594](https://github.com/Lightning-AI/torchmetrics/pull/3594))
- Fixed `GeneralizedDiceScore` using a misaligned per-class weight when a class is absent from a sample's target but present in its prediction (`weight_type="square"`/`"simple"`), returning incorrect scores whenever the batch size differed from the number of classes ([#3564](https://github.com/Lightning-AI/torchmetrics/pull/3564))
- Fixed multiclass stat scores, and the metrics built on them such as `MulticlassAccuracy`, allocating `num_classes**2` memory for non-micro averages, which ran out of memory for large `num_classes` ([#3343](https://github.com/Lightning-AI/torchmetrics/issues/3343))
- Fixed `LogAUC` accepting a lower `fpr_range` bound of 0, which gave `nan`, and `multilabel_logauc` not validating `fpr_range` ([#3662](https://github.com/Lightning-AI/torchmetrics/pull/3662))


---
Expand Down
6 changes: 4 additions & 2 deletions src/torchmetrics/functional/classification/logauc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def _validate_fpr_range(fpr_range: Tuple[float, float]) -> None:
"""Validate the `fpr_range` argument for the logauc metric."""
if not isinstance(fpr_range, tuple) or len(fpr_range) != 2:
raise ValueError(f"The `fpr_range` should be a tuple of two floats, but got {type(fpr_range)}.")
if not (0 <= fpr_range[0] < fpr_range[1] <= 1):
raise ValueError(f"The `fpr_range` should be a tuple of two floats in the range [0, 1], but got {fpr_range}.")
if not (0 < fpr_range[0] < fpr_range[1] <= 1):
raise ValueError(f"The `fpr_range` should be a tuple of two floats in the range (0, 1], but got {fpr_range}.")


def _binary_logauc_compute(
Expand Down Expand Up @@ -316,6 +316,8 @@ def multilabel_logauc(
tensor([0.5000, 0.0000, 0.6835])

"""
if validate_args:
_validate_fpr_range(fpr_range)
fpr, tpr, _ = multilabel_roc(preds, target, num_labels, thresholds, ignore_index, validate_args)
return _reduce_logauc(fpr, tpr, fpr_range, average=average)

Expand Down
16 changes: 16 additions & 0 deletions tests/unittests/classification/test_logauc.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,22 @@ def test_binary_logauc_validates_inputs_by_default():
BinaryLogAUC(validate_args=False).update(preds, target)


@pytest.mark.parametrize(
("metric", "args"),
[
(binary_logauc, {}),
(multiclass_logauc, {"num_classes": 3}),
(multilabel_logauc, {"num_labels": 3}),
],
)
def test_logauc_rejects_zero_fpr_lower_bound(metric, args):
"""Check that a lower FPR bound of 0, where log10 is undefined and the score would be nan, is rejected."""
preds = torch.rand(10, 3) if args else torch.rand(10)
target = torch.randint(0, 3, (10,)) if "num_classes" in args else torch.randint(0, 2, preds.shape)
with pytest.raises(ValueError, match=r"should be a tuple of two floats in the range \(0, 1\]"):
metric(preds, target, fpr_range=(0.0, 0.1), **args)


@pytest.mark.parametrize(
("metric", "kwargs"),
[
Expand Down