Evaluation: Is the Model Any Good?

You cannot improve what you do not measure. Ask the model questions with known answers, compare fairly, and count.

AI · beginner · 7 min · 19 steps · free

What you'll learn

evaluation.py

# How good is a model? Ask it questions with known answers, and count.
tests = [("capital of france", "paris"), ("2 + 2", "4"), ("largest planet", "jupiter"), ("author of hamlet", "shakespeare")]
model_says = {"capital of france": "Paris", "2 + 2": "4", "largest planet": "Saturn", "author of hamlet": "Shakespeare "}
questions = [q for q, _ in tests]

results = []
for question, expected in tests:
    answer = model_says[question].strip().lower()   # compare fairly: ignore case and spaces
    results.append(1 if answer == expected else 0)

accuracy = sum(results) / len(results)
print("accuracy:", accuracy)

Step by step

  1. A test set: questions paired with known right answers.
  2. What the model answered (saved earlier). Note "Paris" with a capital, "Saturn", and "Shakespeare " with a trailing space.
  3. Just the questions, for the chart.
  4. One score per question.
  5. question is created with the value 'capital of france'. expected is created with the value 'paris'.
  6. "Paris" → "paris" after .strip().lower().
  7. It matches: 1.
  8. question changes from 'capital of france' to '2 + 2'. expected changes from 'paris' to '4'.
  9. answer changes from 'paris' to '4'.
  10. "4" matches: 1.
  11. question changes from '2 + 2' to 'largest planet'. expected changes from '4' to 'jupiter'.
  12. answer changes from '4' to 'saturn'.
  13. "saturn" is not "jupiter": 0.
  14. question changes from 'largest planet' to 'author of hamlet'. expected changes from 'jupiter' to 'shakespeare'.
  15. "Shakespeare " becomes "shakespeare".
  16. 1 is added to the end of the list that results points to.
  17. Python moves to the next item of the loop: for question, expected in tests.
  18. Accuracy: 3 right out of 4.
  19. Accuracy 0.75. Real evals have hundreds of questions, and for open-ended answers they often use another model as a judge, checked against people's judgements.

Common mistakes

Best practices

Key takeaways

Check yourself

A model gets 18 of 24 test questions right. What is its accuracy?

Answer: 0.75. 18 ÷ 24 = 0.75.

You change your prompt. How do you know if it helped?

Answer: Rerun the same eval and compare. Same test set, before and after: the numbers show the effect.

·

More in Building with Models