[Python] How to handel heterogenous gnn?

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have created this data:

    HeteroData(
    user={ x=[100, 16] },
    keyword={ x=[321, 16] },
    tweet={ x=[1000, 16] },
    (user, follow, user)={ edge_index=[2, 291] },
    (user, tweetedby, tweet)={ edge_index=[2, 1000] },
    (keyword, haskeyword, tweet)={ edge_index=[2, 3752] }
    )


    And these two varibales based on that:

    x_dict:
    user: torch.Size([100, 16])
    keyword: torch.Size([321, 16])
    tweet: torch.Size([1000, 16])

    edge_index_dict:
    ('user', 'follow', 'user'): torch.Size([2, 291])
    ('user', 'tweetedby', 'tweet'): torch.Size([2, 1000])
    ('keyword', 'haskeyword', 'tweet'): torch.Size([2, 3752])


    My nodes were indexed from 0 to 99 for users, from 100 to 420 for keywords, and next for tweet nodes.

    When I want to run this model:

    class HeteroGATBinaryClassifier(torch.nn.Module):
    def __init__(self, metadata, in_channels, hidden_channels, heads=1):
    super().__init__()
    self.metadata = metadata # Metadata about node and edge types

    # Define GNN layers for each edge type
    self.conv1 = HeteroConv({
    edge_type: GATConv(in_channels, hidden_channels, heads=heads, add_self_loops = False)
    for edge_type in metadata[1]
    }, aggr='mean') # Aggregate using mean

    self.conv2 = HeteroConv({
    edge_type: GATConv(hidden_channels * heads, hidden_channels, heads=heads, add_self_loops = False)
    for edge_type in metadata[1]
    }, aggr='mean')

    # Linear layer for classification (binary output)
    self.classifier = Linear(hidden_channels * heads, 1) # Single output for binary classification

    def forward(self, x_dict, edge_index_dict, target_node_type):
    # First GAT layer
    x_dict = self.conv1(x_dict, edge_index_dict)
    x_dict = {key: F.elu(x) for key, x in x_dict.items()}

    # Second GAT layer
    x_dict = self.conv2(x_dict, edge_index_dict)
    x_dict = {key: F.elu(x) for key, x in x_dict.items()}

    # Apply the classifier only on the target node type
    logits = self.classifier(x_dict[target_node_type]) # Logits for target node type
    return torch.sigmoid(logits) # Output probabilities for binary classification


    I see this error:


    IndexError: Found indices in 'edge_index' that are larger than 999 (got 1420). Please ensure that all indices in 'edge_index' point to valid indices in the interval [0, 1000) in your node feature matrix and try again.
    ​

    Do you have any idea how to handle it?

    Continue reading...

Compartilhe esta Página