ML Systems Lab Open interactive version →
Intermediate 50 min read link predictionknowledge graphTransEnegative samplingMRR

Link Prediction

Heuristics, embedding decoders, knowledge graph completion, negative sampling, evaluation pitfalls

Freebase contains 40 million entities — actors, movies, directors — and 100 million triples: (Christopher Nolan, directed, Inception), (Inception, hasGenre, SciFi), (Leonardo DiCaprio, actedIn, Inception). Most triples are missing. The graph is an incomplete snapshot of a much larger set of true facts. Link prediction asks: what missing triples are likely true? If a user likes Inception, knowledge graph link prediction can infer other Nolan films the user might like by finding films connected to Nolan through the "directed" relation.

Two traps define the field. First, structural heuristics often match or beat learned GNN models on homophilic networks, because triangle closure is the dominant link formation mechanism. Common Neighbors, Adamic-Adar, and Katz scores run in O(|E|) time with no training and are surprisingly competitive on social and citation graphs. Always establish a heuristic baseline before training a GNN. If the GNN doesn't beat Adamic-Adar, the model is learning nothing the structure doesn't already tell you.

Second, evaluation is easy to get wrong in ways that inflate reported accuracy without any genuine generalization. If test edge (A, B) has training edges (A, C) and (C, B) in the training graph, the GNN encodes C's embedding in both A's and B's representations. The dot product between A's and B's embeddings is high because both reflect the shared neighbor C — not because the model generalized. The correct procedure removes test edges from the training adjacency matrix before any GNN training.

NOT this. "Knowledge graphs require hand-crafted ontologies." Modern knowledge graphs are mostly extracted from text automatically using information extraction and OpenIE systems. Wikidata has 90 million-plus triples and is collaboratively maintained. The knowledge graph embedding literature — TransE, RotatE, ComplEx — focuses on how to learn representations from the triple structure, not how to curate the ontology. The curation question is upstream of the ML question, and for most research and production applications it is already solved.

Key points

Takeaway

Evaluation methodology is the most dangerous part of link prediction. Naive random edge splits allow the GNN to learn paths through test edges during training, inflating apparent accuracy without any genuine generalization. The correct procedure removes test edges from the training adjacency matrix entirely — the GNN must never see paths through edges it will be tested on. For temporal graphs, a time-based split is mandatory: a model trained with future knowledge and evaluated on past links is measuring recall of a known graph, not prediction of an unknown one.

Recap

Check your understanding

Q1. You're building a friend recommendation system. Should you use Adamic-Adar or a GNN-based approach? What factors decide this?

Q2. Explain why TransE fails for symmetric relations in knowledge graphs and what model you would use instead.

Q3. Which two of the following statements about data leakage in link prediction are TRUE? (Select two.)

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →