Bird
Raised Fist0
PyTorchml~20 mins

DataLoader basics in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DataLoader Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output length of batches from this DataLoader?

Given the following PyTorch DataLoader setup, how many batches will it produce?

PyTorch
from torch.utils.data import DataLoader, TensorDataset
import torch

data = torch.arange(10)
dataset = TensorDataset(data)
dataloader = DataLoader(dataset, batch_size=3, shuffle=False)

batches = list(dataloader)
print(len(batches))
A3
B4
C5
D10
Attempts:
2 left
💡 Hint

Think about how many full batches of size 3 fit into 10 items, and what happens to the leftover items.

❓ Model Choice
intermediate
1:30remaining
Which DataLoader option enables random shuffling of data each epoch?

You want your model to see data in a different order every time you train. Which DataLoader parameter should you set?

Adrop_last=True
Bbatch_size=1
Cnum_workers=4
Dshuffle=True
Attempts:
2 left
💡 Hint

Think about which option changes the order of data samples.

❓ Hyperparameter
advanced
2:00remaining
What effect does setting drop_last=True have in DataLoader?

Consider a dataset with 10 samples and batch_size=4. What happens if drop_last=True?

AThe last batch with fewer than 4 samples is dropped, so only 2 batches are returned.
BThe last batch is padded with zeros to reach batch size 4.
CAll batches are returned, including the last smaller batch.
DDataLoader raises an error if the last batch is smaller than batch size.
Attempts:
2 left
💡 Hint

Think about what happens to incomplete batches when drop_last is True.

🔧 Debug
advanced
2:30remaining
Why does this DataLoader code raise a RuntimeError?

Examine the code below. Why does it raise a RuntimeError about workers?

PyTorch
from torch.utils.data import DataLoader, TensorDataset
import torch

data = torch.arange(5)
dataset = TensorDataset(data)
dataloader = DataLoader(dataset, batch_size=2, num_workers=2)

for batch in dataloader:
    print(batch)
ATensorDataset cannot be used with num_workers > 0.
Bbatch_size must be 1 when using num_workers > 0.
Cnum_workers > 0 requires the code to be inside a 'if __name__ == "__main__"' block on Windows.
DDataLoader does not support iteration with num_workers > 0.
Attempts:
2 left
💡 Hint

Consider platform-specific multiprocessing rules in Python.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of using DataLoader with multiple workers?

Why would you set num_workers > 0 in a DataLoader? Choose the best explanation.

AIt allows loading data in parallel, reducing waiting time during training.
BIt increases the batch size automatically for faster training.
CIt guarantees data is shuffled more thoroughly each epoch.
DIt compresses data to save memory during loading.
Attempts:
2 left
💡 Hint

Think about how multiple workers affect data loading speed.

Practice

(1/5)
1. What is the main purpose of using DataLoader in PyTorch?
easy
A. To split the dataset into batches and optionally shuffle it for training
B. To define the neural network architecture
C. To calculate the loss during training
D. To save the trained model to disk

Solution

  1. Step 1: Understand DataLoader's role

    DataLoader helps by splitting data into smaller batches and can shuffle data to improve training.
  2. Step 2: Compare options

    Options B, C, and D relate to other parts of training, not data loading.
  3. Final Answer:

    To split the dataset into batches and optionally shuffle it for training -> Option A
  4. Quick Check:

    DataLoader = batching and shuffling [OK]
Hint: DataLoader = batch + shuffle, not model or loss [OK]
Common Mistakes:
  • Confusing DataLoader with model definition
  • Thinking DataLoader calculates loss
  • Assuming DataLoader saves models
2. Which of the following is the correct way to create a DataLoader for a dataset named my_dataset with batch size 32 and shuffling enabled?
easy
A. loader = DataLoader(my_dataset, batch_size=32, shuffle=True)
B. loader = DataLoader(my_dataset, batch=32, shuffle=True)
C. loader = DataLoader(my_dataset, batch_size=32, shuffle=False)
D. loader = DataLoader(my_dataset, size=32, shuffle=True)

Solution

  1. Step 1: Recall DataLoader parameters

    The correct parameter for batch size is batch_size, and shuffle is a boolean.
  2. Step 2: Check each option

    loader = DataLoader(my_dataset, batch_size=32, shuffle=True) uses correct parameter names and values. Options B and D use wrong parameter names. loader = DataLoader(my_dataset, batch_size=32, shuffle=False) disables shuffling.
  3. Final Answer:

    loader = DataLoader(my_dataset, batch_size=32, shuffle=True) -> Option A
  4. Quick Check:

    batch_size and shuffle=True correct [OK]
Hint: Use batch_size= and shuffle= for DataLoader [OK]
Common Mistakes:
  • Using batch instead of batch_size
  • Setting shuffle to False when shuffling is needed
  • Using size instead of batch_size
3. Given the following code, what will be the output printed?
from torch.utils.data import DataLoader, TensorDataset
import torch

x = torch.arange(10)
y = torch.arange(10) * 2
dataset = TensorDataset(x, y)
loader = DataLoader(dataset, batch_size=4, shuffle=False)

for batch in loader:
    print(batch)
medium
A. Only one batch with all data
B. (tensor([0, 1, 2, 3]), tensor([0, 2, 4, 6])) then (tensor([4, 5, 6, 7]), tensor([8, 10, 12, 14])) then (tensor([8, 9]), tensor([16, 18])) but shuffled
C. Error because shuffle=False is invalid
D. (tensor([0, 1, 2, 3]), tensor([0, 2, 4, 6])) then (tensor([4, 5, 6, 7]), tensor([8, 10, 12, 14])) then (tensor([8, 9]), tensor([16, 18]))

Solution

  1. Step 1: Understand DataLoader batching without shuffle

    DataLoader splits data into batches of size 4, no shuffling means order is preserved.
  2. Step 2: Calculate batches

    Dataset has 10 items, batches are: first 4 (0-3), second 4 (4-7), last 2 (8-9). Corresponding y values are double x.
  3. Final Answer:

    (tensor([0, 1, 2, 3]), tensor([0, 2, 4, 6])) then (tensor([4, 5, 6, 7]), tensor([8, 10, 12, 14])) then (tensor([8, 9]), tensor([16, 18])) -> Option D
  4. Quick Check:

    Batch size 4, no shuffle = ordered batches [OK]
Hint: No shuffle means batches keep original order [OK]
Common Mistakes:
  • Assuming shuffle=True by default
  • Expecting all data in one batch
  • Thinking shuffle=False causes error
4. What is wrong with this DataLoader usage?
from torch.utils.data import DataLoader
loader = DataLoader(batch_size=16, shuffle=True)
for batch in loader:
    print(batch)
medium
A. batch_size should be a string, not integer
B. shuffle cannot be True
C. Missing dataset argument when creating DataLoader
D. for loop syntax is incorrect

Solution

  1. Step 1: Check DataLoader parameters

    DataLoader requires a dataset as the first argument; it's missing here.
  2. Step 2: Validate other parts

    batch_size as integer and shuffle=True are valid. For loop syntax is correct.
  3. Final Answer:

    Missing dataset argument when creating DataLoader -> Option C
  4. Quick Check:

    Dataset argument required [OK]
Hint: Always pass dataset first to DataLoader [OK]
Common Mistakes:
  • Forgetting to pass dataset to DataLoader
  • Thinking batch_size must be string
  • Believing shuffle=True is invalid
5. You have a dataset of 103 samples and want to train a model using batches of size 20 with shuffling enabled. How many batches will the DataLoader produce per epoch, and what happens to the last batch?
hard
A. 5 batches; the last batch is dropped because it's smaller
B. 6 batches; the last batch has 3 samples
C. 6 batches; the last batch is padded to 20 samples
D. 5 batches; all batches have exactly 20 samples

Solution

  1. Step 1: Calculate number of batches

    103 samples divided by batch size 20 gives 5 full batches (20*5=100) plus 1 partial batch with 3 samples.
  2. Step 2: Understand default DataLoader behavior

    By default, DataLoader includes the last smaller batch unless drop_last=True is set, so last batch has 3 samples.
  3. Final Answer:

    6 batches; the last batch has 3 samples -> Option B
  4. Quick Check:

    103/20 = 5 full + 1 partial batch [OK]
Hint: Last batch included unless drop_last=True [OK]
Common Mistakes:
  • Assuming last batch is dropped by default
  • Thinking last batch is padded automatically
  • Calculating wrong number of batches
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard