Heterogeneous Graph Learning error - "NotImplementedError"

I am trying to implement a simple Heterogeneous Graph Learning example but I am getting the “NotImplementedError”. I tried following the instructions here but I am not sure what am I missing. Could someone please point out the problem in my code? Thanks in advance.

Below is my code:

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')

Please let me know the whole error message.
What I can guess is the version of torch_geometric

In torch_geometric==2.3.0 I see:

/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

which points to this line of code. However, I’m not familiar enough with torch_geometric to know what exactly is failing.

This is the full error. My version of torch_geometric is 2.1.0

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 <module>
    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 <genexpr>
    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

I got solve your problem.
node_type_1 in the dataset cannot be updated as in the picture (a graph includes only 1, 2)
You should construct an edge which gives information that how to update node_type_1 .

This can be done by below,

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()
1 Like

Thanks a lot. I guess that was it!