AI Hints
Home›Fine-Tuning LLMs Tutorial›Data: Balance, Safety and Practice
Fine-Tuning LLMs · Lesson 81 of 120

Data Practice

Check a dataset yourself.

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

Let's put the data lessons together. We'll write one small script that checks every row, of both splits, before training.

  • Put it all together
  • One checking script
  • Every row, both splits

A data check

Here's our checker. For each split, we count problems. An intent we don't know is a problem. An empty message is a problem. And the book in the answer should appear in the message. Run it. A hundred and one problems! Let's look closer.

app.pypython
from datasets import load_dataset

INTENTS = {"order_status", "return", "price", "stock", "complaint"}
data = load_dataset("json", data_files={"train": "tickets_train.jsonl",
                                        "test": "tickets_test.jsonl"})
for split, rows in data.items():
    problems = 0
    for row in rows:
        if row["intent"] not in INTENTS: problems += 1
        if not row["message"].strip(): problems += 1
        if row["book"] and row["book"] not in row["message"]: problems += 1
    print(f"{split:5} {len(rows):4} rows  {problems} problems")
Output
train  527 rows  101 problems
test    77 rows  15 problems

Fixing the check

Many customers type in lower case, like persuasion, with a small p. Our check compared the exact capital letters, so it was too strict. The data was fine. The check was wrong. We compare lower case versions instead. Run it. Zero problems, in both splits.

app.pypython
from datasets import load_dataset

INTENTS = {"order_status", "return", "price", "stock", "complaint"}
data = load_dataset("json", data_files={"train": "tickets_train.jsonl",
                                        "test": "tickets_test.jsonl"})
for split, rows in data.items():
    problems = 0
    for row in rows:
        if row["intent"] not in INTENTS: problems += 1
        if not row["message"].strip(): problems += 1
        if row["book"] and row["book"].lower() not in row["message"].lower(): problems += 1
    print(f"{split:5} {len(rows):4} rows  {problems} problems")
Output
train  527 rows  0 problems
test    77 rows  0 problems

Your turn

Now try it yourself. Add a check that every message is shorter than three hundred characters. Add a check for duplicate messages. And run the privacy scan on the test split too.

1messages under 300 characters
2no duplicate messages
3privacy scan on the test split

Summary

Let's recap. Write one script that checks every row. When it finds problems, ask if the data is wrong, or the check. And run it every time the data changes. Next, how to measure a fine-tuned model.

Key takeaways
  • Check every row
  • Data wrong, or the check?
  • Run it every time
Next up: Why Evaluation Is Hard — measuring free text