Challenge - 5 Problems
Custom Dataset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of Custom Dataset __len__ method
Consider this PyTorch custom Dataset class. What is the output of
len(dataset)?PyTorch
import torch from torch.utils.data import Dataset class MyDataset(Dataset): def __init__(self, data): self.data = data def __len__(self): return len(self.data) def __getitem__(self, idx): return self.data[idx] data = [1, 2, 3, 4, 5] dataset = MyDataset(data) print(len(dataset))
Attempts:
2 left
💡 Hint
The __len__ method returns the number of items in the dataset.
✗ Incorrect
The __len__ method returns the length of the data list, which has 5 elements.
❓ data_output
intermediate1:30remaining
Output of __getitem__ method in Custom Dataset
Given this custom Dataset, what is the output of
dataset[2]?PyTorch
import torch from torch.utils.data import Dataset class MyDataset(Dataset): def __init__(self, data): self.data = data def __len__(self): return len(self.data) def __getitem__(self, idx): return self.data[idx] * 2 data = [10, 20, 30, 40] dataset = MyDataset(data) print(dataset[2])
Attempts:
2 left
💡 Hint
The __getitem__ method doubles the value at the index.
✗ Incorrect
dataset[2] accesses data[2] which is 30, then multiplies by 2 to get 60.
🔧 Debug
advanced2:00remaining
Identify the error in Custom Dataset class
What error will this code raise when creating the dataset?
PyTorch
import torch from torch.utils.data import Dataset class MyDataset(Dataset): def __init__(self, data): self.data = data def __len__(self): return self.data def __getitem__(self, idx): return self.data[idx] data = [1, 2, 3] dataset = MyDataset(data) print(len(dataset))
Attempts:
2 left
💡 Hint
Check what __len__ returns. It should be an integer.
✗ Incorrect
The __len__ method returns self.data which is a list, but it must return an integer length.
🚀 Application
advanced2:00remaining
Creating a Dataset that returns squared values
You want a custom Dataset that returns the square of each number in a list. Which option correctly implements __getitem__?
PyTorch
import torch from torch.utils.data import Dataset class SquareDataset(Dataset): def __init__(self, data): self.data = data def __len__(self): return len(self.data) def __getitem__(self, idx): # Fill this method pass data = [1, 2, 3, 4] dataset = SquareDataset(data) print(dataset[3]) # Should print 16
Attempts:
2 left
💡 Hint
Square means number multiplied by itself.
✗ Incorrect
Option C returns the square of the element at idx, which matches the requirement.
🧠 Conceptual
expert2:30remaining
Why override __getitem__ and __len__ in Custom Dataset?
Why do we need to override both
__getitem__ and __len__ methods when creating a PyTorch custom Dataset?Attempts:
2 left
💡 Hint
Think about how DataLoader accesses data.
✗ Incorrect
DataLoader needs to know how many samples exist (__len__) and how to get each sample (__getitem__).