torcheval.metrics.functional.multiclass_accuracy¶
-
torcheval.metrics.functional.
multiclass_accuracy
(input: Tensor, target: Tensor, *, average: Optional[str] = 'micro', num_classes: Optional[int] = None, k: int = 1) Tensor [source]¶ Compute accuracy score, which is the frequency of input matching target. Its class version is
torcheval.metrics.MultiClassAccuracy
. See alsobinary_accuracy
,multilabel_accuracy
,topk_multilabel_accuracy
Parameters: - input (Tensor) – Tensor of label predictions
It could be the predicted labels, with shape of (n_sample, ).
It could also be probabilities or logits with shape of (n_sample, n_class).
torch.argmax
will be used to convert input into predicted labels. - target (Tensor) – Tensor of ground truth labels with shape of (n_sample, ).
- average –
'micro'
[default]:- Calculate the metrics globally.
'macro'
:- Calculate metrics for each class separately, and return their unweighted mean. Classes with 0 true instances are ignored.
None
:- Calculate the metric for each class separately, and return
the metric for every class.
NaN is returned if a class has no sample in
target
.
- num_classes – Number of classes. Required for
'macro'
andNone
average methods. - k – Number of top probabilities to be considered. K should be an integer greater than or equal to 1. If k > 1, the input tensor must contain probabilities or logits for every class.
Examples:
>>> import torch >>> from torcheval.metrics.functional import multiclass_accuracy >>> input = torch.tensor([0, 2, 1, 3]) >>> target = torch.tensor([0, 1, 2, 3]) >>> multiclass_accuracy(input, target) tensor(0.5) >>> multiclass_accuracy(input, target, average=None, num_classes=4) tensor([1., 0., 0., 1.]) >>> multiclass_accuracy(input, target, average="macro", num_classes=4) tensor(0.5) >>> input = torch.tensor([[0.9, 0.1, 0, 0], [0.1, 0.2, 0.4, 0,3], [0, 1.0, 0, 0], [0, 0, 0.2, 0.8]]) >>> multiclass_accuracy(input, target) tensor(0.5)
- input (Tensor) – Tensor of label predictions
It could be the predicted labels, with shape of (n_sample, ).
It could also be probabilities or logits with shape of (n_sample, n_class).