Hugging Face, End to EndLesson 47 of 75

Module 5 · Datasets, and the work that happens before any model

From a dataset to batches a trainer can eat

In this lesson

  • 9 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 10 of 10 in this chapter

This chapter’s goalInspect, stream, filter and transform a dataset far larger than your RAM, query the Hub’s parquet conversion with SQL before downloading anything, run duplicate, leakage and class-balance checks on your own data, and publish it with a card stating provenance and licence.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:05Not available
Full audiobookNot availableNot available
What one instruction-tuning row has to containinput_idsThe whole conversation, rendered by the model’s ownchat templateattention_mask1 for real tokens, 0 for padding the collator addsper batchlabelsPlural. Named label instead and the loss is silentlynever computed.-100 across the promptLoss falls on the response only, so the model doesnot learn to write the questionsDecode a collated batch and read both the input and the unmasked labels before you start a long run. Adoubled beginning-of-sequence token, a chat template that did not render, and masking that covered thewrong span all show up here and nowhere else.
Decode a collated batch and read both the input and the unmasked labels before you start a long run. A doubled beginning-of-sequence token, a chat template that did not render, and masking that covered the wrong span all show up here and nowhere else.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

The three things the model needs

Whatever you are training, the loop wants tensors of the right shape with the right labels. For text that means: input_ids, attention_mask, and labels. Everything in this lesson is getting from a table of strings to those three.

Classification: labels are just a column

Python
def prep(batch):
    return tok(batch["text"], truncation=True, max_length=512)

ds = ds.map(prep, batched=True, remove_columns=["text"])
ds = ds.rename_column("label", "labels")
ds.set_format("torch")

Three specific points. The column must be called labels, plural, because that is what the model’s forward signature expects: label silently becomes an unused column and the loss is never computed. Do not pad here; padding at map time pads everything to 512 and wastes compute. And set_format("torch") makes rows come back as tensors without copying the underlying Arrow data.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘From a dataset to batches a trainer can eat’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/preparing-a-dataset-for-training (Accessed: 29 November 2026).