liveBook · Manning

Accuracy

As explained in Build a Large Language Model (From Scratch)

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

Classification accuracy measures the fraction or percentage of predictions that match the correct class labels. For spam detection, the model predicts either 0 for non-spam or 1 for spam. Because classification uses two output values rather than the vocabulary-sized output used in pretraining, prediction takes the final-token logits and selects the largest value with argmax. section 6.6 section 6.9

Calculate accuracy across batches by following this flow:

  • Run the model in evaluation mode and read model(input_batch)[:, -1, :], producing one two-class logit vector per input. section 6.6
  • Convert each vector to a predicted label with torch.argmax(logits, dim=-1); applying softmax first is optional because it does not change the largest index. section 6.6
  • Count predictions equal to target_batch, divide correct predictions by the number of examples, and optionally limit evaluation to a fixed number of batches. section 6.6

Before and after fine-tuning

For logits [-3.5983, 3.9902], argmax returns class 1, meaning spam. Before training, sample accuracies are near the 50% random baseline: 46.25% training, 45.00% validation, and 48.75% test. After fine-tuning, they reach 97.21%, 97.32%, and 95.67%. section 6.6 section 6.7

Figure 6.14 The model outputs corresponding to the last token are converted into probability scores for each input text. The class labels are obtained by looking up the index position of the highest probability score. The model predicts the spam labels incorrectly because it has not yet been trained.
figure

Trace how final-token logits become class probabilities and labels, including why an untrained model can misclassify spam examples. section 6.6

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Explains final-token argmax, batch aggregation, device handling, and evaluation limits, but uses cross-entropy for optimization.
Examples
Uses spam-versus-non-spam labels, two-class logits, batch loaders, and reported training, validation, and test percentages.
What it assumes
Assumes tokenized inputs, target labels, data loaders, and a pretrained model with a two-class output layer.
What you can do after
Apply it via a PyTorch accuracy loader to compare model predictions with target labels across datasets.
How it connects
Connects classification fine-tuning with argmax, softmax, cross-entropy loss, pretrained models, and training, validation, 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 Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.