In a PyTorch training script, what is the main job of the DataLoader class?
Select an answer to reveal the explanation.
Short Explanation and Infographic
Think of DataLoader as the delivery truck for your training loop. Your Dataset object knows how to fetch one sample at a time, but training on one sample at a time is painfully slow and doesn't play nice with GPUs. DataLoader wraps that Dataset and hands you an iterable that spits out ready-to-use mini-batches, handling shuffling, batching, and even parallel loading with multiple workers behind the scenes. That's the correct answer. It's not defining your model's layers — that's nn.Module and friends. It's not computing gradients — that's autograd kicking in during loss.backward(). And it's definitely not updating your weights — that's torch.optim's job. DataLoader's whole purpose in life is getting clean, batched data to your model efficiently, so keep it separate in your mind from the actual learning machinery.
Full explanation below image
Full Explanation
The DataLoader class in torch.utils.data is responsible for taking a Dataset object — which defines how to access individual samples via __getitem__ and __len__ — and turning it into an efficient, iterable source of mini-batches for training or evaluation. It handles batching (grouping individual samples into tensors of a specified batch size), shuffling (randomizing sample order each epoch to reduce correlation between consecutive batches), and can parallelize data loading across multiple worker processes via the num_workers argument, which prevents the CPU-bound work of reading and preprocessing data from bottlenecking GPU utilization. This makes 'wraps a Dataset and provides an iterable over mini-batches' the correct description of its role.
Defining the neural network architecture is the job of torch.nn, where classes like nn.Linear, nn.Conv2d, and custom nn.Module subclasses specify layers and forward computation. DataLoader has no awareness of model structure; it only concerns itself with feeding data into whatever model consumes it.
Computing gradients is handled by PyTorch's autograd engine, triggered when you call .backward() on a loss tensor. Gradients are computed with respect to tensors that have requires_grad=True, and this process is entirely independent of how data was loaded — DataLoader has already finished its job (producing a batch) before the forward pass, loss computation, and backward pass even begin.
Applying weight updates is the responsibility of optimizer classes in torch.optim (e.g., SGD, Adam), invoked via optimizer.step() after gradients are computed. DataLoader never touches model parameters.
As a memory aid: Dataset defines 'what one sample looks like,' DataLoader defines 'how samples get grouped and delivered,' and the model/optimizer/autograd trio define 'what happens once the batch arrives.' Keeping these roles distinct is essential for reading and debugging PyTorch training loops.