Please enable JavaScript to use CodeHS

AI Engineer

Measure what matters

A good model is not just low loss. This tutorial covers the metrics recruiters ask about: perplexity, BLEU, ROUGE, HumanEval, and safety.

AI ~15 min Programmer
Vocabulary

Key terms for this tutorial

Here are the words that matter most in this tutorial. Come back here if a term feels fuzzy.

accuracy
The fraction of predictions that are correct. It can be misleading when classes are imbalanced.
loss
A number that measures how wrong the model is. Training tries to minimize loss.
model
A function with learnable parameters that maps inputs to outputs.
token
A unit of text (word, sub-word, or character) that a language model processes.
BLEU
A score that measures how similar a generated text is to a reference translation.
HumanEval
A benchmark where models are asked to solve coding problems. A pass@k score measures how often one of k samples is correct.
perplexity
A measure of how surprised a language model is by real text. Lower is better.
precision
Of the examples the model predicts as positive, how many actually are.

Intrinsic

Perplexity

Perplexity is exp(cross_entropy). It measures how surprised the model is by each token. Lower is better, but perplexity does not tell you whether the model is useful or safe.

perplexity_eval.py
import math
losses = [2.3, 2.1, 1.9, 2.0]
avg = sum(losses) / len(losses)
print('perplexity:', math.exp(avg))
Output
perplexity: 7.788007830714049

Line-by-line: perplexity_eval.py

Read this slowly. Each line does one small, concrete thing.

  1. import math — Import the math library so we can use its built-in tools.
  2. print('perplexity:', math.exp(avg)) — Print a result so we can see what the model learned.

Pop-in: Read the code one block at a time

Industry code imports many libraries. Focus on the sequence: prepare data, define the model, run the training loop, and evaluate. The rest is setup.

Stuck or curious? Explain this to me on ChatGPT

Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Evaluation and Benchmarks'. Explain the section 'Perplexity' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.


NLP

BLEU and ROUGE

BLEU counts n-gram overlap with reference text and is common in translation. ROUGE is the equivalent for summarisation, focusing on recall of reference tokens.

rouge.py
from collections import Counter

def unigram_f1(pred, ref):
    pred_counts = Counter(pred.split())
    ref_counts = Counter(ref.split())
    overlap = sum((pred_counts & ref_counts).values())
    precision = overlap / len(pred.split())
    recall = overlap / len(ref.split())
    f1 = 2 * precision * recall / (precision + recall + 1e-8)
    return f1

print('unigram F1:', unigram_f1('the cat sat', 'the cat sat on the mat'))
Output
unigram F1: 0.7499999812500047

Line-by-line: rouge.py

Read this slowly. Each line does one small, concrete thing.

  1. from collections import Counter — Import tools from the collections package.
  2. def unigram_f1(pred, ref): — Define the unigram_f1 function, which describes a step in the data or model pipeline.
  3. pred_counts = Counter(pred.split()) — Prepare a dataset by splitting or formatting raw examples.
  4. ref_counts = Counter(ref.split()) — Prepare a dataset by splitting or formatting raw examples.
  5. precision = overlap / len(pred.split()) — Prepare a dataset by splitting or formatting raw examples.
  6. recall = overlap / len(ref.split()) — Prepare a dataset by splitting or formatting raw examples.
  7. print('unigram F1:', unigram_f1('the cat sat', 'the cat sat on the mat')) — Print a result so we can see what the model learned.

Pop-in: Read the code one block at a time

Industry code imports many libraries. Focus on the sequence: prepare data, define the model, run the training loop, and evaluate. The rest is setup.

Stuck or curious? Explain this to me on ChatGPT

Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Evaluation and Benchmarks'. Explain the section 'BLEU and ROUGE' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.


Code

HumanEval and safety

HumanEval tests code generation by running generated functions against unit tests. Safety evaluation uses red-teaming prompts and classifiers to check for toxicity, bias, and harmful instructions.

No single metric captures a model. Always combine automatic metrics with human evaluation and domain-specific tests.

Stuck or curious? Explain this to me on ChatGPT

Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Evaluation and Benchmarks'. Explain the section 'HumanEval and safety' in plain English. Include simple analogies, vocabulary definitions, and a short example.


Check yourself

Check yourself

?

Perplexity limitations

Why is low perplexity not enough?

A model can have low perplexity but still be unhelpful, biased, unsafe, or unable to follow instructions. Evaluation needs task-specific metrics and human judgement.

Stuck or curious? Explain this to me on ChatGPT

Prompt: I am a beginner AI engineer studying 'AI Engineer: Evaluation and Benchmarks'. The practice question 'Perplexity limitations' asks: Why is low perplexity not enough? The answer is: A model can have low perplexity but still be unhelpful, biased, unsafe, or unable to follow instructions. Evaluation needs task-specific metrics and human judgement.. Explain the concept behind this in simple terms.

Stuck or curious? Explain this to me on ChatGPT

Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Evaluation and Benchmarks'. Explain the section 'Check yourself' in plain English. Include simple analogies, vocabulary definitions, and a short example.


Quick quiz

Three questions

1. What does perplexity measure?

Stuck or curious? Explain this question to me on ChatGPT

Prompt: I am a beginner AI engineer studying 'AI Engineer: Evaluation and Benchmarks'. The quiz asks: What does perplexity measure? The correct answer is: How surprised the model is by each token. Explain why that answer is right and why the other choices are wrong, using a simple example.

2. When is BLEU used?

Stuck or curious? Explain this question to me on ChatGPT

Prompt: I am a beginner AI engineer studying 'AI Engineer: Evaluation and Benchmarks'. The quiz asks: When is BLEU used? The correct answer is: Translation and text generation quality against references. Explain why that answer is right and why the other choices are wrong, using a simple example.

3. Why combine metrics?

Stuck or curious? Explain this question to me on ChatGPT

Prompt: I am a beginner AI engineer studying 'AI Engineer: Evaluation and Benchmarks'. The quiz asks: Why combine metrics? The correct answer is: No single score captures usefulness, safety, and correctness. Explain why that answer is right and why the other choices are wrong, using a simple example.

Keep learning

Next: MLOps, experiment tracking, and deployment.

Part of the CodeHS tutorial library.