Skip to content

← Reductions and dim step 2 of 7

Easy Primitives

Accuracy from a comparison

Jump to the task

Accuracy is the number of correct predictions divided by the number of predictions. Comparing predicted labels with target labels gives a boolean mask; converting that mask to floating point lets you average its zeros and ones.

import torch

preds = torch.tensor([1, 2, 3, 4])
labels = torch.tensor([1, 0, 3, 4])
correct = preds == labels
print(correct.tolist())              # [True, False, True, True]
print(correct.sum().item())          # 3
print(correct.float().mean().item()) # 0.75

Calling correct.mean() directly raises a RuntimeError: without an explicit floating or complex dtype, mean() does not accept a boolean input. In contrast, sum() promotes boolean values to int64 by default. These are the operations' dtype rules, not a reason to assume that all reductions accept the same inputs.

Two ways to compute the fraction

For this nonempty mask, both correct.float().mean() and correct.sum() / correct.numel() compute the fraction correct. You can also request a floating dtype directly with correct.mean(dtype=torch.float32).

When combining multiple batches, add their correct counts and their element counts, then divide. Averaging batch accuracies without weights gives the wrong overall accuracy when batch sizes differ.

Return a tensor

The examples use .item() only to display Python numbers. The function below must return a scalar tensor, so leave that extraction out. On CUDA, .item() can wait for GPU work to make the value available to Python. If you only need a report at the end, accumulating counts on the device can avoid extracting a Python number after every batch.

Your task

def accuracy(preds: torch.Tensor, labels: torch.Tensor) -> torch.Tensor

The inputs are nonempty, one-dimensional integer tensors of the same shape, containing predicted and target class labels. Return the fraction of matching positions as a zero-dimensional floating-point tensor. Do not return a Python number or a per-position mask.