0
0
PyTorchml~20 mins

Dataset class (custom datasets) in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Dataset Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple custom Dataset __getitem__
What is the output of this code snippet when accessing dataset[1]?
PyTorch
import torch
from torch.utils.data import Dataset

class MyDataset(Dataset):
    def __init__(self):
        self.data = [10, 20, 30]
    def __len__(self):
        return len(self.data)
    def __getitem__(self, idx):
        return self.data[idx] * 2

dataset = MyDataset()
print(dataset[1])
A40
BIndexError
C30
D20
Attempts:
2 left
💡 Hint
Remember __getitem__ returns data[idx] multiplied by 2.
data_output
intermediate
2:00remaining
Length of a custom Dataset
What is the output of len(dataset) for this custom Dataset?
PyTorch
from torch.utils.data import Dataset

class SampleDataset(Dataset):
    def __init__(self):
        self.samples = list(range(7))
    def __len__(self):
        return len(self.samples)
    def __getitem__(self, idx):
        return self.samples[idx]

dataset = SampleDataset()
print(len(dataset))
A6
B7
CTypeError
D8
Attempts:
2 left
💡 Hint
Check how many elements are in samples list.
🔧 Debug
advanced
2:00remaining
Identify the error in this custom Dataset code
What error will this code raise when trying to create the dataset instance?
PyTorch
from torch.utils.data import Dataset

class BrokenDataset(Dataset):
    def __init__(self):
        self.data = [1, 2, 3]
    def __getitem__(self, idx):
        return self.data[idx]

# Missing __len__ method

dataset = BrokenDataset()
print(len(dataset))
ATypeError: object of type 'BrokenDataset' has no len()
BAttributeError: 'BrokenDataset' object has no attribute '__len__'
CNo error, dataset created successfully
DIndexError when accessing dataset[0]
Attempts:
2 left
💡 Hint
Dataset requires __len__ method to work properly.
🚀 Application
advanced
2:00remaining
Custom Dataset with transform application
Given this custom Dataset, what will be the output of dataset[0]?
PyTorch
from torch.utils.data import Dataset

def add_five(x):
    return x + 5

class TransformDataset(Dataset):
    def __init__(self, data, transform=None):
        self.data = data
        self.transform = transform
    def __len__(self):
        return len(self.data)
    def __getitem__(self, idx):
        sample = self.data[idx]
        if self.transform:
            sample = self.transform(sample)
        return sample

data = [1, 2, 3]
dataset = TransformDataset(data, transform=add_five)
print(dataset[0])
A1
B5
CTypeError
D6
Attempts:
2 left
💡 Hint
Transform adds 5 to the original data element.
🧠 Conceptual
expert
2:00remaining
Why implement a custom Dataset class in PyTorch?
Which of the following is the best reason to create a custom Dataset class in PyTorch?
ATo replace the need for DataLoader in batching data
BTo automatically parallelize model training across GPUs
CTo define how to load and access data samples and their labels efficiently
DTo speed up the training by compiling the model
Attempts:
2 left
💡 Hint
Think about what Dataset controls in PyTorch data handling.