0
0
PyTorchml~20 mins

Custom Dataset class in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Dataset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1: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))
A5
B4
C6
DTypeError
Attempts:
2 left
💡 Hint
The __len__ method returns the number of items in the dataset.
data_output
intermediate
1: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])
A30
BIndexError
C40
D60
Attempts:
2 left
💡 Hint
The __getitem__ method doubles the value at the index.
🔧 Debug
advanced
2: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))
ANo error, prints 3
BTypeError: __len__ should return int, not list
CTypeError: object of type 'list' has no len()
DTypeError: 'list' object cannot be interpreted as an integer
Attempts:
2 left
💡 Hint
Check what __len__ returns. It should be an integer.
🚀 Application
advanced
2: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
Areturn self.data[idx] + self.data[idx]
Breturn self.data[idx] * 2
Creturn self.data[idx] ** 2
Dreturn self.data[idx] ** 3
Attempts:
2 left
💡 Hint
Square means number multiplied by itself.
🧠 Conceptual
expert
2: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?
ABecause DataLoader uses __len__ to know dataset size and __getitem__ to get data samples by index
BBecause __len__ loads data and __getitem__ saves data
CBecause __getitem__ is used to shuffle data and __len__ to batch data
DBecause __len__ returns data type and __getitem__ returns data shape
Attempts:
2 left
💡 Hint
Think about how DataLoader accesses data.