Repository: pykeen/pykeen
Issue:  link prediction in knowledge graph embedding
GitHub labels: question

i need to do link prediction in knowledge graph embedding. I must be doing the link prediction by Bayesian estimation. 
can you guide me how can do it? 
at least, how many relations must be in the train step? 
I must compute a similarity matrix for head and tail too, can you introduce me any functions for it. 

Hi @afrooze, it looks like this is a duplicate of https://github.com/pykeen/pykeen/discussions/268, so I'll answer here.

With PyKEEN, you start with a dataset and fit a given knowledge graph embedding model like TransE or RotatE, then you can do link prediction. There are three tutorials you should check out first:

1. [Training a model in PyKEEN](https://pykeen.readthedocs.io/en/latest/tutorial/first_steps.html)
2. [Using your own data to train a model](https://pykeen.readthedocs.io/en/latest/tutorial/byod.html)
3. [Making novel link predictions](https://pykeen.readthedocs.io/en/latest/tutorial/making_predictions.html)

Each model defines an interaction function that is used to score the predicted links. The interface lets you either pick a given entity and predict scores for all heads/tails, or you can predict scores for all possible triples (expensive). Each model has its own scoring, so it's a bit difficult to compare scores directly (this is an area of active research), but you can definitely rank. In a realistic project, like drug discovery, link prediction corresponds to generating hypotheses that can be tested in a laboratory, so ranking is more useful than binary prediction of yes/no for each possible triple.

Wit regards to Bayesian estimation, I'm not quite sure what you mean. I'll also need a bit more context to answer your follow up questions about the number of relations in the train step. Typically, there are no rules about how many relations appear in a set of triples to do meaningful training. However, if there's only one relation type, you could consider other network-based methods for embedding that are all much more simple and have better theoretical backing with concepts like graph laplacians, etc.

If you want to generate a node similarity matrix, you can grab the node embeddings from the model that you train and use any function that suits you to generate this. Most models are a subclass of [EntityRelationEmbeddingModel](https://pykeen.readthedocs.io/en/latest/api/pykeen.models.EntityRelationEmbeddingModel.html) and have a field called `entity_embeddings` that is an instance of `torch.nn.Embedding`. The following image shows the class hierarchy, so you can see for which models this is true:

<img src="https://pykeen.readthedocs.io/en/latest/_images/inheritance-0fff2d46974d9d9d5908f0a7a12c14ddd31de59a.png" />



thank you for your complete and useful answer.  
at first, I have to train my model then I have to do link prediction. can you explain more about the ranking process? is the ranking about the possible edges scoring?

If there are entities in set E and relations in set R, there are `|E| * |R| * |E|` possible triples. All can be enumerated and scored by a trained model using the functions in the 3rd link from above. Bigger scores means the triple is more likely to be true (though, this shouldn't be considered as a probability distribution), so you sort the triples by score with big ones first and small ones last (that's what I meant by ranking). The same can be done if you want to predict all tails for a given head, but then your search space is only of size `|R| * |E|`. The functions outlined in this tutorial implement each of these possible operations. In the end, maybe you choose to take the top 100 ranked scores, or the top 1% of scores. That's up to you, and depends on your use case. If you could explain what your overall goal is (on the problem level, not the methodology or the code. Are you doing drug discovery? Co-author recommendation? Fraud detection?) then I could probably give better advice.

I am going to use it in the question/answering system.

as I understood the ranking must be done with my trained model. is it true?

> as I understood the ranking must be done with my trained model. is it true?

Scoring triples with an untrained model, while technically possible because of how the code is written, wouldn't be meaningful at all. Same goes for using the embeddings that are randomly initialized before training.

thank you so much for your complete and useful answers.

You’re very welcome. I’m going to close this issue, please feel free to open another if you’re got more questions