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.

Classification accuracy is the fraction of predictions that match their true labels. It gives a direct, practical measure of how often a trained classifier gets an example right. section 2.3.3

For binary classification, convert predicted probabilities into labels with a cutoff: probabilities below 0.5 become 0, and probabilities at or above 0.5 become 1. Compare those labels with the actual labels, count matches, and average the results across test batches. section 2.3.3

Fashion MNIST classification

An ankle-boot classifier reaches 87.84% accuracy on the test set. For ten-category clothing classification, choose the class with torch.argmax, compare it with the true label, and obtain an accuracy of 0.8819665605095541. 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}")

Follow the complete PyTorch calculation: load test batches, reshape images and labels, threshold predictions, compare tensors, average matches, and print accuracy. section 2.3.3

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains thresholding, argmax selection, tensor comparison, and batch averaging, but does not develop alternative evaluation metrics.
Examples
Uses ankle-boot recognition, Fashion MNIST clothing labels, torch.where thresholding, and torch.argmax class selection.
What it assumes
Assumes a trained classifier, labeled batches, and model outputs; teaches prediction-to-label conversion and correctness comparison through examples.
What you can do after
Evaluate a PyTorch classifier by converting outputs into labels, comparing them with targets, and averaging correct predictions.
How it connects
Connects classification accuracy with binary classification, multicategory classification, sigmoid, softmax, and test datasets.

Also explained in

Compare how 32 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.