-
Hi, I need some help with the interpretation of final results from a heterogeneous network. I am working on a node classification task on a heterogeneous network using this architecture. I would like to identify the different weights of connected nodes on the final classification results of focal nodes. For example, if the focal node N0 (type A) is classified as good and is directly connected to nodes N2 (type A), N3 (type B), and N4 (type C), I would like to get attention scores from N2, N3, and N4 that sum up to 1, indicating their respective impact on node N0's final classification result. I have read some papers that provide such implementations and even visualizations of such attention weights. However, I am wondering if PyG has a function that supports this implementation, as I am using PyG for my project. So far, I have tried "return_attention_weights" in GATConv, which does not support heterogeneous graph learning, and "return_semantic_attention_weights" in HANConv, which does not provide attention scores for each node's classification result. Does PyG offer any functions or support for implementing this type of attention mechanism in a heterogeneous network? Thank you very much! I am looking forward to your insightful feedback, as this is of great help to my projects. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
If you want to retrieve attention scores in a heterogeneous GNN, then this is possible but a bit cumbersome. I suggest to write a class HeteroGATConv(torch.nn.Module):
def __init__(self, node_types, edge_types, channels):
self.convs = torch.nn.ModuleDict()
for edge_type in edge_types:
self.convs[edge_type] = GATConv((-1, -1), channels)
def forward(self, x_dict, edge_index_dict):
out_dict = defaultdict(list)
attention_dict = {}
for edge_type edge_index in edge_index_dict.items():
x_src = x_dict[edge_type[0]]
x_dst = x_dict[edge_type[2]]
out, att = self.convs[edge_type]((x_src, x_dst), edge_index, return_attention_weights=True)
attention_dict[edge_type] = att
out_dict[edge_type[2]] = out
# Group `out_dict` into a single tensor
# ...
return out_dict, attention_dict |
Beta Was this translation helpful? Give feedback.
If you want to retrieve attention scores in a heterogeneous GNN, then this is possible but a bit cumbersome. I suggest to write a
HeteroGATConv
implementation: