Training, Optimization & Scaling

Losses, sampling, mini-batch training on billion-edge graphs, and debugging GNN training.

1. Loss Functions

TaskLossNotes
Node classificationCross-entropyOnly on labeled train nodes
Multi-labelBCE with logitsNodes can have multiple labels
Link predictionBCE / margin rankingNeed negative samples
Graph regressionMSE / MAEOn graph-level readout
Self-supervisedContrastive (InfoNCE)GraphMAE, DGI - pretrain then finetune

2. Full-Batch vs Mini-Batch

Full-batch: entire graph in memory, one gradient step per epoch. Works for Cora (2.7K nodes). Fails for OGBN-Products (2.4M nodes).

Mini-batch: sample subset of nodes/edges per step. Required for large graphs.

3. Neighbor & Subgraph Sampling

from torch_geometric.loader import NeighborLoader

loader = NeighborLoader(
    data,
    num_neighbors=[25, 10],
    batch_size=1024,
    input_nodes=data.train_mask,
)

5. Optimization Tricks

6. Debugging Checklist

  1. Does a label propagation or MLP baseline work? If MLP beats GNN, structure may not help.
  2. Check train accuracy - can't fit train = bug or too few layers
  3. Verify masks and edge splits for leakage
  4. Plot loss curve - diverging = lr too high
  5. Check isolated nodes - get zero or self-loop-only messages
  6. Compare 2 vs 4 layers - val drops at 4 = oversmoothing

7. Frameworks

FrameworkStrength
PyTorch GeometricResearch default, huge model zoo
DGLLarge-scale, heterogeneous, distributed
PyG + OGBStandardized benchmarks
DeepSNAPBridge NetworkX and PyG