AI Hints
Home›Fine-Tuning LLMs Tutorial›Working with Datasets
Fine-Tuning LLMs · Lesson 25 of 120

The datasets Library

Loading training data.

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

Fine-tuning is mostly about data. The model learns only what your examples show it. So let's learn the tool we'll use to load, and prepare, every example. The datasets library.

  • Fine-tuning is about data
  • Examples teach the model
  • The datasets library

File formats

Training data usually lives in files. A very common format is JSON lines, with the ending dot j s o n l. Each line is one example, written as a small JSON object. It's easy to read, easy to add to, and works with any language. Our Riverside Books data uses it.

keyvalue
JSON Lines.jsonl
Each lineone example
Each examplea small JSON object
Our datatickets_train.jsonl

Load a dataset

Load dataset reads our file. JSON says the format. Split equals train gives us one dataset. Then we print the dataset, the first row, and the first five intents. Run it. Five hundred and twenty seven rows, with three columns. Message, intent, and book. A row is just a Python dictionary. And asking for a column gives us a list.

app.pypython
from datasets import load_dataset

ds = load_dataset("json", data_files="tickets_train.jsonl", split="train")
print(ds)
print(ds[0])
print(ds["intent"][:5])
Output
Dataset({
    features: ['message', 'intent', 'book'],
    num_rows: 527
})
{'message': 'Hello! The driver was rude when delivering Dune.', 'intent': 'complaint',
'book': 'Dune'}
['complaint', 'stock', 'return', 'return', 'price']

Why not just use pandas?

Why use this library, instead of a simple list or pandas? Because datasets can handle data far bigger than your memory. It saves results on disk, so repeated work is fast. And it plugs straight into the Hugging Face trainers. You can also turn any dataset into pandas, with to pandas, whenever you like.

keyvalue
Big datadoesn't need to fit in memory
Cachingrepeated work is fast
Trainersplug straight in
pandasds.to_pandas()

Summary

Let's recap. Training data often lives in JSON lines files. Load dataset reads them. A row is a dictionary, and a column is a list. Next, changing, and filtering, our data.

Key takeaways
  • .jsonl files
  • load_dataset()
  • rows and columns
Next up: map, filter, split — preparing data