Create a DataLoader
Problem Statement
Create a DataLoader to batch a dataset.
Background
DataLoader wraps a Dataset and provides automatic batching, shuffling, and parallel loading — the standard way to feed data into a training loop.
Your Task
Write a function create_dataloader(dataset, batch_size=2, shuffle=False) that returns a DataLoader for the given dataset with the specified batch size, shuffling only when shuffle is true (default: no shuffling).
Output Format
Return a DataLoader object.
Example
dataset (6 samples), batch_size=2, shuffle=False
DataLoader with 3 batches
- Input:
datasetholds samples, each a (feature vector, label) pair, e.g. features with labels .batch_sizeis how many samples get grouped together, andshuffleis off, so samples keep their original order. - Math: the number of batches is . Batch 1 is samples 1–2, batch 2 is samples 3–4, batch 3 is samples 5–6.
- What one batch looks like: the samples are stacked along a new leading axis, so batch 1 gives features (a array: batch size feature count) and labels (length 2).
- Output: an iterable object, not a list of numbers; its length is the batch count,
3. - Notice that the last batch can be smaller: with and you get batches of sizes .
Constraints
- Use torch.utils.data.DataLoader
- batch_size will be a positive integer
- shuffle will be a boolean