Data Loaders
Data loaders feed mini-batches to the training loop.
In the earlier chapters, a batch was a group of examples processed together.
PyTorch's DataLoader is a practical tool for producing those batches.
from torch.utils.data import DataLoader, TensorDataset
dataset = TensorDataset(x_values, y_values)
loader = DataLoader(dataset, batch_size=32, shuffle=True)
for x_batch, y_batch in loader:
...
The data loader does not change the learning problem. It handles repeated, ordinary work:
- grouping examples into batches
- optionally shuffling their order
- iterating through the dataset
- supporting larger datasets and input pipelines later
Batching is not only a coding convenience. It shapes the training signal. A single example can be noisy. A full dataset can be expensive. A mini-batch is the usual compromise.
When reading PyTorch code, always ask what one item from the loader contains. For supervised learning, a batch usually contains inputs and targets. For other tasks, the batch structure may be different.
Answer it first, then check.
The data loader supplies the loop. The model still learns from examples, predictions, losses, gradients, and updates.