AI Hints
Home›Fine-Tuning LLMs Tutorial›Data: Cleaning, Splits and Size
Fine-Tuning LLMs · Lesson 74 of 120

Splits and Leakage

Keep the test set honest.

⏱ 2 min read▶ Video at 1:53:41🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

A test set only works if the model has never seen it. When test examples sneak into the training data, your results look better than they really are. This is called data leakage.

  • The test set must be unseen
  • When it leaks in
  • Results look too good

Hunt for leaks

Let's check our own data. The key function normalises a message. Lower case, no exclamation marks or full stops, and single spaces. We count unique training messages. Then we look for test messages, whose key also appears in training. Run it. Eight training messages were near duplicates. And four test messages also appear in the training data. Our test set leaks a little. We should remove those four, before trusting our score.

app.pypython
from datasets import load_dataset

data = load_dataset("json", data_files={"train": "tickets_train.jsonl",
                                        "test": "tickets_test.jsonl"})
def key(text):
    return " ".join(text.lower().replace("!", "").replace(".", "").split())

train_keys = [key(m) for m in data["train"]["message"]]
print("Train messages:        ", len(train_keys))
print("Unique after cleaning: ", len(set(train_keys)))
leaks = [m for m in data["test"]["message"] if key(m) in set(train_keys)]
print("Test messages also in train:", len(leaks))
for m in leaks[:3]:
    print("  ", m)
Output
Train messages:         527
Unique after cleaning:  519
Test messages also in train: 4
   please refund dune, i changed my mind. cheers
   Hi, Is Beloved on sale at the moment?
   Is Beloved available in the Bath shop?

Avoiding leakage

How do you avoid leakage? Remove duplicates before you split. Split by something bigger than one example, when it makes sense. For example, by customer, or by date. Then no customer appears in both sets. And keep a final test set, that you never look at until the very end.

keyvalue
Dedupbefore splitting
Split by groupcustomer, or date
Final test setuse it once, at the end
Nevertune on the test set

Summary

Let's recap. Leakage means test examples sneak into training. It makes scores look too good. Normalise and compare messages to find it. And dedup before splitting. Next, how much data you actually need.

Key takeaways
  • Leakage = scores too good
  • Normalise and compare
  • Dedup before splitting
Next up: How Much Data? — enough, but not too much