Skip to main content

Graph Autoencoders (GAE & VGAE)

Graph Autoencoders bring the "Compression & Reconstruction" philosophy of deep learning to graph structures. By learning to compress a graph into a low-dimensional bottleneck and then reconstruct it, we can identify hidden relationships and perform high-accuracy link prediction.

GAE Workflow Visualization

1. The Architectural Logic

A Graph Autoencoder consists of two distinct components:

  1. Encoder: Typically a GNN (GCN or GAT) that maps both node features XX and graph structure AA into a latent matrix ZZ.
  2. Decoder: A simple generative model that predicts the presence of edges based on the latent embeddings ZZ.

The Mathematical Forward Pass

For a standard GAE with a GCN encoder:

  1. Latent Encoding: Z=GCN(X,A)Z = \text{GCN}(X, A)
  2. Edge Reconstruction: A^=σ(ZZT)\hat{A} = \sigma(Z Z^T)

The term ZZTZ Z^T calculates the inner product between all pairs of node embeddings. If the dot product between ziz_i and zjz_j is high, the decoder predicts that an edge (i,j)(i, j) should exist.


2. VGAE: The Variational Evolution

Standard GAEs are deterministic—they map each node to a single point. Variational Graph Autoencoders (VGAE) represent nodes as distributions (typically Gaussians), allowing for better generalization and a smoother latent space.

The VGAE Encoder

Instead of learning one vector zz, the encoder learns two: a mean μ\mu and a standard deviation σ\sigma.

  • μ=GCNμ(X,A)\mu = \text{GCN}_{\mu}(X, A)
  • logσ=GCNσ(X,A)\log \sigma = \text{GCN}_{\sigma}(X, A)

We then sample the final embedding zz using the reparameterization trick: z=μ+ϵσz = \mu + \epsilon \odot \sigma.


3. Loss Function: Binary Cross-Entropy

The goal is to make the reconstructed adjacency matrix A^\hat{A} as close as possible to the original matrix AA.

L=(i,j)Elog(A^ij)(i,j)Elog(1A^ij)\mathcal{L} = -\sum_{(i,j) \in \mathcal{E}} \log(\hat{A}_{ij}) - \sum_{(i,j) \notin \mathcal{E}} \log(1 - \hat{A}_{ij})

For VGAE, we also add a KL-Divergence term to force the latent distributions to stay close to a standard normal distribution N(0,I)\mathcal{N}(0, I).


4. Why Use GAEs?

TaskExplanation
Link PredictionPredicting future friendships or chemical bonds between molecules.
DenoisingReconstructing a "cleaner" version of a noisy or incomplete graph.
Dimensionality ReductionVisualizing complex, high-dimensional networks in 2D or 3D space.

5. Python Implementation (PyG Style)

gae_model.py
import torch
from torch_geometric.nn import GCNConv, GAE

class GCNEncoder(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv1 = GCNConv(in_channels, 2 * out_channels)
self.conv2 = GCNConv(2 * out_channels, out_channels)

def forward(self, x, edge_index):
x = self.conv1(x, edge_index).relu()
return self.conv2(x, edge_index)

# Initialize
# encoder = GCNEncoder(num_features, 16)
# model = GAE(encoder)

# Training step
# z = model.encode(x, edge_index)
# loss = model.recon_loss(z, edge_index)
# loss.backward()
Pro Tip: Link Prediction Metrics

When evaluating a GAE, don't just use Accuracy. Because most graphs are sparse (mostly zeros in AA), a model that predicts "no edge" everywhere will have high accuracy. Instead, use AUC-ROC and Average Precision (AP).