pyg-team / pytorch_geometric

Graph Neural Network Library for PyTorch
https://pyg.org
MIT License
21.11k stars 3.63k forks source link

MessagePassing error when run heterogeneous GNN #6535

Closed yadong-zhang closed 1 year ago

yadong-zhang commented 1 year ago

🐛 Describe the bug

I was doing inductive learning with multiple heterogeneous graphs, here is the individual data:

HeteroData( gen={ x=[5, 6], y=[5, 1] }, load={ x=[8, 6] }, notany={ x=[1, 6] }, (gen, branch, gen)={ edge_index=[2, 2] }, (gen, branch, load)={ edge_index=[2, 7] }, (load, branch, load)={ edge_index=[2, 6] }, (gen, trafo, load)={ edge_index=[2, 2] }, (gen, trafo, notany)={ edge_index=[2, 1] }, (load, trafo, load)={ edge_index=[2, 1] }, (load, trafo, notany)={ edge_index=[2, 2] } )

I built a simple GNN model and then transformed it into heterogeneous GNN using torch_geometric.nn.to_hetero(), here is the code: import numpy as np import pandas as pd import matplotlib.pyplot as plt import torch import torch.nn as nn from torch.functional import F from torch_geometric.nn import GCNConv, GATConv, to_hetero from torch_geometric.data import Data, HeteroData from torch_geometric.loader import DataListLoader, DataLoader

class GNN_model(torch.nn.Module): def __init__(self): super(GNN_model, self).__init__() self.conv1 = GCNConv(6, 4, add_self_loops=False) self.conv2 = GCNConv(4, 2, add_self_loops=False) self.conv3 = GCNConv(2, 1, add_self_loops=False)

def forward(self, x, edge_index): x = self.conv1(x, edge_index) x = self.conv2(x, edge_index) x = self.conv3(x, edge_index) return x

model = GNN_model() model = to_hetero(model, data.metadata(), aggr='sum') model.train() y_pred = model(data.x_dict, data.edge_idnex_dict)

It shows the following error when running the model: **ValueError**: MessagePassing.propagate only supports 'torch.LongTensor' of shape '[2, num_messages]' or 'torch_sparse.SparseTensor' for argument 'edge_index'.

I double checked the edge_index of all edge types, they are all torch.int64. Can anyone help with this?

Environment

EdisonLeeeee commented 1 year ago

There is a typo: data.edge_idnex_dict should be data.edge_index_dict.

yadong-zhang commented 1 year ago

There is a typo: data.edge_idnex_dict should be data.edge_index_dict.

Thanks! I corrected the typo, now the error becomes: **AttributeError**: 'tuple' object has no attribute 'size'

EdisonLeeeee commented 1 year ago

GCNConv does not support message passing in bipartite graphs, you may have a try on SAGEConv. You can refer to GNN Cheatsheet for all supported operations.

yadong-zhang commented 1 year ago

GCNConv does not support message passing in bipartite graphs, you may have a try on SAGEConv. You can refer to GNN Cheatsheet for all supported operations.

Problem solved. Thanks for the help! :)