File size: 11,377 Bytes
9c6594c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# Copyright The Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional
import torch
from torch import Tensor
from torchmetrics.functional.classification.confusion_matrix import (
_multilabel_confusion_matrix_arg_validation,
_multilabel_confusion_matrix_format,
_multilabel_confusion_matrix_tensor_validation,
)
from torchmetrics.utilities.data import _cumsum
def _rank_data(x: Tensor) -> Tensor:
"""Rank data based on values."""
# torch.unique does not support input that requires grad
with torch.no_grad():
_, inverse, counts = torch.unique(x, sorted=True, return_inverse=True, return_counts=True)
ranks = _cumsum(counts, dim=0)
return ranks[inverse]
def _ranking_reduce(score: Tensor, num_elements: int) -> Tensor:
return score / num_elements
def _multilabel_ranking_tensor_validation(
preds: Tensor, target: Tensor, num_labels: int, ignore_index: Optional[int] = None
) -> None:
_multilabel_confusion_matrix_tensor_validation(preds, target, num_labels, ignore_index)
if not preds.is_floating_point():
raise ValueError(f"Expected preds tensor to be floating point, but received input with dtype {preds.dtype}")
def _multilabel_coverage_error_update(preds: Tensor, target: Tensor) -> tuple[Tensor, int]:
"""Accumulate state for coverage error."""
offset = torch.zeros_like(preds)
offset[target == 0] = preds.min().abs() + 10 # Any number >1 works
preds_mod = preds + offset
preds_min = preds_mod.min(dim=1)[0]
coverage = (preds >= preds_min[:, None]).sum(dim=1).to(torch.float32)
return coverage.sum(), coverage.numel()
def multilabel_coverage_error(
preds: Tensor,
target: Tensor,
num_labels: int,
ignore_index: Optional[int] = None,
validate_args: bool = True,
) -> Tensor:
"""Compute multilabel coverage error [1].
The score measure how far we need to go through the ranked scores to cover all true labels. The best value is equal
to the average number of labels in the target tensor per sample.
Accepts the following input tensors:
- ``preds`` (float tensor): ``(N, C, ...)``. Preds should be a tensor containing probabilities or logits for each
observation. If preds has values outside [0,1] range we consider the input to be logits and will auto apply
sigmoid per element.
- ``target`` (int tensor): ``(N, C, ...)``. Target should be a tensor containing ground truth labels, and therefore
only contain {0,1} values (except if `ignore_index` is specified).
Additional dimension ``...`` will be flattened into the batch dimension.
Args:
preds: Tensor with predictions
target: Tensor with true labels
num_labels: Integer specifying the number of labels
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.
Example:
>>> from torch import rand, randint
>>> from torchmetrics.functional.classification import multilabel_coverage_error
>>> preds = rand(10, 5)
>>> target = randint(2, (10, 5))
>>> multilabel_coverage_error(preds, target, num_labels=5)
tensor(3.9000)
References:
[1] Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010). Mining multi-label data. In Data mining and
knowledge discovery handbook (pp. 667-685). Springer US.
"""
if validate_args:
_multilabel_confusion_matrix_arg_validation(num_labels, threshold=0.0, ignore_index=ignore_index)
_multilabel_ranking_tensor_validation(preds, target, num_labels, ignore_index)
preds, target = _multilabel_confusion_matrix_format(
preds, target, num_labels, threshold=0.0, ignore_index=ignore_index, should_threshold=False
)
coverage, total = _multilabel_coverage_error_update(preds, target)
return _ranking_reduce(coverage, total)
def _multilabel_ranking_average_precision_update(preds: Tensor, target: Tensor) -> tuple[Tensor, int]:
"""Accumulate state for label ranking average precision."""
# Invert so that the highest score receives rank 1
neg_preds = -preds
score = torch.tensor(0.0, device=neg_preds.device)
num_preds, num_labels = neg_preds.shape
for i in range(num_preds):
relevant = target[i] == 1
ranking = _rank_data(neg_preds[i][relevant]).float()
if len(ranking) > 0 and len(ranking) < num_labels:
rank = _rank_data(neg_preds[i])[relevant].float()
score_idx = (ranking / rank).mean()
else:
score_idx = torch.ones_like(score)
score += score_idx
return score, num_preds
def multilabel_ranking_average_precision(
preds: Tensor,
target: Tensor,
num_labels: int,
ignore_index: Optional[int] = None,
validate_args: bool = True,
) -> Tensor:
"""Compute label ranking average precision score for multilabel data [1].
The score is the average over each ground truth label assigned to each sample of the ratio of true vs. total labels
with lower score. Best score is 1.
Accepts the following input tensors:
- ``preds`` (float tensor): ``(N, C, ...)``. Preds should be a tensor containing probabilities or logits for each
observation. If preds has values outside [0,1] range we consider the input to be logits and will auto apply
sigmoid per element.
- ``target`` (int tensor): ``(N, C, ...)``. Target should be a tensor containing ground truth labels, and therefore
only contain {0,1} values (except if `ignore_index` is specified).
Additional dimension ``...`` will be flattened into the batch dimension.
Args:
preds: Tensor with predictions
target: Tensor with true labels
num_labels: Integer specifying the number of labels
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.
Example:
>>> from torch import rand, randint
>>> from torchmetrics.functional.classification import multilabel_ranking_average_precision
>>> preds = rand(10, 5)
>>> target = randint(2, (10, 5))
>>> multilabel_ranking_average_precision(preds, target, num_labels=5)
tensor(0.7744)
References:
[1] Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010). Mining multi-label data. In Data mining and
knowledge discovery handbook (pp. 667-685). Springer US.
"""
if validate_args:
_multilabel_confusion_matrix_arg_validation(num_labels, threshold=0.0, ignore_index=ignore_index)
_multilabel_ranking_tensor_validation(preds, target, num_labels, ignore_index)
preds, target = _multilabel_confusion_matrix_format(
preds, target, num_labels, threshold=0.0, ignore_index=ignore_index, should_threshold=False
)
score, num_elements = _multilabel_ranking_average_precision_update(preds, target)
return _ranking_reduce(score, num_elements)
def _multilabel_ranking_loss_update(preds: Tensor, target: Tensor) -> tuple[Tensor, int]:
"""Accumulate state for label ranking loss.
Args:
preds: tensor with predictions
target: tensor with ground truth labels
sample_weight: optional tensor with weight for each sample
"""
num_preds, num_labels = preds.shape
relevant = target == 1
num_relevant = relevant.sum(dim=1)
# Ignore instances where number of true labels is 0 or n_labels
mask = (num_relevant > 0) & (num_relevant < num_labels)
preds = preds[mask]
relevant = relevant[mask]
num_relevant = num_relevant[mask]
# Nothing is relevant
if len(preds) == 0:
return torch.tensor(0.0, device=preds.device), 1
inverse = preds.argsort(dim=1).argsort(dim=1)
per_label_loss = ((num_labels - inverse) * relevant).to(torch.float32)
correction = 0.5 * num_relevant * (num_relevant + 1)
denom = num_relevant * (num_labels - num_relevant)
loss = (per_label_loss.sum(dim=1) - correction) / denom
return loss.sum(), num_preds
def multilabel_ranking_loss(
preds: Tensor,
target: Tensor,
num_labels: int,
ignore_index: Optional[int] = None,
validate_args: bool = True,
) -> Tensor:
"""Compute the label ranking loss for multilabel data [1].
The score is corresponds to the average number of label pairs that are incorrectly ordered given some predictions
weighted by the size of the label set and the number of labels not in the label set. The best score is 0.
Accepts the following input tensors:
- ``preds`` (float tensor): ``(N, C, ...)``. Preds should be a tensor containing probabilities or logits for each
observation. If preds has values outside [0,1] range we consider the input to be logits and will auto apply
sigmoid per element.
- ``target`` (int tensor): ``(N, C, ...)``. Target should be a tensor containing ground truth labels, and therefore
only contain {0,1} values (except if `ignore_index` is specified).
Additional dimension ``...`` will be flattened into the batch dimension.
Args:
preds: Tensor with predictions
target: Tensor with true labels
num_labels: Integer specifying the number of labels
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.
Example:
>>> from torch import rand, randint
>>> from torchmetrics.functional.classification import multilabel_ranking_loss
>>> preds = rand(10, 5)
>>> target = randint(2, (10, 5))
>>> multilabel_ranking_loss(preds, target, num_labels=5)
tensor(0.4167)
References:
[1] Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010). Mining multi-label data. In Data mining and
knowledge discovery handbook (pp. 667-685). Springer US.
"""
if validate_args:
_multilabel_confusion_matrix_arg_validation(num_labels, threshold=0.0, ignore_index=ignore_index)
_multilabel_ranking_tensor_validation(preds, target, num_labels, ignore_index)
preds, target = _multilabel_confusion_matrix_format(
preds, target, num_labels, threshold=0.0, ignore_index=ignore_index, should_threshold=False
)
loss, num_elements = _multilabel_ranking_loss_update(preds, target)
return _ranking_reduce(loss, num_elements)
|