0
0
PyTorchml~10 mins

DataLoader basics in PyTorch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a DataLoader for the dataset with batch size 4.

PyTorch
from torch.utils.data import DataLoader

dataloader = DataLoader(dataset, batch_size=[1])
Drag options to blanks, or click blank then click option'
A4
B16
C1
D8
Attempts:
3 left
💡 Hint
Common Mistakes
Using batch_size larger than dataset size causes errors.
Forgetting to set batch_size causes default of 1.
2fill in blank
medium

Complete the code to shuffle the dataset in the DataLoader.

PyTorch
dataloader = DataLoader(dataset, batch_size=4, shuffle=[1])
Drag options to blanks, or click blank then click option'
AFalse
BTrue
C0
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Setting shuffle to False causes data to be loaded in the same order every time.
Using None or 0 for shuffle causes errors.
3fill in blank
hard

Fix the error in the code to correctly iterate over the DataLoader batches.

PyTorch
for [1] in dataloader:
    inputs, labels = batch
    print(inputs.shape, labels.shape)
Drag options to blanks, or click blank then click option'
Alabels
Bdataloader
Cinputs
Dbatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'inputs' or 'labels' as loop variable causes unpacking errors.
Using 'dataloader' as loop variable causes infinite loop.
4fill in blank
hard

Fill both blanks to create a DataLoader with batch size 8 and no shuffling.

PyTorch
dataloader = DataLoader(dataset, batch_size=[1], shuffle=[2])
Drag options to blanks, or click blank then click option'
A8
BTrue
CFalse
D16
Attempts:
3 left
💡 Hint
Common Mistakes
Using shuffle=True when no shuffling is wanted.
Setting batch size too large for dataset.
5fill in blank
hard

Fill all three blanks to create a DataLoader with batch size 16, shuffling enabled, and 2 worker threads.

PyTorch
dataloader = DataLoader(dataset, batch_size=[1], shuffle=[2], num_workers=[3])
Drag options to blanks, or click blank then click option'
A4
BTrue
C2
D16
Attempts:
3 left
💡 Hint
Common Mistakes
Setting num_workers to 0 disables multi-threading.
Using shuffle=False when random order is needed.