Evaluating Generative Models

Evaluación de modelos generativos

Desarrollo y Evaluación de Modelos Propios

Especialización en IA Generativa y Desarrollo de Negocios

Today

Agenda · 3 hours

  1. Warm-up: three questions on fine-tuning (S04) 10'
  2. Theory: text metrics, human and LLM judges, classification metrics, validation, KPIs 60'
  3. Break 10'
  4. Guided lab: perplexity, BLEU, chrF, ROUGE, k-fold and error analysis 60'
  5. Session challenge (graded, individual) 40'

Project milestone H2: your group's proposal canvas is due Wednesday 14 Oct, 23:59.

Warm-up

Three questions on last session: LoRA, overfitting and the test set.

Warm-up · 10'
Warm-up · Q1

With LoRA, a weight matrix becomes \(W = W_0 + BA\). During fine-tuning, what is trained?

\(B\) is \(d \times r\) and \(A\) is \(r \times k\) with a small rank \(r\), so each adapted matrix trains \(r(d + k)\) parameters instead of \(d \cdot k\). The pretrained \(W_0\) is untouched, which is why adapters are small files and several can share one base model.
Warm-up · Q2

Fine-tuning on 200 examples: training loss keeps falling, but validation loss rises after epoch 2. What is happening?

The model is memorising the 200 training examples. Early stopping on the validation loss, fewer epochs, weight decay, LoRA dropout or more (and more varied) data all help. Today we make "validation" precise: how to split, how many folds, and what to keep sealed.
Warm-up · Q3

In the S04 challenge, the AI-proposed TrainingArguments picked the best epoch by looking at the test set. Why is that a problem?

Every decision you take while looking at a dataset tunes the model to it. Choose with a validation set (or cross-validation) and open the test set once, at the end. That rule is today's backbone.

Learning objectives

By the end of this session you can…

  1. Explain why generated text is hard to evaluate and separate intrinsic from extrinsic evaluation RAE 2
  2. Compute and interpret perplexity, BLEU, ROUGE, chrF and embedding similarity, and name what each misses RAE 2
  3. Design a human or LLM-as-a-judge evaluation that controls known biases RAE 2
  4. Choose classification metrics and a validation scheme without leakage RAE 2
  5. Link an offline metric to a business KPI and a go/no-go threshold RAE 2

RAE 2: evaluate trained models with standard metrics and validation methods (course syllabus).

Part 1

Why is generation hard to evaluate?

Many right answers, many ways to be wrong, and a model's own measure of surprise.

Theory · 60'

The core difficulty

One input, many right answers

Input: a 6-minute call in which an Andes Bank customer reports a card charged twice at a supermarket and asks for a refund.

Three good summaries

  1. Customer reports a duplicate supermarket charge and requests a refund.
  2. Double card payment at a supermarket; refund requested.
  3. Client was billed twice for one purchase and wants the money back.

One fluent, wrong summary

Customer reports a duplicate supermarket charge; the refund was already approved.

It shares most words with summary 1, yet it invents a commitment the bank never made.

No single reference: good outputs differ in words and order.
Many dimensions: fluency, faithfulness, coverage, tone, safety.
Humans disagree too, and their time is expensive.

A map of evaluation

Intrinsic vs extrinsic evaluation

Intrinsic: the output itself

  • Reference-free: perplexity, an LLM judge, human ratings
  • Lexical overlap with a reference: BLEU, ROUGE, chrF
  • Semantic similarity to a reference: embeddings, BERTScore
  • Task metrics for labels: precision, recall, F1

Extrinsic: the effect it causes

  • Did the agent resolve the case faster?
  • Did fewer customers call back?
  • Did fraud losses or review hours go down?
  • Measured offline (a simulation, a pilot) or online (A/B test, shadow mode)
Rule of thumb: intrinsic metrics are cheap and fast, so you use them to iterate; extrinsic ones are what the business pays for, so you use them to decide.

Reference-free · perplexity

Perplexity: how surprised is the model?

For a text of \(N\) tokens, perplexity is the exponential of the average negative log-likelihood:

$$ \text{PPL} = \exp\Big(-\frac{1}{N}\sum_{t=1}^{N} \log p(x_t \mid x_{<t})\Big) $$

Worked example

The model gives the three tokens probabilities \(\tfrac12, \tfrac14, \tfrac18\).

Mean NLL \(= \tfrac13(\ln 2 + \ln 4 + \ln 8) = \ln 4\) → \(\text{PPL} = 4\).

As uncertain as a fair choice among 4 tokens at every step.

What it is good for

  • Tracking training and validation loss (it is \(e^{\text{loss}}\))
  • Comparing models that share a tokenizer on the same text
  • Spotting out-of-domain text (tweets, another language)

Introduced for speech recognition by Jelinek et al. (1977); see Jurafsky & Martin, SLP3, ch. 3.

Reference-free · a trap

Perplexity depends on the tokenizer

GPT-2 on a short textTokens / wordPPL per tokenPPL per word
News paragraph (English)1.1042.863.4
Same news, words shuffled1.103,489.98,163.2
Informal tweet (English)1.43315.03,841.6
News paragraph (Spanish)2.02141.322,538.0
Computed in today's lab with openai-community/gpt2 on four short texts written for class. PPL per word = exp(total NLL ÷ number of words).
Not comparable across tokenizers: a model that cuts words into more, easier pieces gets a lower per-token PPL.
Compare fairly: same text, same tokenizer; or normalise per word, character or byte; or use a task metric.
Check · perplexity

On the same Spanish test set, model A reports perplexity 18 and model B reports 27. They use different tokenizers. Which one models Spanish better?

Per-token perplexity averages over different numbers of tokens for the same text. Convert to a shared unit (e.g., bits per byte or per word) or, better for a business decision, compare both models on the task you care about. This exact trap is one of the planted errors in today's challenge.

Buying on benchmarks

A leaderboard is not your test set

Andes Bank (fictional) must pick a model for a Spanish-language customer chat. Two vendors send impressive scores: one a low perplexity, the other a top rank on a public English benchmark.

Neither number was measured on Andes Bank's customers, language or task.

Ask before you believe a score

  • Which data? Language, domain, date. Could the model have seen it during training (contamination)?
  • Which unit and settings? Tokenizer, prompt, decoding, number of examples.
  • Against what baseline? The current process, a simple rule, the cheaper model.

Build a small private evaluation set from real, anonymised cases and run every candidate on it.

Part 2

Comparing with a reference, and beyond

BLEU, ROUGE, chrF, embeddings, human raters and LLM judges.

Reference-based · translation

BLEU: clipped n-gram precision + brevity penalty

1 · Clipped precision \(p_n\): each candidate n-gram counts at most as many times as it appears in the reference.

2 · Brevity penalty for a candidate of length \(c\) and reference length \(r\):

$$ \text{BP} = \begin{cases} 1 & c > r \\ e^{\,1 - r/c} & c \le r \end{cases} $$

3 · Geometric mean of \(p_1 \dots p_4\):

$$ \text{BLEU} = \text{BP} \cdot \exp\Big(\sum_{n=1}^{4} \tfrac14 \log p_n\Big) $$

Why clip? (Papineni et al., 2002)

Reference: the cat is on the mat
Candidate: the the the the the the the

Plain unigram precision: 7/7. Clipped: 2/7, because "the" appears only twice in the reference.

BLEU is precision-based; recall is only approximated by the brevity penalty. Corpus BLEU pools counts over all sentences.

Reference-based · summaries and morphology

ROUGE looks at recall, chrF at characters

ROUGE (Lin, 2004)

ROUGE-N recall: the share of the reference's n-grams that the candidate covers (ROUGE-1 unigrams, ROUGE-2 bigrams).

ROUGE-L: longest common subsequence (LCS) of \(m\) reference and \(n\) candidate words; order matters, gaps are allowed.

\(R = \tfrac{\text{LCS}}{m}\) · \(P = \tfrac{\text{LCS}}{n}\) · \(F_1 = \tfrac{2PR}{P + R}\) (as in the rouge_score package)

chrF (Popović, 2015)

An F-score over character n-grams (up to 6 characters). Partial credit for aprobó vs aprobado, which share most characters.

\(F_\beta\) with \(\beta = 2\) in sacrebleu: recall counts more than precision.

Useful for morphologically rich languages such as Spanish.

Shared blind spot: all three reward surface overlap with one reference. None of them checks that the facts are right.

Simulator · BLEU & ROUGE calculator

Score a candidate against a reference

Candidate words found in the reference (clipped):

n-gramclipped matches / total\(p_n\)

Length \(c\) = – · \(r\) = – · BP = –

BLEU (0–100): –

PrecisionRecallF1
ROUGE-1–––
ROUGE-L–––

Lower-cased words, punctuation ignored. * Smoothing: the \(k\)-th order with 0 matches uses \(p_n = 1/(2^k \cdot \text{total})\) (sacrebleu's "exp" method).

Check · BLEU

Reference: “El banco aprobó ayer la transferencia del cliente.” Which candidate gets the higher sentence BLEU with sacrebleu?

In the lab: the negated sentence gets BLEU 70.7 (chrF 94.8), the paraphrase BLEU 23.5 (chrF 70.0). One inserted word keeps most n-grams; a reordering breaks them. Overlap is not meaning, so pair these metrics with a meaning check: embeddings, a judge or a person.

Reference-based · meaning

Embedding similarity and BERTScore

  • Sentence embeddings (e.g., all-MiniLM-L6-v2; Reimers & Gurevych, 2019): one vector per text; cosine similarity near 1 means "about the same thing".
  • BERTScore (Zhang et al., 2020): match every token to its most similar token in the other text using contextual embeddings, then compute precision, recall and F1.
  • Both reward paraphrases that n-gram metrics miss.
Pair (English)ROUGE-L F1Cosine
Paraphrase0.5000.668
Negation0.9330.801
Unrelated0.000−0.035
From the lab: MiniLM cosine and ROUGE-L on three hand-written pairs. The negation still scores high on both.
Still not a fact-checker: "was approved" and "was not approved" sit close in embedding space. Pick an embedding model that covers your language (MiniLM was trained mainly on English).

The reference standard

Human evaluation, designed properly

What to rate

Explicit criteria: fluency, faithfulness to the source, coverage, tone, safety. One question per criterion.

How to rate

Likert (1–5 per criterion) or pairwise (A vs B, which is better?). Pairwise is often easier for raters to answer consistently.

Who and how many

At least two raters on a shared sample; measure agreement (e.g., Cohen's κ); blind them to which system wrote what.

Guidelines with examples are the cheapest way to raise agreement: show a 1, a 3 and a 5 for each criterion before rating starts.

Scaling human judgment

LLM-as-a-judge: useful, but biased

A strong LLM grades one answer against a rubric, or compares two answers. Zheng et al. (2023) found that strong judges such as GPT-4 agreed with human preferences more than 80% of the time on their benchmarks, similar to the agreement between humans.

Known biases

  • Position: prefers the answer in a given slot
  • Verbosity: prefers longer answers
  • Self-enhancement: favours answers from its own model family
  • Limited reasoning when grading maths or logic

Mitigations

  • Swap A and B; accept a verdict only if both orders agree
  • Give a rubric and, when possible, a reference answer
  • Ask for a justification before the verdict
  • Use a judge from a different family than the system under test
  • Validate the judge on a sample with human ratings before trusting it

Self-check · name the problem

What went wrong in each evaluation?

The judge prefers whichever answer is shown first.
Position bias. Swap the order and require agreement.
The judge prefers the longer answer even when it adds nothing.
Verbosity bias. Rubric that rewards concision; control length.
A perfect paraphrase gets a low BLEU.
Lexical metric limit. Add chrF, embeddings or human review.
Two raters agree only about half of the time.
Unclear guidelines. Add examples per score; measure κ again.
The test sentences were also in the model's training data.
Contamination. The score is optimistic; use fresh, private data.
The summarizer wins on ROUGE but invents a refund that was never approved.
Faithfulness failure. Overlap ≠ truth; check facts with people or a rubric judge.

Beyond one number

Holistic evaluation (HELM)

Liang et al. (2022) evaluated many language models on many scenarios, and for each scenario reported seven kinds of metrics, not one:

Accuracy Calibration Robustness Fairness Bias Toxicity Efficiency

Lessons for your project

  • Report a small set of metrics that expose trade-offs (quality vs cost vs risk).
  • Evaluate every candidate under the same conditions (data, prompts, decoding).
  • Say what you did not measure.
Final project minimum: at least two justified metrics, one technical and one tied to the business or to risk, compared against a baseline on the same test set.

The metric your customer feels

Pick metrics that match the failure that hurts

NovaTel (fictional) machine-translates English support replies into Spanish. A wrong number or a negation flipped in a reply about charges triggers a complaint; a slightly unusual word order does not.

  • chrF to track quality over time; BLEU only next to a baseline
  • Weekly human review by bilingual agents, focused on numbers and negations
  • KPI: complaints and repeat contacts about translated replies
≈ 6.7 h

of reviewer time per round to rate 200 replies with 2 raters at about 1 minute each (200 × 2 × 1 min = 400 min; illustrative arithmetic).

Human evaluation has a budget: plan the sample size and how often you repeat it.

Part 3

When the output is a label

Precision, recall, thresholds, honest validation, error analysis and business KPIs.

Classification metrics

The confusion matrix and its ratios

FlaggedNot flagged
Actually positiveTPFN
Actually negativeFPTN

\(\text{Precision} = \dfrac{TP}{TP + FP}\) · \(\text{Recall} = \dfrac{TP}{TP + FN}\)

\(F_1 = \dfrac{2 \cdot P \cdot R}{P + R}\) · \(\text{Accuracy} = \dfrac{TP + TN}{\text{all}}\)

  • Precision: of what we flagged, how much was right? (cost of false alarms)
  • Recall: of what was there, how much did we catch? (cost of misses)
  • Accuracy paradox: if 1% of transactions are fraud, "never fraud" is 99% accurate with 0 recall.
  • Several classes: macro-F1 averages classes equally (small classes count); weighted-F1 follows class frequency.

Reference: Google, Machine Learning Crash Course, classification module.

Simulator · threshold explorer

Where do you put the threshold?

FlaggedNot flagged
FraudTP –FN –
LegitFP –TN –

Precision – · Recall – · F1 – · Accuracy –

Cost = FP + FN × ratio: – · lowest at –

40 synthetic transactions (12 fraud, 28 legit) with made-up model scores. Cost is in "false-alert units": a false alert costs 1.

Check · metrics

Andes Bank's fraud flagger: a missed fraud costs far more than a false alert, and the analyst team can review at most 300 alerts a day. What should drive the threshold?

Misses are expensive, so maximise recall, but alerts need people: the review capacity sets a floor on precision (or a cap on alerts). Accuracy hides the rare class. Write the rule down before looking at test results.

Validation

Holdout, k-fold and the sealed test set

Stratified 5-fold cross-validation on the training data:

Each fold keeps the class proportions (stratification). Report the mean and the spread across folds.

  • Holdout: one train/validation/test split. Simple, but one split can be lucky or unlucky.
  • k-fold: every example is validated once; use it to choose models and hyperparameters when data are scarce.
  • Test set: touched once, at the end, to confirm. If you tune on cross-validation, the CV score is optimistic: keep the test set (or use nested CV).

Kohavi (1995) recommended stratified 10-fold cross-validation for model selection.

Validation · the silent killer

Data leakage: when the test is not really unseen

LeakExampleFix
DuplicatesThe same template message appears in train and testDeduplicate before splitting
PreprocessingTF-IDF vocabulary or a scaler fitted on all the dataPut it inside a Pipeline
GroupsMessages from the same customer on both sidesGroup-aware split (GroupKFold)
TimeTraining on next month to predict last monthSplit by date
TuningChoosing the epoch or threshold on the test setDecide on validation; test once
ContaminationA public test set inside a pretrained model's training dataPrivate, fresh evaluation data

Leakage is a widespread cause of irreproducible results in applied ML (Kapoor & Narayanan, 2023).

Check · leakage

NovaTel trains an intent classifier on chat messages; many customers wrote ten or more. Messages were shuffled randomly into train and test. Test F1 is excellent, but in production it is much lower. Most likely cause?

The model partly memorised how each customer writes, and the test set rewarded that. Split by customer (group-aware split) so that test customers are new, as they are in production.

From numbers to fixes

Error analysis: read the mistakes

1 · Sample

20+ errors from the validation set, or the top confused pairs.

2 · Group

Ambiguous input, overlapping labels, missing vocabulary, label noise…

3 · Count

How many errors per group? Fix the biggest group first.

4 · Hypothesise

One cause and one change per group.

5 · Re-evaluate

On validation (CV); confirm on test once.

In today's lab (Banking77, 10 intents)

Top confusions include pending_transfer → transfer_not_received_by_recipient: "Why hasn't my transfer gone through yet?" could be either. That is a label-definition problem more than a model problem.

Slices: also break the metric down by segment (channel, language, product, customer type). An average can hide a group where the model fails.

From offline to business

Link each model metric to a KPI

ProductOffline metricOperational signalBusiness KPI
Call summarizerHuman faithfulness score; ROUGE vs a baselineMinutes of after-call workCost per call
FAQ botCorrect-answer rate (human or validated judge)Share of chats resolved without an agentCost to serve · satisfaction
Fraud flaggerRecall at the capacity-limited precisionAlerts reviewed per dayFraud losses · analyst hours
Reply translationchrF + human adequacy reviewReplies edited by agentsRepeat contacts · complaints
Go / no-go: fix the thresholds before you look at the test set (e.g., "beats the baseline on the offline metric and no critical errors in the human review sample"), then confirm online with a pilot or an A/B test.

Thresholds are business decisions

Put a price on each cell of the confusion matrix

In the threshold simulator, the cheapest threshold moved from 0.73 to 0.21 when a missed fraud went from costing the same as a false alert to costing ten times more.

Same model, same data: only the cost assumption changed.

Questions for the business owner

  • What does one false positive cost (time, customer friction)?
  • What does one false negative cost (losses, regulatory risk)?
  • Which capacity limits the decision (reviewers, budget)?
  • Who signs off the threshold, and when is it reviewed?

Apply it

Choose the metrics for four products

Evaluation plan in one line each · 7 min

  1. NovaTel call center: live EN→ES translation of chat replies.
  2. Andes Bank: summarizer of internal committee meetings.
  3. Banco Andino: FAQ bot on the public website.
  4. Andes Bank: fraud flagger for card transactions.

For each: one automatic metric, one human or judge check, one business KPI and one go/no-go threshold.

Rooms of 3–4, each room takes all four. All companies are fictional.

Each room reports one product in 30 seconds.

Break

Stretch, refill your coffee, and open Kaggle for the lab.

Hands-on

Guided lab

Four small models, five metrics, one confusion matrix, all on a free CPU.

Lab · 60'

Lab bridge

What we will build

1Perplexity of gpt2 on news, tweets and Spanish; same tokenizer vs different units12'
2EN→ES with opus-mt-en-es on opus_books: BLEU and chrF with sacrebleu12'
3Summaries with flan-t5-small on CNN/DailyMail: ROUGE, lead-3 baseline, MiniLM similarity15'
4Banking77 (10 intents): stratified 5-fold, confusion matrix, top confused pairs15'

Open the notebook

Kaggle checklist: Internet on · Accelerator none (CPU is enough today).

Open in Kaggle Colab

Session challenge

Individual, graded, 40 minutes in class. Submit by 23:59 today.

Challenge de la sesión · evaluado

Challenge S05: tus métricas, tu caso, tu plan

  1. Ejecución: ROUGE y similitud en tus 8 noticias (vs. lead-3), BLEU y chrF en tus 20 traducciones, validación cruzada con tu k.
  2. Análisis: encuentra un caso en el que la métrica y tu juicio no coinciden y explica por qué.
  3. Explica y decide: plan de evaluación para tu producto: métricas, evaluación humana, KPI y umbrales.
  4. Crítica a la IA: encuentra, explica y corrige 3 errores sobre métricas y validación.
  • Rúbrica: ejecución 40% · análisis 40% · reflexión y registro de IA 20%.
  • IA permitida con registro; micro-sustentaciones: si no explicas tu entrega, máximo 3.0.
  • Entrega: Archivo → Descargar notebook → e-Aulas como S05_<codigo>.ipynb, 23:59.

Wrap-up

Key takeaways

  • Perplexity measures surprise under a model; compare it only with the same text and tokenizer.
  • BLEU is precision with a brevity penalty, ROUGE is recall-oriented, chrF works on characters: all measure overlap, not truth.
  • Embeddings, human raters and validated LLM judges add meaning, at a cost and with biases.
  • Choose the threshold with explicit costs; report k-fold mean ± spread; open the test set once.
  • Every metric should point to a KPI and a go/no-go rule written in advance.

Exit question

For your final project: which failure would hurt the user most, and which metric would see it?

Next session: basic AI applications with Gradio and Startti, today at 10:00 after a short break. Its pre-work and your Startti licence were due before Saturday. Project: the group's H2 proposal canvas is due Wed 14 Oct, 23:59.

References

Sources for this session

  • Papineni, K., Roukos, S., Ward, T., & Zhu, W.-J. (2002). BLEU: A method for automatic evaluation of machine translation. ACL.
  • Lin, C.-Y. (2004). ROUGE: A package for automatic evaluation of summaries. Text Summarization Branches Out (ACL workshop).
  • Popović, M. (2015). chrF: Character n-gram F-score for automatic MT evaluation. WMT.
  • Post, M. (2018). A call for clarity in reporting BLEU scores. WMT.
  • Zhang, T., Kishore, V., Wu, F., Weinberger, K. Q., & Artzi, Y. (2020). BERTScore: Evaluating text generation with BERT. ICLR.
  • Reimers, N., & Gurevych, I. (2019). Sentence-BERT: Sentence embeddings using Siamese BERT-networks. EMNLP.
  • Zheng, L., et al. (2023). Judging LLM-as-a-judge with MT-Bench and Chatbot Arena. NeurIPS Datasets and Benchmarks.
  • Liang, P., et al. (2022). Holistic evaluation of language models. arXiv:2211.09110.
  • Jelinek, F., Mercer, R. L., Bahl, L. R., & Baker, J. K. (1977). Perplexity: A measure of the difficulty of speech recognition tasks. JASA, 62(S1), S63.
  • Jurafsky, D., & Martin, J. H. (2023). Speech and language processing (3rd ed. draft), ch. 3: N-gram language models.
  • Google. Machine Learning Crash Course: Classification (accuracy, precision, recall, thresholds).
  • Kohavi, R. (1995). A study of cross-validation and bootstrap for accuracy estimation and model selection. IJCAI.
  • Kapoor, S., & Narayanan, A. (2023). Leakage and the reproducibility crisis in machine-learning-based science. Patterns, 4(9).
  • See, A., Liu, P. J., & Manning, C. D. (2017). Get to the point: Summarization with pointer-generator networks. ACL.
  • Tiedemann, J., & Thottingal, S. (2020). OPUS-MT: Building open translation services for the world. EAMT.
  • Chung, H. W., et al. (2022). Scaling instruction-finetuned language models (FLAN-T5). arXiv:2210.11416.