What is the purpose of creating a Custom Dataset class in PyTorch?
A Custom Dataset class lets you load and prepare your own data in a way PyTorch understands. It helps you organize data and labels so you can easily use them for training models.
Click to reveal answer
beginner
Which two methods must you always define in a PyTorch Custom Dataset class?
You must define __len__ to return the number of samples, and __getitem__ to get a single data sample by index.
Click to reveal answer
beginner
How does the __getitem__ method work in a Custom Dataset?
It takes an index number and returns the data and label at that position. This allows PyTorch to get data one piece at a time during training.
Click to reveal answer
intermediate
Why might you want to add data transformations inside a Custom Dataset?
Transformations like resizing or normalizing images help prepare data in a consistent way before training. Adding them inside the Dataset keeps data ready and clean.
Click to reveal answer
intermediate
What is the benefit of using a Custom Dataset with a DataLoader in PyTorch?
The DataLoader can automatically handle batching, shuffling, and parallel loading of data from your Custom Dataset, making training faster and easier.
Click to reveal answer
Which method in a Custom Dataset returns the total number of samples?
A__getitem__
B__len__
Cload_data
Dget_sample
✗ Incorrect
The __len__ method returns how many samples the dataset contains.
What does the __getitem__ method return?
AA single data sample and its label
BThe entire dataset
CThe dataset size
DThe data loader
✗ Incorrect
The __getitem__ method returns one data sample and its label by index.
Why add transformations inside a Custom Dataset?
ATo make data loading slower
BTo avoid using DataLoader
CTo increase dataset size
DTo prepare data consistently before training
✗ Incorrect
Transformations prepare data in a consistent way, like resizing or normalizing.
What does a DataLoader do when used with a Custom Dataset?
ADefines __getitem__ method
BCreates the dataset
CHandles batching and shuffling automatically
DLoads data from disk manually
✗ Incorrect
DataLoader helps by batching, shuffling, and loading data in parallel.
Which PyTorch class do you inherit from to create a Custom Dataset?
Atorch.utils.data.Dataset
Btorch.Tensor
Ctorch.optim.Optimizer
Dtorch.nn.Module
✗ Incorrect
You inherit from torch.utils.data.Dataset to create a Custom Dataset.
Explain how to create a Custom Dataset class in PyTorch and why it is useful.
Think about the two main methods and the purpose of the class.
You got /5 concepts.
Describe how transformations can be integrated into a Custom Dataset and their benefits.
Consider what happens to data before it is returned.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of creating a Custom Dataset class in PyTorch?
easy
A. To visualize data samples
B. To organize and provide access to data samples for model training
C. To perform gradient descent optimization
D. To define the neural network architecture
Solution
Step 1: Understand Dataset role
A Custom Dataset class is designed to organize data and provide easy access to samples during training.
Step 2: Differentiate from other components
Neural network architecture and optimization are handled elsewhere, not by the Dataset class.
Final Answer:
To organize and provide access to data samples for model training -> Option B
Quick Check:
Dataset class = data access [OK]
Hint: Dataset class organizes data for training access [OK]
Common Mistakes:
Confusing Dataset with model definition
Thinking Dataset handles optimization
Assuming Dataset visualizes data
2. Which method must be implemented in a PyTorch Custom Dataset class to return the number of samples?
easy
A. __getitem__
B. __init__
C. __len__
D. forward
Solution
Step 1: Recall Dataset methods
__len__ returns the total number of samples in the dataset.
Step 2: Identify other methods' roles
__getitem__ fetches a sample, __init__ initializes the dataset, forward is for models, not datasets.
Final Answer:
__len__ -> Option C
Quick Check:
Count samples = __len__ [OK]
Hint: Count samples with __len__ method [OK]
Common Mistakes:
Using __getitem__ to get length
Confusing __init__ with length method
Using forward method in Dataset
3. Given this Custom Dataset code, what will print(len(dataset)) output?
__len__ returns length of self.data, which is a list of 4 elements.
Step 2: Evaluate print output
Calling len(dataset) calls __len__, so output is 4.
Final Answer:
4 -> Option A
Quick Check:
len([10,20,30,40]) = 4 [OK]
Hint: len(dataset) calls __len__ returning data length [OK]
Common Mistakes:
Confusing length with last element value
Expecting __getitem__ output instead of length
Assuming error without checking code
4. Identify the error in this Custom Dataset class:
from torch.utils.data import Dataset
class SampleDataset(Dataset):
def __init__(self, data):
self.data = data
def __len__(self):
return self.data
def __getitem__(self, idx):
return self.data[idx]
sample = SampleDataset([1,2,3])
print(len(sample))
medium
A. Missing __init__ method
B. __getitem__ returns wrong index
C. Dataset class not imported
D. __len__ returns data list instead of its length
Solution
Step 1: Analyze __len__ method
__len__ returns self.data, which is a list, not an integer length.
Step 2: Understand len() requirement
len() expects __len__ to return an integer. Returning a list causes a TypeError.
Final Answer:
__len__ returns data list instead of its length -> Option D
Quick Check:
__len__ must return int, not list [OK]
Hint: __len__ must return integer length [OK]
Common Mistakes:
Returning data list instead of length in __len__
Ignoring import statements
Confusing __getitem__ with __len__
5. You want to create a Custom Dataset class that returns both input features and labels from two separate lists. Which implementation correctly achieves this?