Pytorch Geometric Dataset class __init__ is not executed

username: CallMeMisterOwl
cooked: <p>I’ve created a custom <code>Dataset</code> class that inherits from the pytorch-geometric <code>Dataset</code> class. As shown in the official <a href="https://pytorch-geometric.readthedocs.io/en/latest/notes/create_dataset.html" rel="noopener nofollow ugc">website</a>, one can modify the <code>__init__()</code> function of the custom dataset.<br>
Example:</p>
<pre><code class="lang-auto">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])
</code></pre>
<p>Though when I initialise my custom dataset class <strong>it does not execute the <code> __init__()</code> function</strong>. I can completely remove the function and it will still run until the point a variable from the <code> __init__()</code> is missing.<br>
I had to question my sanity at some point, since I can’t comprehend what’s going on here.<br>
<strong>This can’t be intended</strong>.<br>
If you know why this is happening or even have vague idea how to fix this <strong>PLEASE</strong>  let me know. I would be incredibly grateful.</p>
<p>This is how my implementation looks like:</p>
<pre><code class="lang-auto">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'))
</code></pre>
<p>Edit: python version 3.9 and package versions<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/0/6/06b06274af718bf1a3a2aa526c7f116438133608.png" alt="image" data-base62-sha1="XaLyCkcw59gLwedsTjDGOeqyfe" width="546" height="257"></p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="CallMeMisterOwl" data-post="1" data-topic="159636">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/callmemisterowl/48/52563_2.png" class="avatar"> CallMeMisterOwl:</div>
<blockquote>
<p>Though when I initialise my custom dataset class <strong>it does not execute the <code> __init__()</code> function</strong>.</p>
</blockquote>
</aside>
<p>That’s not the case and you can verify that your <code>__init__</code> method is called by adding a simple <code>print</code> statement. Note that your <code>StringdbDataset</code> has a typo and uses <code>__int__</code> instead of <code>__init__</code>, so you should fix it in case you also have the same typo in your code.</p>
<p>In any case, this works:</p>
<pre data-code-wrap="python"><code class="lang-python">class MyDataset(InMemoryDataset):
    def __init__(self, root=None, transform=None):
        print("executing __init__")
        super().__init__(root, transform)
        
dataset = MyDataset()
</code></pre>
<aside class="quote no-group" data-username="CallMeMisterOwl" data-post="1" data-topic="159636">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/callmemisterowl/48/52563_2.png" class="avatar"> CallMeMisterOwl:</div>
<blockquote>
<p>I can completely remove the function and it will still run until the point a variable from the <code> __init__()</code> is missing.<br>
I had to question my sanity at some point, since I can’t comprehend what’s going on here.<br>
<strong>This can’t be intended</strong>.</p>
</blockquote>
</aside>
<p>This is intended and is called <code>Inheritance</code>. <a href="https://www.w3schools.com/python/python_inheritance.asp">This article</a> explains it in more detail. In your case your custom <code>MyDataset</code> is derived from <code>InMemoryDataset</code>, which defined an <a href="https://github.com/pyg-team/pytorch_geometric/blob/b69573ef6cf490d30297ed8d97599b1f35fee29d/torch_geometric/data/in_memory_dataset.py#L46-L53"><code>__init__</code> method</a> which will be executed. You can also add a <code>print</code> statement to this PyTorch-Geometric class and would see that it’s called:</p>
<ul>
<li>after the <code>__init__</code> method of your custom class is called</li>
<li>directly if your custom class does not implement an <code>__init__</code> method.</li>
</ul>

username: CallMeMisterOwl
cooked: <blockquote>
<p>Note that your <code>StringdbDataset</code> has a typo and uses <code>__int__</code> instead of <code>__init__</code> , so you should fix it in case you also have the same typo in your code.</p>
</blockquote>
<p>Thank god, I really thought I’m losing my mind. Yes, it turns out I had this typo in my code for <strong>weeks</strong> without realising it. Of course everything, but me, works as intended.<br>
This is really embarrassing… <img src="https://discuss.pytorch.org/images/emoji/apple/man_facepalming.png?v=12" title=":man_facepalming:" class="emoji" alt=":man_facepalming:" loading="lazy" width="20" height="20"><br>
Apologies for wasting your time and <strong>thank you !</strong></p>
<p>PS: I hope my supervisor doesn’t find this post <img src="https://discuss.pytorch.org/images/emoji/apple/shushing_face.png?v=12" title=":shushing_face:" class="emoji" alt=":shushing_face:" loading="lazy" width="20" height="20"></p>

username: ptrblck
cooked: <p>Don’t worry, we all including your supervisor, ran into the same issue at least once before. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>