liveBook · Manning

DataLoader

As explained in Deep Learning with PyTorch, Second Edition

A PyTorch abstraction that batches and loads samples from datasets.

Training one sample at a time can leave computing resources underused. PyTorch’s DataLoader takes samples from a Dataset, groups them into batches, and supplies those batches to a training or validation loop. The Dataset provides individual samples; the DataLoader handles sampling and collation. section 13.3.2 section 13.13

Three settings shape how a loader supplies data:

  • batch_size sets how many samples go into a minibatch. A model can then process multiple samples together. section 7.2.5 section 13.3.2
  • shuffle=True shuffles training data at the beginning of each epoch. Validation can instead use shuffle=False. section 7.2.5
  • num_workers enables separate processes to prepare batches in the background, overlapping data loading with GPU computation. section 13.3.2

From image dataset to training loop

Create a loader with `train_loader = torch.utils.data.DataLoader(cifar2, batch_size=64, shuffle=True)`, then iterate with `for imgs, labels in train_loader:`. Each iteration supplies a minibatch: `imgs` has shape `64 × 3 × 32 × 32` for 64 RGB images, and `labels` has 64 label indices. A separate validation loader uses `shuffle=False` while checking classification accuracy. section 7.2.5

Figure 7.14 A data loader dispensing minibatches by using a dataset to sample individual data items.
figure

The diagram shows how a loader uses a dataset’s individual items to dispense minibatches. section 7.2.5

The same pattern works when samples come from CT scans rather than small images: build a Dataset, pass it to a DataLoader, and iterate over the resulting batch tuples. With workers enabled, batches can be prepared while the GPU processes an earlier batch. Worker count and batch size are worth testing together, because some combinations may exhaust available memory. section 13.3 section 13.3.2 section 13.12

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains dataset sampling, batch collation, shuffling, and worker-based loading through code; leaves worker-count and memory limits to experimentation.
Examples
Uses 64-image CIFAR minibatches, CT-scan batch tuples, and a worker-count timing exercise.
What it assumes
Assumes a Dataset that provides samples; explains how its items become batches rather than requiring readers to implement collation.
What you can do after
Apply it via PyTorch DataLoader to batch and shuffle datasets for training, then configure workers for background loading.
How it connects
Connects Dataset, minibatches, stochastic gradient descent, validation, and parallel data loading.

Also explained in

Compare how 4 books explain DataLoader →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.