When working with custom datasets in PyTorch, the key metric is data loading correctness and efficiency. This means your dataset class must correctly load and return the right data samples and labels without errors. While this is not a model metric like accuracy, it is critical because bad data loading leads to wrong training and poor model results. Efficiency matters too, so training is not slowed down.
Dataset class (custom datasets) in PyTorch - Model Metrics & Evaluation
For dataset classes, we don't have a confusion matrix. Instead, we check data integrity by verifying the number of samples matches expectations and that each sample-label pair is correct. For example:
Dataset size: 1000 samples
Sample 0: image shape (3, 224, 224), label: 5
Sample 999: image shape (3, 224, 224), label: 2
This ensures the dataset class correctly loads all data.
There is a tradeoff between loading data correctly and loading it fast. If you load all data into memory at once, loading is fast but uses lots of RAM. If you load data on the fly, it uses less memory but can slow training. The goal is to balance correctness (no errors, right labels) with efficiency (fast enough to keep training smooth).
Good: Dataset returns correct samples and labels, length matches dataset size, no crashes during training, data shapes are consistent, and loading speed keeps up with training.
Bad: Dataset returns wrong labels, crashes on some indexes, length is wrong, data shapes vary unexpectedly, or loading is too slow causing training delays.
- Mixing up labels and data order causing wrong training signals.
- Not implementing
__len__or__getitem__correctly. - Loading all data into memory causing crashes on large datasets.
- Slow data loading blocking GPU training.
- Data leakage by accidentally including test data in training dataset.
Your custom dataset class returns 1000 samples but during training, the model gets random results and loss does not improve. What could be wrong?
Answer: The dataset might be returning wrong labels or mismatched data-label pairs. Check your __getitem__ method to ensure correct data loading.
Practice
Solution
Step 1: Understand Dataset role
A custom Dataset class is designed to organize data and provide easy access to samples and labels.Step 2: Differentiate from other components
Neural network architecture and visualization are separate tasks, not handled by Dataset classes.Final Answer:
To organize and provide access to your data for training models -> Option AQuick Check:
Dataset class = data organization [OK]
- Confusing Dataset with model definition
- Thinking Dataset handles visualization
- Assuming Dataset optimizes training speed
Solution
Step 1: Recall Dataset methods
__len__ returns the total number of samples in the dataset.Step 2: Differentiate from other methods
__getitem__ fetches a sample, __init__ initializes the dataset, load_data is not a standard method.Final Answer:
__len__ -> Option CQuick Check:
Count samples = __len__ [OK]
- Using __getitem__ to get length
- Confusing __init__ with length method
- Assuming load_data is required
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(len(dataset))
print(dataset[1])
Solution
Step 1: Calculate length of dataset
len(dataset) calls __len__, which returns length of self.data = 3.Step 2: Calculate dataset[1]
dataset[1] calls __getitem__ with idx=1, returns self.data[1]*2 = 20*2 = 40.Final Answer:
3 and 40 -> Option AQuick Check:
Length=3, item*2=40 [OK]
- Confusing index values
- Expecting error without DataLoader
- Forgetting the multiplication by 2
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]
Solution
Step 1: Check __len__ method
__len__ returns self.data, which is a list, but it must return an integer length.Step 2: Verify other methods
__getitem__ correctly returns data element; __init__ is present; Dataset inheritance is correct.Final Answer:
__len__ should return an integer, not the data list -> Option BQuick Check:
__len__ must return length integer [OK]
- Returning list instead of length in __len__
- Confusing Dataset with nn.Module
- Omitting __init__ method
__getitem__ method?Solution
Step 1: Understand __getitem__ role
__getitem__ should load and return the actual data sample and label when requested.Step 2: Evaluate options
Loading image on demand with transformations is efficient and standard. Returning file path or only labels is incomplete. Loading all images in __init__ can use too much memory.Final Answer:
Load the image file from disk, apply transformations, and return the image and label -> Option DQuick Check:
__getitem__ loads and returns data [OK]
- Loading all data in __init__ causing memory issues
- Returning file paths instead of data
- Ignoring images and returning only labels
Ready to prove it?
Pass the test and this pattern moves unknown
