Label Ranking Average Precision¶
Module Interface¶
- class torchmetrics.LabelRankingAveragePrecision(**kwargs)[source]
Computes 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.
- Parameters
kwargs¶ (
Any
) – Additional keyword arguments, see Advanced metric settings for more info.
Example
>>> from torchmetrics import LabelRankingAveragePrecision >>> _ = torch.manual_seed(42) >>> preds = torch.rand(10, 5) >>> target = torch.randint(2, (10, 5)) >>> metric = LabelRankingAveragePrecision() >>> metric(preds, target) 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.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- update(preds, target, sample_weight=None)[source]
- Parameters
preds¶ (
Tensor
) – tensor of shape[N,L]
whereN
is the number of samples andL
is the number of labels. Should either be probabilities of the positive class or corresponding logitstarget¶ (
Tensor
) – tensor of shape[N,L]
whereN
is the number of samples andL
is the number of labels. Should only contain binary labels.sample_weight¶ (
Optional
[Tensor
]) – tensor of shapeN
whereN
is the number of samples. How much each sample should be weighted in the final score.
- Return type
- class torchmetrics.classification.MultilabelRankingAveragePrecision(num_labels, ignore_index=None, validate_args=True, **kwargs)[source]
Computes 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.- Parameters
Example
>>> from torchmetrics.classification import MultilabelRankingAveragePrecision >>> _ = torch.manual_seed(42) >>> preds = torch.rand(10, 5) >>> target = torch.randint(2, (10, 5)) >>> metric = MultilabelRankingAveragePrecision(num_labels=5) >>> metric(preds, target) 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.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- compute()[source]
Override this method to compute the final metric value from state variables synchronized across the distributed backend.
- Return type
Functional Interface¶
- torchmetrics.functional.label_ranking_average_precision(preds, target, sample_weight=None)[source]
Computes 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.
- Parameters
preds¶ (
Tensor
) – tensor of shape[N,L]
whereN
is the number of samples andL
is the number of labels. Should either be probabilities of the positive class or corresponding logitstarget¶ (
Tensor
) – tensor of shape[N,L]
whereN
is the number of samples andL
is the number of labels. Should only contain binary labels.sample_weight¶ (
Optional
[Tensor
]) – tensor of shapeN
whereN
is the number of samples. How much each sample should be weighted in the final score.
Example
>>> from torchmetrics.functional import label_ranking_average_precision >>> _ = torch.manual_seed(42) >>> preds = torch.rand(10, 5) >>> target = torch.randint(2, (10, 5)) >>> label_ranking_average_precision(preds, target) 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.
- Return type
- torchmetrics.functional.classification.multilabel_ranking_average_precision(preds, target, num_labels, ignore_index=None, validate_args=True)[source]
Computes 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.- Parameters
Example
>>> from torchmetrics.functional.classification import multilabel_ranking_average_precision >>> _ = torch.manual_seed(42) >>> preds = torch.rand(10, 5) >>> target = torch.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.
- Return type