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
27 changes: 26 additions & 1 deletion src/torchmetrics/classification/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,22 @@ class BinaryAccuracy(BinaryStatScores):
Specifies a target value that is ignored and does not contribute to the metric calculation
validate_args: bool indicating if input arguments and tensors should be validated for correctness.
Set to ``False`` for faster computations.
input_format: str specifying the format of the input preds tensor. Can be one of:
- ``'probs'``: preds tensor contains values in the [0,1] range and is considered to be probabilities. Only
thresholding will be applied to the tensor and values will be checked to be in [0,1] range.
- ``'logits'``: preds tensor contains values outside the [0,1] range and is considered to be logits. We
will apply sigmoid to the tensor and threshold the values before calculating the metric.
- ``'labels'``: preds tensor contains integer values and is considered to be labels. No formatting will be
applied to preds tensor.

kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

Example (preds is int tensor):
>>> from torch import tensor
>>> from torchmetrics.classification import BinaryAccuracy
>>> target = tensor([0, 1, 0, 1, 0, 1])
>>> preds = tensor([0, 0, 1, 1, 0, 1])
>>> metric = BinaryAccuracy()
>>> metric = BinaryAccuracy(input_format="labels")
>>> metric(preds, target)
tensor(0.6667)

Expand Down Expand Up @@ -206,6 +215,13 @@ class MulticlassAccuracy(MulticlassStatScores):
Specifies a target value that is ignored and does not contribute to the metric calculation
validate_args: bool indicating if input arguments and tensors should be validated for correctness.
Set to ``False`` for faster computations.
input_format: str specifying the format of the input preds tensor. Can be one of:
- ``'probs'``: preds tensor contains values in the [0,1] range and is considered to be probabilities. Only
thresholding will be applied to the tensor and values will be checked to be in [0,1] range.
- ``'logits'``: preds tensor contains values outside the [0,1] range and is considered to be logits. We
will apply sigmoid to the tensor and threshold the values before calculating the metric.
- ``'labels'``: preds tensor contains integer values and is considered to be labels. No formatting will be
applied to preds tensor.

Example (preds is int tensor):
>>> from torch import tensor
Expand Down Expand Up @@ -359,6 +375,13 @@ class MultilabelAccuracy(MultilabelStatScores):
Specifies a target value that is ignored and does not contribute to the metric calculation
validate_args: bool indicating if input arguments and tensors should be validated for correctness.
Set to ``False`` for faster computations.
input_format: str specifying the format of the input preds tensor. Can be one of:
- ``'probs'``: preds tensor contains values in the [0,1] range and is considered to be probabilities. Only
thresholding will be applied to the tensor and values will be checked to be in [0,1] range.
- ``'logits'``: preds tensor contains values outside the [0,1] range and is considered to be logits. We
will apply sigmoid to the tensor and threshold the values before calculating the metric.
- ``'labels'``: preds tensor contains integer values and is considered to be labels. No formatting will be
applied to preds tensor.

Example (preds is int tensor):
>>> from torch import tensor
Expand Down Expand Up @@ -500,6 +523,7 @@ def __new__( # type: ignore[misc]
top_k: Optional[int] = 1,
ignore_index: Optional[int] = None,
validate_args: bool = True,
input_format: Literal["probs", "logits", "labels"] = "probs",
**kwargs: Any,
) -> Metric:
"""Initialize task metric."""
Expand All @@ -509,6 +533,7 @@ def __new__( # type: ignore[misc]
"multidim_average": multidim_average,
"ignore_index": ignore_index,
"validate_args": validate_args,
"input_format": input_format,
})

if task == ClassificationTask.BINARY:
Expand Down
4 changes: 2 additions & 2 deletions src/torchmetrics/classification/f_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class BinaryFBetaScore(BinaryStatScores):
>>> from torchmetrics.classification import BinaryFBetaScore
>>> target = tensor([0, 1, 0, 1, 0, 1])
>>> preds = tensor([0, 0, 1, 1, 0, 1])
>>> metric = BinaryFBetaScore(beta=2.0)
>>> metric = BinaryFBetaScore(beta=2.0, input_format="labels")
>>> metric(preds, target)
tensor(0.6667)

Expand Down Expand Up @@ -645,7 +645,7 @@ class BinaryF1Score(BinaryFBetaScore):
>>> from torchmetrics.classification import BinaryF1Score
>>> target = tensor([0, 1, 0, 1, 0, 1])
>>> preds = tensor([0, 0, 1, 1, 0, 1])
>>> metric = BinaryF1Score()
>>> metric = BinaryF1Score(input_format="labels")
>>> metric(preds, target)
tensor(0.6667)

Expand Down
35 changes: 30 additions & 5 deletions src/torchmetrics/classification/group_fairness.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class BinaryGroupStatRates(_AbstractGroupStatScores):
>>> target = torch.tensor([0, 1, 0, 1, 0, 1])
>>> preds = torch.tensor([0, 1, 0, 1, 0, 1])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> metric = BinaryGroupStatRates(num_groups=2)
>>> metric = BinaryGroupStatRates(num_groups=2, input_format="labels")
>>> metric(preds, target, groups)
{'group_0': tensor([0., 0., 1., 0.]), 'group_1': tensor([1., 0., 0., 0.])}

Expand All @@ -115,6 +115,7 @@ def __init__(
threshold: float = 0.5,
ignore_index: Optional[int] = None,
validate_args: bool = True,
input_format: Literal["probs", "logits", "labels"] = "probs",
**kwargs: Any,
) -> None:
super().__init__()
Expand All @@ -128,6 +129,7 @@ def __init__(
self.threshold = threshold
self.ignore_index = ignore_index
self.validate_args = validate_args
self.input_format = input_format

self._create_states(self.num_groups)

Expand All @@ -141,7 +143,14 @@ def update(self, preds: Tensor, target: Tensor, groups: Tensor) -> None:

"""
group_stats = _binary_groups_stat_scores(
preds, target, groups, self.num_groups, self.threshold, self.ignore_index, self.validate_args
preds,
target,
groups,
self.num_groups,
self.threshold,
self.ignore_index,
self.validate_args,
self.input_format,
)

self._update_states(group_stats)
Expand Down Expand Up @@ -183,6 +192,13 @@ class BinaryFairness(_AbstractGroupStatScores):
ignore_index: Specifies a target value that is ignored and does not contribute to the metric calculation
validate_args: bool indicating if input arguments and tensors should be validated for correctness.
Set to ``False`` for faster computations.
input_format: str specifying the format of the input preds tensor. Can be one of:
- ``'probs'``: preds tensor contains values in the [0,1] range and is considered to be probabilities. Only
thresholding will be applied to the tensor and values will be checked to be in [0,1] range.
- ``'logits'``: preds tensor contains values outside the [0,1] range and is considered to be logits. We
will apply sigmoid to the tensor and threshold the values before calculating the metric.
- ``'labels'``: preds tensor contains integer values and is considered to be labels. No formatting will be
applied to preds tensor.
kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

Returns:
Expand All @@ -195,7 +211,7 @@ class BinaryFairness(_AbstractGroupStatScores):
>>> target = torch.tensor([0, 1, 0, 1, 0, 1])
>>> preds = torch.tensor([0, 1, 0, 1, 0, 1])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> metric = BinaryFairness(2)
>>> metric = BinaryFairness(2, input_format="labels")
>>> metric(preds, target, groups)
{'DP_0_1': tensor(0.), 'EO_0_1': tensor(0.)}

Expand Down Expand Up @@ -223,6 +239,7 @@ def __init__(
threshold: float = 0.5,
ignore_index: Optional[int] = None,
validate_args: bool = True,
input_format: Literal["probs", "logits", "labels"] = "probs",
**kwargs: Any,
) -> None:
super().__init__()
Expand All @@ -234,7 +251,7 @@ def __init__(
)

if validate_args:
_binary_stat_scores_arg_validation(threshold, "global", ignore_index)
_binary_stat_scores_arg_validation(threshold, "global", ignore_index, input_format=input_format)

if not isinstance(num_groups, int) and num_groups < 2:
raise ValueError(f"Expected argument `num_groups` to be an int larger than 1, but got {num_groups}")
Expand All @@ -243,6 +260,7 @@ def __init__(
self.threshold = threshold
self.ignore_index = ignore_index
self.validate_args = validate_args
self.input_format = input_format

self._create_states(self.num_groups)

Expand All @@ -261,7 +279,14 @@ def update(self, preds: Tensor, target: Tensor, groups: Tensor) -> None:
target = torch.zeros(preds.shape)

group_stats = _binary_groups_stat_scores(
preds, target, groups, self.num_groups, self.threshold, self.ignore_index, self.validate_args
preds,
target,
groups,
self.num_groups,
self.threshold,
self.ignore_index,
self.validate_args,
self.input_format,
)

self._update_states(group_stats)
Expand Down
2 changes: 1 addition & 1 deletion src/torchmetrics/classification/hamming.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class BinaryHammingDistance(BinaryStatScores):
>>> from torchmetrics.classification import BinaryHammingDistance
>>> target = tensor([0, 1, 0, 1, 0, 1])
>>> preds = tensor([0, 0, 1, 1, 0, 1])
>>> metric = BinaryHammingDistance()
>>> metric = BinaryHammingDistance(input_format="labels")
>>> metric(preds, target)
tensor(0.3333)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class BinaryNegativePredictiveValue(BinaryStatScores):
>>> from torchmetrics.classification import BinaryNegativePredictiveValue
>>> target = tensor([0, 1, 0, 1, 0, 1])
>>> preds = tensor([0, 0, 1, 1, 0, 1])
>>> metric = BinaryNegativePredictiveValue()
>>> metric = BinaryNegativePredictiveValue(input_format="labels")
>>> metric(preds, target)
tensor(0.6667)

Expand Down
4 changes: 2 additions & 2 deletions src/torchmetrics/classification/precision_recall.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class BinaryPrecision(BinaryStatScores):
>>> from torchmetrics.classification import BinaryPrecision
>>> target = tensor([0, 1, 0, 1, 0, 1])
>>> preds = tensor([0, 0, 1, 1, 0, 1])
>>> metric = BinaryPrecision()
>>> metric = BinaryPrecision(input_format="labels")
>>> metric(preds, target)
tensor(0.6667)

Expand Down Expand Up @@ -543,7 +543,7 @@ class BinaryRecall(BinaryStatScores):
>>> from torchmetrics.classification import BinaryRecall
>>> target = tensor([0, 1, 0, 1, 0, 1])
>>> preds = tensor([0, 0, 1, 1, 0, 1])
>>> metric = BinaryRecall()
>>> metric = BinaryRecall(input_format="labels")
>>> metric(preds, target)
tensor(0.6667)

Expand Down
2 changes: 1 addition & 1 deletion src/torchmetrics/classification/specificity.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class BinarySpecificity(BinaryStatScores):
>>> from torchmetrics.classification import BinarySpecificity
>>> target = tensor([0, 1, 0, 1, 0, 1])
>>> preds = tensor([0, 0, 1, 1, 0, 1])
>>> metric = BinarySpecificity()
>>> metric = BinarySpecificity(input_format="labels")
>>> metric(preds, target)
tensor(0.6667)

Expand Down
Loading
Loading