neural-networkpredictpytorch-geometricgraph-neural-network

Single prediction after training Graph Neural Network


I'm learning about Graph Neural Networks using PyTorch Geometric lib, but I don't know how to solve a trick question.

The docs have a very nice list of Collab code for each graph problem. For example, I'm using this one about Link Prediction on the MovieLens dataset.

I can complete all the #TODOs on this code and do the training part of the Neural Network. It's working fine, I receive an excellent accuracy score after iterating over my validation dataset. My problem is: What should I do now to predict a single missing link?

When I run output = model(test_data), I receive a Tensor with some positive and negative values (see below) that do not mean much to me.

output = model(test_data)
print(output)

tensor([ -0.2850,   2.7798,   3.7315,  ..., -10.3339,  -6.0439,  -5.1031],
       device='cuda:0', grad_fn=<SumBackward1>)

Since I found some similar questions about it here in SO, but none helped me, I will describe clearly what I want and avoid duplications.

I want to know how I can create a function that receives two nodes and give me the probability of having a link between a movie and a user.

def is_there_a_link(user, movie):
    //What code goes here?
    if prediction > 0.5:
        # more than 50%
        return 'YES'
    else:
        return 'NO'

#Should I recommend the movie with id 3 for the user with id 1?
prediction = is_there_a_link(test_data['user'].node_id[1], test_data['movie'].node_id[3])

I also looked at this video on PyG documentation with a slightly different implementation. Still, it also ends after validation (Other PyTorch tutorials on the web always stop after validation too). What I should do next is not clear to me.


Solution

  • When I run output = model(test_data), I receive a Tensor with some positive and negative values (see below) that do not mean much to me.

    That's because it's not probabilities, it's probability logits

    To get the probabilities you need to run

    output = model(test_data)
    probabilities = torch.nn.functional.softmax(output, dim=0)
    print(probabilities)