Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Write a code to do link prediction in heterogeneous graph with torch_geometric

  1. User

    Write a code to do link prediction in heterogeneous graph with torch_geometric

  2. Assistant

    Sure, here's an example code to do link prediction in a heterogeneous graph using torch_geometric:

    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    from torch_geometric.nn import HeteroConv
    
    class LinkPredictionModel(nn.Module):
        def __init__(self, num_node_types, hidden_dim):
            super(LinkPredictionModel, self).__init__()
            
            self.conv = HeteroConv({
                'node_type1': nn.Linear(num_node_types['node_type1'], hidden_dim),
                'node_type2': nn.Linear(num_node_types['node_type2'], hidden_dim),
                'node_type3': nn.Linear(num_node_types['node_type3'], hidden_dim)
            })
            
            self.linear = nn.Linear(hidden_dim, 1)
            
        def forward(self, x, edge_index, edge_type):
            x = self.conv(x, edge_index, edge_type)
            x = self.linear(x)
            return torch.sigmoid(x)
    
    # Create a sample heterogeneous graph
    x_node_type1 = torch.FloatTensor([1, 2, 3]).unsqueeze(1)
    x_node_type2 = torch.FloatTensor([4, 5]).unsqueeze(1)
    x_node_type3 = torch.FloatTensor([6, 7, 8, 9]).unsqueeze(1)
    x = {'node_type1': x_node_type1, 'node_type2': x_node_type2, 'node_type3': x_node_type3}
    
    edge_index_node_type1 = torch.LongTensor([[0, 1],
                                              [1, 0]])
    edge_index_node_type2 = torch.LongTensor([[0, 1],
                                              [1, 0]])
    edge_index_node_type3 = torch.LongTensor([[0, 1, 2, 3],
                                              [1, 2, 3, 0]])
    edge_index = {'node_type1': edge_index_node_type1, 'node_type2': edge_index_node_type2, 'node_type3': edge_index_node_type3}
    
    edge_type_node_type1 = torch.LongTensor([0, 0])
    edge_type_node_type2 = torch.LongTensor([1, 1])
    edge_type_node_type3 = torch.LongTensor([2, 2, 2, 2])
    edge_type = {'node_type1': edge_type_node_type1, 'node_type2': edge_type_node_type2, 'node_type3': edge_type_node_type3}
    
    # Define the model
    model = LinkPredictionModel(num_node_types={'node_type1': 1, 'node_type2': 1, 'node_type3': 1}, hidden_dim=16)
    
    # Generate random negative examples by corrupting positive edges
    num_negative_samples = 16
    neg_edge_index = []
    neg_edge_type = []
    for node_type in edge_index.keys():
        for i in range(num_negative_samples):
            neg_edge_index.append(torch.tensor([torch.randint(x[node_type].size(0), (1,)),
                                                 torch.randint(x[node_type].size(0), (1,))]))
            neg_edge_type.append(torch.tensor([torch.randint(edge_index[node_type].size(0), (1,))]))
    
    neg_edge_index = torch.cat(neg_edge_index, dim=1).squeeze()
    neg_edge_type = torch.cat(neg_edge_type).squeeze()
    
    optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
    
    # Training loop
    num_epochs = 100
    model.train()
    for epoch in range(num_epochs):
        optimizer.zero_grad()
        
        pos_output = model(x, edge_index, edge_type)
        neg_output = model(x, neg_edge_index, neg_edge_type)
        
        loss = F.binary_cross_entropy(pos_output, torch.ones_like(pos_output)) + \
               F.binary_cross_entropy(neg_output, torch.zeros_like(neg_output))
        
        loss.backward()
        optimizer.step()
        
        if epoch % 10 == 0:
            print(f'Epoch: {epoch}, Loss: {loss.item()}')
    
    # Link prediction
    model.eval()
    output = model(x, edge_index, edge_type)
    predicted_edges = (output > 0.5).nonzero()
    
    print('Predicted edges:')
    for edge in predicted_edges:
        print(f'({edge[0]}, {edge[1]})')
    

    This code defines a simple LinkPredictionModel using HeteroConv module provided by torch_geometric. It then trains the model using positive and negative examples generated from the input graph. Finally, it predicts the missing edges based on the trained model and prints them. Note that this is just a basic example, and you can customize and extend it based on your specific requirements.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.