liveBook · Manning

Accuracy

As explained in Build a Text-to-Image Generator (from Scratch)

Accuracy is the proportion of predictions that exactly match the corresponding ground-truth labels.

Classification accuracy is the proportion of predictions that exactly match their ground-truth labels. It summarizes how often a classifier is correct across an evaluated dataset. section 3.4.3 section 13.4.3

Calculate accuracy through a short prediction-and-comparison pipeline:

  • Run the evaluation dataset through the model and obtain class logits. section 3.4.3
  • Select the class with the highest logit as each prediction. section 3.4.3
  • Compare predictions with ground-truth labels, count exact matches, and aggregate the result across test batches. section 3.4.3 section 13.4.3

Two evaluation results

A trained ViT classifier reaches 0.5621 accuracy on its test evaluation. A fine-tuned ResNet50 reaches 0.9373 on a balanced test set containing 10,000 real and 10,000 fake images. section 3.4.3 section 13.4.3

Listing 3.10 Using the trained ViT to classify images
import math
import torch.nn.functional as F  

model.load_state_dict(torch.load('files/ViT.pth'))
model.eval()
with torch.no_grad():
    batch=next(iter(testloader))  #1
    batch = [t.to(device) for t in batch]
    images, labels = batch
    logits, attention_maps = model(images, 
        output_attentions=True)  #2
    predictions = torch.argmax(logits, dim=1)  #3

print(predictions)
print([names[i] for i in predictions.tolist()])  #4

Inspect a complete PyTorch evaluation path: load trained weights, switch to evaluation mode, obtain logits, select predictions with argmax, and display class names. section 3.4.3

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Explains prediction extraction and batch aggregation with runnable PyTorch code, stopping short of alternative classification metrics.
Examples
Uses CIFAR-10 ViT evaluation at 0.5621 and balanced real-versus-fake ResNet50 evaluation at 0.9373.
What it assumes
Assumes a trained classifier, labeled test data, logits, and class-index conventions while demonstrating prediction comparison.
What you can do after
Apply accuracy via PyTorch evaluation loops that compare argmax predictions with labels and report the aggregate result.
How it connects
Connects ground-truth labels, logits, argmax prediction, softmax, test sets, and transfer learning.

Also explained in

Compare how 32 books explain accuracy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.