liveBook · Manning

Link prediction

As explained in Graph Neural Networks in Action

Link prediction, or edge prediction, is a graph-learning task that estimates whether an edge exists or should exist between graph entities.

Link prediction, also called edge prediction, estimates whether two graph nodes are connected or should be connected. Use it when relationships are missing, expensive to observe, or changing over time—for example, to suggest products or social connections. section 5.5.1 section 5.2.1

Product co-purchases

In an electronics graph, products are nodes and past co-purchases are edges. A link predictor estimates which other product pairs should be connected. Before training, inspect the number of edges and the degree distribution to understand how connected the graph is. section 5.2.1

A graph autoencoder provides one implementation:

  • Encode node features and the observed edge index with GCN layers to produce low-dimensional node representations. section 5.2.2
  • Decode a candidate node pair by taking the inner product of its representations to estimate an edge. section 5.2.2
  • Split edges into training, validation, and test sets. During training, pair observed links with sampled nonedges and compare predictions with their labels. section 5.2.3
  • Train with a binary classification loss and assess held-out predictions with ROC AUC. Check that the decoder’s sigmoid output and the chosen loss agree on whether inputs are probabilities or logits. section 5.2.2 section 5.2.3
Listing 5.8 Convert to a PyG object
data = Data(x=feature_matrix,         #1
            edge_index=edge_index,   
            edge_attr=edge_attr,     
            y=labels)                

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

transform = T.Compose([\
     T.NormalizeFeatures(),\                  #2
     T.ToDevice(device),                     
     T.RandomLinkSplit(num_val=0.05,\
     num_test=0.1, is_undirected=True,       
     add_negative_train_samples=False)])     
train_data, val_data, test_data = transform(data)

The PyTorch Geometric data-preparation listing shows feature normalization and an undirected edge split with 5% validation and 10% test edges. section 5.2.3

A graph neural network is most useful here when relationships carry information. For largely standalone records with insignificant connections, a graph model may be a poor fit. Some edge-prediction problems are dynamic: in mechanical reasoning, the connections between physical objects can change over time. section 1.6 section 1.3.3

How Graph Neural Networks in Action covers it

How deep it goes
Builds a GCN-based encoder and decoder, then details edge splitting, training, and evaluation; defers the inner-product rationale.
Examples
Uses Amazon Electronics co-purchases to demonstrate edge exploration, a validation/test split, and held-out ROC AUC evaluation.
What it assumes
Assumes familiarity with node features and graph adjacency data; explains the edge-prediction workflow rather than graph construction from zero.
What you can do after
Apply it via PyTorch Geometric to prepare edges, train a graph autoencoder, and evaluate predicted links.
How it connects
Connects graph autoencoders, graph convolutional networks, negative sampling, binary cross-entropy, and ROC AUC.

Also explained in

Compare how 3 books explain link prediction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Neural Networks in Action. The section links point to the parts of the book it draws on.