pythonpytorchpytorch-dataloaderpytorch-geometric

Can't instantiate abstract class when I use `Dataset` in torch_geometric


I try to establish a class that inherit from Dataset of torch_geometric, the codes are:

from torch_geometric.data import Data, Dataset
import numpy as np

class GraphTimeSeriesDataset(Dataset):
    def __init__(self):
        # Initialize your dataset here
        pass

    def __len__(self):
        # Return the total number of samples in the dataset
        return len(self.data_list)

    def __getitem__(self, idx):
        # Return the data sample at the given index
        return self.data_list[idx]

dataset = GraphTimeSeriesDataset()

And the error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 17
     13     def __getitem__(self, idx):
     14         # Return the data sample at the given index
     15         return self.data_list[idx]
---> 17 dataset = GraphTimeSeriesDataset()

TypeError: Can't instantiate abstract class GraphTimeSeriesDataset with abstract methods get, len

I'm not sure what's the root cause of this error, because I can operate these codes in other environment.

My questions:

  1. Is this error caused by environment or codes itself?
  2. How should I adjust my codes to avoid this kind of error?
  3. What's possible root cause?

Solution

  • What it is stating here is that while you have some magic operations implemented, that the abstract base class has declared the methods get and len as abstract. Try adding

    def get(…): pass

    def len(…): pass

    Not sure the method signatures. But try that and see what happens.