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


