Heterogeneous Graph Learning error - "NotImplementedError"

achm3dz: <p>I am trying to implement a simple Heterogeneous Graph Learning example but I am getting the “NotImplementedError”. I tried following the instructions <a href="https://pytorch-geometric.readthedocs.io/en/latest/notes/heterogeneous.html" rel="noopener nofollow ugc">here</a> but I am not sure what am I missing. Could someone please point out the problem in my code? Thanks in advance.</p>
<p>Below is my code:</p>
<pre><code class="lang-auto">import torch
from torch_geometric.data import HeteroData
from torch_geometric.nn import to_hetero
from torch_geometric.nn import SAGEConv


class GNN(torch.nn.Module):
    def __init__(self, hidden_channels, out_channels):
        super().__init__()
        self.conv1 = SAGEConv((-1, -1), hidden_channels)
        self.conv2 = SAGEConv((-1, -1), out_channels)

    def forward(self, x, edge_index):
        x = self.conv1(x, edge_index).relu()
        x = self.conv2(x, edge_index)
        return x
    
data = HeteroData()

data['node_type_1'].x = torch.rand((2,1))

data['node_type_2'].x = torch.rand((8,4))

type1_type2_edges = torch.tensor([[0, 0], [0, 1], [1, 0], [1, 1]])

type2_type2_edges = torch.tensor([[0,1],[0,2],[0,3],[1,0],[1,2],[1,3],
                                  [2,0],[2,1],[2,3],[3,0],[3,1],[3,2],
                                  [4,5],[4,6],[4,7],[5,4],[5,6],[5,7],
                                  [6,4],[6,5],[6,7],[7,4],[7,5],[7,6],
                                  [0,4],[1,5],[2,6],[3,7]])

data['node_type_1', 'actuates', 'node_type_2'].edge_index = type1_type2_edges.t().contiguous()
data['node_type_2', 'interconnect', 'node_type_2'].edge_index = type2_type2_edges.t().contiguous()
    

model = GNN(hidden_channels=64, out_channels=4)
model = to_hetero(model, data.metadata(), aggr='sum')
</code></pre>

thecho7: <p>Please let me know the whole error message.<br>
What I can guess is the <strong>version</strong> of <code>torch_geometric</code></p>

ptrblck: <p>In <code>torch_geometric==2.3.0</code> I see:</p>
<pre><code class="lang-python">/opt/miniforge3/envs/2.0.0_cu118/lib/python3.8/site-packages/torch_geometric/nn/to_hetero_transformer.py:156: UserWarning: There exist node types ({'node_type_1'}) whose representations do not get updated during message passing as they do not occur as destination type in any edge type. This may lead to unexpected behavior.

  File /opt/miniforge3/envs/2.0.0_cu118/lib/python3.8/site-packages/torch_geometric/nn/to_hetero_transformer.py:404 in _recurse
    raise NotImplementedError

NotImplementedError
</code></pre>
<p>which points to <a href="https://github.com/pyg-team/pytorch_geometric/blob/306e7900ac61f0b2f45b19ba7b5fcc5cc0b845b8/torch_geometric/nn/to_hetero_transformer.py#L404">this line of code</a>. However, I’m not familiar enough with <code>torch_geometric</code> to know what exactly is failing.</p>

achm3dz: <p>This is the full error. My version of torch_geometric is 2.1.0</p>
<pre><code class="lang-auto">lib\site-packages\torch_geometric\nn\to_hetero_transformer.py:145: UserWarning: There exist node types ({'node_type_1'}) whose representations do not get updated during message passing as they do not occur as destination type in any edge type. This may lead to unexpected behaviour.
  warnings.warn(
Traceback (most recent call last):

  File "C:\Users\jilan\anaconda3\envs\ADS\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
    exec(code, globals, locals)

  File "c:\users\jilan\dropbox\autonomous vehicle\vehicle control\gnn_v2\untitled6.py", line 44, in &lt;module&gt;
    model = to_hetero(model, data.metadata(), aggr='sum')

  File "C:\Users\jilan\anaconda3\envs\ADS\lib\site-packages\torch_geometric\nn\to_hetero_transformer.py", line 118, in to_hetero
    return transformer.transform()

  File "C:\Users\jilan\anaconda3\envs\ADS\lib\site-packages\torch_geometric\nn\fx.py", line 157, in transform
    getattr(self, op)(node, node.target, node.name)

  File "C:\Users\jilan\anaconda3\envs\ADS\lib\site-packages\torch_geometric\nn\to_hetero_transformer.py", line 294, in call_method
    args, kwargs = self.map_args_kwargs(node, key)

  File "C:\Users\jilan\anaconda3\envs\ADS\lib\site-packages\torch_geometric\nn\to_hetero_transformer.py", line 397, in map_args_kwargs
    args = tuple(_recurse(v) for v in node.args)

  File "C:\Users\jilan\anaconda3\envs\ADS\lib\site-packages\torch_geometric\nn\to_hetero_transformer.py", line 397, in &lt;genexpr&gt;
    args = tuple(_recurse(v) for v in node.args)

  File "C:\Users\jilan\anaconda3\envs\ADS\lib\site-packages\torch_geometric\nn\to_hetero_transformer.py", line 387, in _recurse
    raise NotImplementedError

NotImplementedError
</code></pre>

thecho7: <p>I got solve your problem.<br>
<code>node_type_1</code> in the dataset cannot be updated as in the picture (a graph includes only 1, 2)<br>
You should construct an edge which gives information that how to update <code>node_type_1</code> .<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/7/5703b44194909ed0cfab0bc9a7b11616b8ee2965.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/5703b44194909ed0cfab0bc9a7b11616b8ee2965" title="20230330_111857"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/5703b44194909ed0cfab0bc9a7b11616b8ee2965_2_375x500.jpeg" alt="20230330_111857" data-base62-sha1="cpLtvFLX6hlcS0XxBrfKwwjBuVn" width="375" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/5703b44194909ed0cfab0bc9a7b11616b8ee2965_2_375x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/5703b44194909ed0cfab0bc9a7b11616b8ee2965_2_562x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/5703b44194909ed0cfab0bc9a7b11616b8ee2965_2_750x1000.jpeg 2x" data-dominant-color="D6D2CE"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">20230330_111857</span><span class="informations">3000×4000 262 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>This can be done by below,</p>
<pre><code class="lang-auto">data['node_type_1', 'actuates', 'node_type_2'].edge_index = type1_type2_edges.t().contiguous()
data['node_type_2', 'interconnect', 'node_type_2'].edge_index = type2_type2_edges.t().contiguous()
data['node_type_2', 'actuates', 'node_type_1'].edge_index = type2_type2_edges.t().contiguous()
</code></pre>

achm3dz: <p>Thanks a lot. I guess that was it!</p>