Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Benchmarks and contamination

After this lesson: I can check a benchmark for contamination with n-gram overlap.

60 min lesson3 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Top marks on a test it had seen

A new model scores 95% on a well-known set of maths questions. A week later, someone finds those questions, with answers, on web pages the model may have read.

Was it reasoning or remembering? You will check a test set against training text, the way evaluation teams do.

By the end of this lesson

Note

I can check a benchmark for contamination with n-gram overlap.

Key words

WordWhat it meansExample
Benchmarka fixed set of questions with answers, used to compare models
Saturationwhen top models all score near full marks
Contaminationtest questions appearing in the training data
N-grama run of n words in a rowleaves at noon
Overlapthe share of a question's n-grams found in training text
Held-out setquestions kept private and never published

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Benchmarks compare only under the same rules

A benchmark gives every model the same questions and counts right answers. Scores compare only when the prompts, the number of tries and the marking are the same.

A small set is noisy: one question in 50 is 2 points. And once top models all score near full marks, the set has reached saturation and no longer tells them apart.

Worked example: what a score difference means

QuestionsModel A rightModel B rightGap
5045442 points, one question
1,0009008802 points, 20 questions
500, near saturation496495Too small to mean much

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Overlap finds questions the model has read

If test questions were in the corpus, a high score may measure memory, not skill. That is contamination.

A common check splits each question into n-grams of words and looks each one up in the training text. The overlap is the share found. A question with a high overlap is flagged and its score reported apart.

Try it: n-grams and overlap

Code: python
def ngrams(text, n):
    words = text.split()
    grams = []
    for k in range(len(words) - n + 1):
        grams.append(" ".join(words[k:k + n]))
    return grams

training = "the bus to the park leaves at noon every day"
question = "a train to the park leaves at noon"

seen = ngrams(training, 3)
mine = ngrams(question, 3)
shared = 0
for g in mine:
    if g in seen:
        shared = shared + 1
        print("seen:", g)
print("share", round(shared / len(mine), 2))
# Change the 3 to 5 in both places. What share do you get now?

Make a free account to save your work and track your progress.

Write the contamination script into the lab

Command
docker exec jupyter sh -c "printf '%s\n' 'corpus = \"\"\"a train leaves the city at eight and reaches the coast at ten so the journey takes two hours' 'quiz night question seven a shop sells pens at 40 pence each how much do five pens cost the answer is 2 pounds' 'the canteen sells soup for 2 pounds and bread for 50 pence' 'quiz night question nine if a bus leaves at nine and the trip takes 45 minutes when does it arrive the answer is 9 45' 'the library opens at nine and shuts at six on weekdays\"\"\"' 'questions = [' '    \"a shop sells pens at 40 pence each how much do five pens cost\",' '    \"a shop sells rulers at 30 pence each how much do four rulers cost\",' '    \"if a bus leaves at nine and the trip takes 45 minutes when does it arrive\",' '    \"if the bus leaves at nine and the journey takes 45 minutes what time does it get there\",' > steps/contam.py"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Finish writing the script

Command
docker exec jupyter sh -c "printf '%s\n' '    \"a train leaves at eight and the journey takes two hours when does it arrive\",' '    \"the gym opens at seven and shuts at ten how many hours is it open\",' ']' 'right = [1, 0, 1, 1, 0, 1]' 'N = 5' 'def grams(text):' '    w = text.split()' '    return [\" \".join(w[k:k + N]) for k in range(len(w) - N + 1)]' 'seen = set(grams(corpus.replace(chr(10), \" \")))' 'flags = []' 'for k, q in enumerate(questions):' '    g = grams(q)' '    share = sum(1 for x in g if x in seen) / len(g)' '    flags.append(share >= 0.5)' '    print(\"question\", k + 1, \"share seen\", round(share, 2), \"FLAGGED\" if flags[-1] else \"\")' 'clean = [r for r, f in zip(right, flags) if not f]' 'print(\"score on all\", sum(right), \"of\", len(right), \"score on clean\", sum(clean), \"of\", len(clean))' >> steps/contam.py"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check six questions against the training text

Command
docker exec jupyter python3 steps/contam.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What contam.py printed, with 5-grams

QuestionShare seenFlaggedModel right
1 pens at 40 pence1.0YesYes
2 rulers at 30 pence0.1NoNo
3 bus at nine, 45 minutes1.0YesYes
4 the same, reworded0.14NoYes
5 train at eight0.09NoNo
6 gym hours0.0NoYes

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think a low overlap proves a question was never seen. Question 4 was the same question reworded and shared only 0.14. N-gram checks catch copies, not paraphrases, so treat a clean result as a lower limit on contamination.

Keeping a benchmark honest

Teams report the score on flagged and clean questions apart. They keep a held-out set private, so it cannot be published and read. They also write new questions after the training data was collected.

None of this is perfect. A score is evidence to weigh, the same as in module 85.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Assignment: a contamination check

Build the contamination check from the lesson.

ngrams(text, n) lists every run of n words, joined with spaces. overlap(question, corpus, n) is the share of the question's n-grams found in the corpus. flagged(questions, corpus, n, threshold) returns the positions of questions at or above the threshold. clean_score(results, flags) is the share right among the questions not flagged.

Code: python
def ngrams(text, n):
    # Every run of n words in a row, each joined with one space.
    return []


def overlap(question, corpus, n):
    # The share of the n-grams of the question that also appear in the corpus.
    # 0 if the question has no n-grams.
    return 0


def flagged(questions, corpus, n, threshold):
    # The positions of the questions whose overlap is at least the threshold.
    return []


def clean_score(results, flags):
    # results holds 1 or 0 per question; flags is a list of positions.
    # The share of 1s among the questions not flagged, or 0 if none are left.
    return 0

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.