Classification#

kornia.metrics.accuracy(pred, target, topk=(1,))[source]#

Compute the accuracy over the k top predictions for the specified values of k.

Convention:
  • pred holds one score per class, \((B, C)\); only the order of the scores in a row matters. target holds class indices, \((B,)\) or \((B, 1)\).

  • The result is a list with one 0-d float32 tensor per entry of topk, in that order. Each is a percentage in \([0, 100]\), not a fraction, taken over the whole batch: a sample counts for k when its target is among its k highest scores, and a k above \(C\) counts as \(C\). Losses and metrics lists the scale of every task metric.

Parameters:
  • pred (Tensor) – the class scores, with shape \((B, C)\).

  • target (Tensor) – the ground-truth class indices, with shape \((B,)\) or \((B, 1)\).

  • topk (Tuple[int, ...], optional) – the expected topk ranking. Default: (1,)

Return type:

List[Tensor]

Example

>>> logits = torch.tensor([[0, 1, 0]])
>>> target = torch.tensor([[1]])
>>> accuracy(logits, target)
[tensor(100.)]