Datasets
The datasets library ships with Transformers: pull Hub data, tokenize with map, then feed Trainer or a DataLoader. This chapter uses public rotten_tomatoes (binary movie reviews), shared with Fine-tuning.
load_dataset
Typical shape:
label is 0 / 1 (negative / positive). A slice such as load_dataset("rotten_tomatoes", split="train[:500]") is enough for a CPU smoke test.
Other sources: Hub org/name, local data/*.json, CSV. Gated datasets need hf auth login and a click-through on the website.
Features and labels
Before you write a training script, confirm the text column (text here) and the label column (label). Those two names are the usual copy-paste bugs when you switch corpora.
map: batched tokenization
Tips:
batched=True: many rows per call—much faster than a Python loop.truncation=True: clip tomodel_max_lengthso long reviews do not OOM.- Leave dynamic padding to a DataCollator; avoid
padding="max_length"on the whole set unless you truly want a fixed length. Trainerlooks for alabelscolumn. Some versions also acceptlabel; renaming is safer.
DataCollator: build batches
DataCollatorWithPadding pads to the longest sequence in the current batch, which is cheaper than a global max length. Generation jobs often use DataCollatorForLanguageModeling or DataCollatorForSeq2Seq.
[batch, seq] tensor shapes are covered in the PyTorch tutorial.
Format, cache, and small slices
map caches on disk. After you change the function, pass load_from_cache_file=False or a new cache_file_name. Do not compute metrics inside map—use evaluate in Trainer’s compute_metrics (next chapter).
Dataset pages on the Hub
Every dataset has a card (e.g. rotten_tomatoes): license, fields, citation. Read the license before production; academic-only corpora do not belong in a commercial product.
You can ds.push_to_hub("your-name/my-reviews") to publish a preprocessed copy (login required).