Post

Self-Supervised Learning on Graphs — Methods and Tips

Self-Supervised Learning on Graphs — Methods and Tips

Self-Supervised Learning on Graphs — Methods and Tips

Self-supervised learning (SSL) reduces label reliance by learning representations from structure and attributes. In graphs, SSL has driven large gains across node/graph classification, link prediction, and retrieval.

Why SSL for Graphs?

  • Labels are expensive and sparse on real-world graphs.
  • Structural inductive biases (neighborhoods, paths) are rich supervision.
  • SSL pretraining transfers across tasks and domains.

Paradigms

Contrastive (InfoMax)

Maximize agreement between two correlated “views” of the same node/graph.

  • Methods: DGI, InfoGraph, MVGRL, GraphCL
  • Objective (InfoNCE):
\[\mathcal{L}_\text{InfoNCE} = -\sum_i \log \frac{\exp(\text{sim}(z_i, z_i^+)/\tau)}{\sum_{j} \exp(\text{sim}(z_i, z_j)/\tau)}\]

Predictive (Masking/Context)

Predict masked node attributes or context substructures.

  • Methods: AttrMasking, ContextPred, GPT-GNN
  • Pros: No explicit negatives; stable optimization.

Generative/Reconstruction

Reconstruct edges, node features, or subgraphs.

  • Methods: Edge/node reconstruction, variational objectives.
  • Useful for link prediction, imputations.

Bootstrap (Negative-free)

Align two encoders with momentum targets.

  • Methods: BGRL, BYOL-for-graphs
  • Pros: Simpler pipelines, strong stability without negatives.

Graph Augmentations (the critical ingredient)

  • Node drop / edge drop (DropNode/DropEdge)
  • Attribute masking / feature corruption
  • Subgraph sampling (random walk, k-hop)
  • Diffusion / Laplacian smoothing views
  • Caution: choose augmentations that preserve task-relevant semantics (homophily vs. heterophily).

Practical Training Tips

  • Start simple: 2-layer GCN/GraphSAGE backbone; hidden 128–256.
  • Temperature τ in contrastive losses: 0.1–0.5 works well.
  • Balance positives/negatives; hard-negative mining can help.
  • Gradient clipping (e.g., 1.0) with large fanouts.
  • Early stopping on linear probe validation.

Evaluation Protocols

  • Linear probe: freeze encoder, train a simple classifier on labels.
  • Fine-tuning: initialize from SSL, train end-to-end with labels.
  • Report mean±std over multiple seeds; graphs are high-variance.

Minimal PyTorch Geometric Sketch (Contrastive)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Pseudocode sketch
import torch
from torch import nn
from torch_geometric.nn import GCNConv

def get_views(data):
    # return two augmented views of the same graph
    return aug1(data), aug2(data)

class Encoder(nn.Module):
    def __init__(self, in_dim, hid_dim):
        super().__init__()
        self.gcn1 = GCNConv(in_dim, hid_dim)
        self.gcn2 = GCNConv(hid_dim, hid_dim)
    def forward(self, x, edge_index):
        x = self.gcn1(x, edge_index).relu()
        x = self.gcn2(x, edge_index)
        return x

def info_nce(z1, z2, tau=0.2):
    z1, z2 = nn.functional.normalize(z1, dim=-1), nn.functional.normalize(z2, dim=-1)
    logits = z1 @ z2.T / tau
    labels = torch.arange(z1.size(0), device=z1.device)
    return nn.CrossEntropyLoss()(logits, labels)

Key Takeaways

  • Choose augmentations that match your domain; they define your supervision signal.
  • Prefer simple backbones and robust regularization early on.
  • Use linear probe to select checkpoints; then fine-tune.
This post is licensed under CC BY 4.0 by the author.