🎓 A1Learner
🔍

PyTorch Measuring Accuracy

Accuracy is the fraction of predictions a classifier gets right. A prediction is the class with the highest score in one output row.

Argmax reads the prediction

Each row holds one score per class. argmax picks the position of the largest score.

scores = torch.tensor([[2.1, 0.3, -1.0],
                       [0.2, 1.5, 0.9],
                       [-0.5, 0.1, 3.0]])
predictions = scores.argmax(dim=1)
print("Predictions:", predictions.tolist())

Count the right answers

Comparing predictions with labels gives True and False values. Their mean is the accuracy.

predictions = torch.tensor([0, 1, 2, 1])
labels = torch.tensor([0, 2, 2, 1])
correct = (predictions == labels).sum().item()
accuracy = correct / len(labels)
print("Correct:", correct)
print("Accuracy:", accuracy)

Example

import torch
from torch import nn

torch.manual_seed(0)
centers = torch.tensor([[0.0, 0.0], [3.0, 0.0], [0.0, 3.0]])
counts = [34, 33, 33]
parts = []
labels = []
for label, count in enumerate(counts):
    parts.append(centers[label] + 0.5 * torch.randn(count, 2))
    labels += [label] * count
x = torch.cat(parts)
y = torch.tensor(labels)

torch.manual_seed(0)
model = nn.Linear(2, 3)
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.05)

with torch.no_grad():
    start_scores = model(x)
    start_pred = start_scores.argmax(dim=1)
    start_acc = (start_pred == y).float().mean().item()

for step in range(200):
    scores = model(x)
    loss = loss_fn(scores, y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

with torch.no_grad():
    final_scores = model(x)
    final_pred = final_scores.argmax(dim=1)
    final_acc = (final_pred == y).float().mean().item()

print("Points:", x.shape[0])
print("Accuracy before training:", round(start_acc, 2))
print("Accuracy after training:", round(final_acc, 2))

Key points

  • Prediction: the class number that argmax reads off a score row
  • Use scores.argmax(dim=1) to turn score rows into class numbers
  • Correct count: the sum of predictions == labels
  • Accuracy: the correct count divided by the total count
  • Accuracy 1.0 means every prediction was right
  • Accuracy turns raw scores into one plain score for the model
  • Use .float() to turn True and False into 1.0 and 0.0 so a mean can average them.

In this example, seed 0 is set before the data is made. The data is 100 points in three clouds around three centres. Each cloud carries one class label. Seed 0 is set again before the model is built. The model is one Linear layer with three outputs. A no_grad pass scores the untrained model first. Its accuracy prints as 0.08. The training loop then runs 200 steps with cross-entropy and Adam. A second no_grad pass scores the trained model. Its accuracy prints as 1.0. Training turned almost every wrong answer right.

Note

Accuracy says nothing about which class fails, only how often

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

A classifier gets 45 of 50 test points right. What is its accuracy?