A variational graph autoencoder (VGAE) learns a probabilistic latent representation of a graph and decodes samples from it. Use one when the goal is not only to reconstruct graph structure, such as missing links, but also to sample new graphs or subgraphs. Compared with a graph autoencoder (GAE), its more structured latent space is especially useful when diverse synthetic samples matter. section 5.3.1 section 5.3.2
A practical link-prediction pipeline has four parts:
- Pass node features and edges through a graph encoder. Instead of one fixed embedding, return a mean and log variance for each latent dimension. section 5.3.1
- Sample latent representations and decode them into predictions about edges. PyTorch Geometric’s VGAE supplies the sampling mechanism and a built-in inner-product decoder. section 5.3.1
- Train against observed edges and sampled negative edges, adding KL-divergence loss to the reconstruction objective. The KL term regularizes the learned latent distribution relative to a prior. section 5.3.1 section 5.6
- Evaluate both the task result and, when generating graphs, the samples: reconstruction scores alone need not capture whether synthetic graphs resemble the data. section 5.3
Listing 5.13 VariationalGCNEncoder
class VariationalGCNEncoder(torch.nn.Module): #1
def __init__(self, input_size, layers, latent_dims):
super().__init__()
self.layer0 = GCNConv(input_size, layers[0])
self.layer1 = GCNConv(layers[0], layers[1])
self.mu = GCNConv(layers[1], latent_dims)
self.logvar = GCNConv(layers[1], latent_dims)
def forward(self, x, edge_index):
x = self.layer0(x, edge_index).relu()
x = self.layer1(x, edge_index).relu()
mu = self.mu(x, edge_index)
logvar = self.logvar(x, edge_index)
return mu, logvar #2
The encoder implementation makes the key change from a GAE visible: two GCN output heads return mu and logvar rather than a single point embedding. section 5.3.1
From product links to candidate molecules
For an Amazon product co-purchase network, a VGAE can learn which pairs of products should be connected. For molecule generation, represent atoms and bonds as a graph, reconstruct its adjacency matrix, and add a property-prediction loss for QED. Sampling near latent regions associated with higher QED then suggests new molecular graphs to test; a generated graph is a candidate, not a verified drug. section 5.0 section 5.4.2 section 5.4.4
Choose the simpler GAE when fast reconstruction of clearly structured graph data is the priority. A VGAE is often better suited to noisy data or varied new samples, but regularizing the latent space creates a trade-off with reconstruction: compact, continuous regions can make data less separated. The architecture still inherits GNN concerns such as over-squashing. section 5.3.2 section 5.3 section 5.5.2
