Introduction
Real datasets don't fit neatly into one tensor in memory: millions of image files on disk, huge text corpora, records in a database. Training needs a steady stream of mini-batches, shuffled each epoch, preprocessed on the fly, and delivered fast enough that the GPU never waits.
PyTorch splits this job into two simple pieces:
- a
Datasetknows how to get one example (and how many there are); - a
DataLoaderturns a dataset into shuffled mini-batches, optionally loading them in parallel.
Think of a library: the Dataset is the catalogue that can fetch any single book by its number; the DataLoader is the librarian who brings you books in bundles, in random order, with helpers fetching the next bundle while you read.