Shortcuts

torcheval.metrics.MulticlassAccuracy

class torcheval.metrics.MulticlassAccuracy(*, average: Optional[str] = 'micro', num_classes: Optional[int] = None, k: int = 1, device: Optional[device] = None)[source]

Compute accuracy score, which is the frequency of input matching target. Its functional version is torcheval.metrics.functional.multiclass_accuracy(). See also BinaryAccuracy, MultilabelAccuracy, TopKMultilabelAccuracy

Parameters:
  • average (str, Optional) –
    • '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' and None 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 import MulticlassAccuracy
>>> metric = MulticlassAccuracy()
>>> input = torch.tensor([0, 2, 1, 3])
>>> target = torch.tensor([0, 1, 2, 3])
>>> metric.update(input, target)
>>> metric.compute()
tensor(0.5)

>>> metric = MulticlassAccuracy(average=None, num_classes=4)
>>> input = torch.tensor([0, 2, 1, 3])
>>> target = torch.tensor([0, 1, 2, 3])
>>> metric.update(input, target)
>>> metric.compute()
tensor([1., 0., 0., 1.])

>>> metric = MulticlassAccuracy(average="macro", num_classes=2)
>>> input = torch.tensor([0, 0, 1, 1, 1])
>>> target = torch.tensor([0, 0, 0, 0, 1])
>>> metric.update(input, target)
>>> metric.compute()
tensor(0.75)

>>> metric = MulticlassAccuracy()
>>> 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]])
>>> target = torch.tensor([0, 1, 2, 3])
>>> metric.update(input, target)
>>> metric.compute()
tensor(0.5)
__init__(*, average: Optional[str] = 'micro', num_classes: Optional[int] = None, k: int = 1, device: Optional[device] = None) None[source]

Initialize a metric object and its internal states.

Use self._add_state() to initialize state variables of your metric class. The state variables should be either torch.Tensor, a list of torch.Tensor, or a dictionary with torch.Tensor as values

Methods

__init__(*[, average, num_classes, k, device]) Initialize a metric object and its internal states.
compute() Return the accuracy score.
load_state_dict(state_dict[, strict]) Loads metric state variables from state_dict.
merge_state(metrics) Implement this method to update the current metric's state variables to be the merged states of the current metric and input metrics.
reset() Reset the metric state variables to their default value.
state_dict() Save metric state variables in state_dict.
to(device, *args, **kwargs) Move tensors in metric state variables to device.
update(input, target) Update states with the ground truth labels and predictions.

Attributes

device The last input device of Metric.to().

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources