Back to section lessons
20 min · medium · Lesson 8 of 8

Evaluation, checkpoints and the final CLI

Compare the model with a baseline, save state_dict safely, and run validate → train → evaluate → predict end to end.

Lesson contents
  1. 1 Accuracy and macro F1
  2. 2 Confusion matrix and error analysis
  3. 3 What a checkpoint stores
  4. 4 Category and score
  5. 5 Task
  6. 6 Running the full project
  7. 7 Common mistake
  8. 8 What's next

Accuracy and macro F1

Accuracy is the fraction of correct answers. If one category appears far more often than the others, accuracy alone isn't enough: a model can ignore rare classes and still achieve a substantial value.

For one category, precision is the fraction of correct predictions among those the model assigned to that category. Recall is the fraction of examples found among all actual examples in that category. F1 combines precision and recall: a high score requires both to be good.

Macro F1 calculates F1 for each category, then averages the values with equal weight. This keeps a rare category from disappearing behind the large number of examples in a common one. Equal weights for class F1 scores don't mean each individual error has the same effect.

For the course acceptance checks, both validation metrics must exceed the majority baseline by at least 0.10. If they don't reach the threshold, the experiment is unsuccessful. Don't adjust the threshold after the run to make the metrics pass.

Confusion matrix and error analysis

A confusion matrix counts "actual category → predicted category" pairs. It shows which classes the model confuses:

                 predicted
actual           avail perf conn cap
availability        8    1    0   0
performance         2    6    0   1
connectivity        0    1    7   0
capacity            0    2    0   6

These numbers illustrate the format; they don't promise a project result. The corpus is synthetic, so this evaluation doesn't establish quality on real logs.

Use validation to choose settings. Run test after fixing the model, seed and configuration, then save the result with the dataset hash.

What a checkpoint stores

A checkpoint contains the model's state_dict and simple configuration: the vocabulary, label list, embedding size and maximum length. You don't need to save executable objects or arbitrary classes.

Always load onto the CPU:

payload = torch.load(path, map_location="cpu", weights_only=True)
model.load_state_dict(payload["state_dict"])
model.eval()

weights_only=True reduces the attack surface when reading a PyTorch file. Still, load checkpoints only from trusted sources and validate their structure before passing data to the model.

Category and score

The predict command returns a category and a score between 0 and 1. Here, score is the model's highest softmax score. It is not the probability of a correct diagnosis and doesn't establish answer quality.

The course classifier chooses a category. It doesn't run commands, change infrastructure or replace incident investigation.

Task

Implement the following in exercises/lesson08.py:

def checkpoint_roundtrip(path, model, vocab, labels, max_length):
    ...

The function uses the existing save_checkpoint and load_checkpoint from incident_classifier.checkpoint, then returns the loaded model. To save it, create a TrainingConfig with the supplied max_length and the original model's embedding size. The exercise's dataset_hash is the string lesson08.

Requirements:

Hints

Look at the signatures of save_checkpoint and load_checkpoint. The first function takes the model, vocab, labels, config, and a named dataset_hash.

load_checkpoint returns five values. This exercise needs the first item, the restored model. Structure validation and the eval() call are already part of the project's shared safe loader.

Checking the exercise

python -m pytest -q tests/test_exercises.py --student exercises -k lesson08
Where to find the solution

The reference implementation is in solutions/lesson08.py. Look separately at which fields it checks before loading weights into the model.

python -m pytest -q tests/test_exercises.py --student solutions -k lesson08

Running the full project

First, validate the data and train the model with a fixed seed:

python -m incident_classifier validate data/incidents.jsonl
python -m incident_classifier train --data data/incidents.jsonl --output artifacts/model.pt --seed 42

Use only validation while choosing settings:

python -m incident_classifier evaluate --data data/incidents.jsonl --checkpoint artifacts/model.pt --split validation

After fixing your approach, run one final test evaluation:

python -m incident_classifier evaluate --data data/incidents.jsonl --checkpoint artifacts/model.pt --split test

Check an external example that isn't in the corpus:

python -m incident_classifier predict --checkpoint artifacts/model.pt 'После обновления выросло время ответа'

The output must contain category and score. Describe score only as a softmax score.

Finish with the complete test suite:

python -m pytest -q
python -m pytest -q tests/test_exercises.py --student solutions

After the initial installation, the project's required commands work offline.

Common mistake

If you keep changing the model after viewing test results, test becomes another validation set. Fix the configuration and seed before the first final run.

What's next

The free track ends with a working local classifier and tests. A continuation covering attention, a tiny Transformer, a structured assistant and fine-tuning an open model is in development. Pricing and dates have not been set. The current continuation outline is on the course page.

Done reading?

Mark the lesson as read to move your track progress forward.

Practice

Fix a local lab. The agent checks your work and gives hints if you get stuck.

Full access to all labs

$134 $67.00 for one year −50%

One payment, no auto-renewal · Terms

Buy for $67.00