Pytorch Geometric Dataset class __init__ is not executed

I’ve created a custom Dataset class that inherits from the pytorch-geometric Dataset class. As shown in the official website, one can modify the __init__() function of the custom dataset.
Example:

class MyDataset(InMemoryDataset):
    def __init__(self, root, data_list, transform=None):
        self.data_list = data_list
        super().__init__(root, transform)
        self.data, self.slices = torch.load(self.processed_paths[0])

Though when I initialise my custom dataset class it does not execute the __init__() function. I can completely remove the function and it will still run until the point a variable from the __init__() is missing.
I had to question my sanity at some point, since I can’t comprehend what’s going on here.
This can’t be intended.
If you know why this is happening or even have vague idea how to fix this PLEASE let me know. I would be incredibly grateful.

This is how my implementation looks like:

class StringdbDataset(Dataset):
    def __int__(self, root, transform=None, pre_transform=None, pre_filter=None):
        super().__init__(root, transform, pre_transform, pre_filter)
        self.edges = torch.load(op.join(self.processed_dir, f'edges.pt'))

Edit: python version 3.9 and package versions
image

That’s not the case and you can verify that your __init__ method is called by adding a simple print statement. Note that your StringdbDataset has a typo and uses __int__ instead of __init__, so you should fix it in case you also have the same typo in your code.

In any case, this works:

class MyDataset(InMemoryDataset):
    def __init__(self, root=None, transform=None):
        print("executing __init__")
        super().__init__(root, transform)
        
dataset = MyDataset()

This is intended and is called Inheritance. This article explains it in more detail. In your case your custom MyDataset is derived from InMemoryDataset, which defined an __init__ method which will be executed. You can also add a print statement to this PyTorch-Geometric class and would see that it’s called:

  • after the __init__ method of your custom class is called
  • directly if your custom class does not implement an __init__ method.
1 Like

Note that your StringdbDataset has a typo and uses __int__ instead of __init__ , so you should fix it in case you also have the same typo in your code.

Thank god, I really thought I’m losing my mind. Yes, it turns out I had this typo in my code for weeks without realising it. Of course everything, but me, works as intended.
This is really embarrassing… :man_facepalming:
Apologies for wasting your time and thank you !

PS: I hope my supervisor doesn’t find this post :shushing_face:

1 Like

Don’t worry, we all including your supervisor, ran into the same issue at least once before. :wink:

1 Like