liveBook · Manning

Accuracy

As explained in Learn Generative AI with PyTorch

The fraction or percentage of predictions that match the true labels in a classification task.

Accuracy is the fraction of classification predictions that match the true labels. It provides a straightforward way to evaluate predictions on a test dataset. section 2.3.3 section 2.4.2

Calculate accuracy by turning model outputs into predicted labels, then comparing them with the true labels:

  • For binary classification, convert probabilities below 0.5 to 0 and probabilities at or above 0.5 to 1. section 2.3.3
  • For multicategory classification, choose the class with the highest output value. section 2.4.2
  • Compare predicted and true labels, then average the matches. section 2.3.3 section 2.4.2

Fashion MNIST classification

A binary image classifier distinguishes ankle boots from T-shirts and reports 87.84% accuracy on the test set. A multicategory classifier evaluates ten clothing labels and reports accuracy of about 0.882. section 2.3.3 section 2.4.2

Listing 2.7 Calculating the accuracy of the predictions
import numpy as np
results=[]
for imgs,labels in binary_test_loader:                     #1
    imgs=imgs.reshape(-1,28*28).to(device)
    labels=(labels/9).reshape(-1,1).to(device)
    preds=binary_model(imgs)
    pred10=torch.where(preds>0.5,1,0)                      #2
    correct=(pred10==labels)                               #3
    results.append(correct.detach().cpu()\
      .numpy().mean())                                     #4
accuracy=np.array(results).mean()                          #5
print(f"the accuracy of the predictions is {accuracy}")

See the PyTorch listing that thresholds binary predictions, compares them with labels, and calculates accuracy. section 2.3.3

How Learn Generative AI with PyTorch covers it

How deep it goes
Calculates accuracy by thresholding binary probabilities or selecting the highest-scoring class, then averaging label matches.
Examples
Uses Fashion MNIST images to classify ankle boots versus T-shirts and to predict ten clothing categories.
What you can do after
Evaluate a trained classifier by comparing thresholded binary or argmax multicategory predictions with test labels.
How it connects
Connects accuracy to binary classification, multiclass classification, predicted labels, and test datasets.

Also explained in

Compare how 34 books explain accuracy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.