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
| Word | What it means | Example |
|---|---|---|
| Benchmark | a fixed set of questions with answers, used to compare models | |
| Saturation | when top models all score near full marks | |
| Contamination | test questions appearing in the training data | |
| N-gram | a run of n words in a row | leaves at noon |
| Overlap | the share of a question's n-grams found in training text | |
| Held-out set | questions kept private and never published |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsBenchmarks 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
| Questions | Model A right | Model B right | Gap |
|---|---|---|---|
| 50 | 45 | 44 | 2 points, one question |
| 1,000 | 900 | 880 | 2 points, 20 questions |
| 500, near saturation | 496 | 495 | Too small to mean much |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsOverlap 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
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
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
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
docker exec jupyter python3 steps/contam.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
What contam.py printed, with 5-grams
| Question | Share seen | Flagged | Model right |
|---|---|---|---|
| 1 pens at 40 pence | 1.0 | Yes | Yes |
| 2 rulers at 30 pence | 0.1 | No | No |
| 3 bus at nine, 45 minutes | 1.0 | Yes | Yes |
| 4 the same, reworded | 0.14 | No | Yes |
| 5 train at eight | 0.09 | No | No |
| 6 gym hours | 0.0 | No | Yes |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsA 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 questionsAssignment: 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.
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