Why we extend Dataset in our custom class dataset

what will happen if I will not extend the Dataset(I know this is an abstract class ) but what is the usefulness of this extending?
import numpy as np

from torch.utils.data import Dataset
 
class ExampleDataset(Dataset):
    def __init__(self, data):
        self.data = data
 
    def __len__(self):
        return len(self.data)
    
    def __getitem__(self, idx):
        return self.data[idx]


sample_data = np.arange(0, 10)
print('The whole data: ', sample_data)
dataset = ExampleDataset(sample_data)
print('Number of samples in the data: ', len(dataset))
print(dataset[2])
print(dataset[0:5])

It’s a way to check it’s an instance of a BaseClass. If you read the base class it’s nothing but a scheme.

what will happen if we will not extend the Baseclass?

Well I would say (my memory is finite) nothing happens but in the worst case just an exception like dataloader requires dataset class or something like that. For sure you will find an exception if you use ConcatDataset over non inherited classes.

1 Like

Do we need to use the Dataset class?

Well, we are able to run deep learning models without the Dataset class; but loading a dataset is generally memory-intensive — so it’s highly recommended to use the Dataset class.