PyG Practitioner's Guide
PyTorch Geometric (PyG) is a library for graph machine learning built on PyTorch. Created by Matthias Fey, founding engineer at Kumo.ai, it provides graph data structures, message-passing layers, datasets, and tooling for graph learning in research and production.
Source title: The Practitioner’s Guide to PyTorch Geometric
Guide snapshot: 66 GNN layers, 120+ datasets, and 21M+ PyPI downloads.
Get started in five minutes
PyG requires PyTorch. Install both packages, then verify that PyG imports correctly.
The Cora citation network is a useful first node-classification example. It contains 2,708 papers, 10,556 citations, and seven research-topic classes.
Evaluate the trained model on the test split.
The source guide reports approximately 81% test accuracy for this basic two-layer GCN on Cora.
From relational tables to predictions
Every relational database is a graph hiding in plain sight. Rows are nodes, and foreign-key relationships are edges. PyG makes that structure explicit so a model can learn from both record attributes and connections.
Represent the data as a graph
Define node types for entities such as customers, orders, products, and transactions. Define edges for relationships such as purchases, transfers, and foreign keys.
Learn with GNN layers
Each layer aggregates information from neighboring nodes. With two or three layers, a node can use signal from its multi-hop neighborhood.
Choose a GNN layer
PyG includes a broad collection of layers because different graph structures and operational constraints require different message-passing strategies.
Start here
Attention, heterogeneous data, and transformers
Begin with GCNConv as a baseline.
Use GATConv when the importance of neighbors differs, HGTConv or HeteroConv for multi-table enterprise data, and sampling-oriented layers when the full graph cannot fit in memory.
For the foundations behind message passing, graph types, and graph transformers, see Introduction to Graph Transformers.
Concepts to understand
The following ideas determine whether a graph model is well matched to the problem.
- Message passing: The basic computation through which nodes exchange and aggregate neighbor information.
- Heterogeneous graphs: Graphs with multiple node and edge types, typical of relational databases.
- Over-smoothing: A failure mode where many GNN layers make node representations too similar.
- Link prediction: Predicting missing or future edges for recommendations, fraud detection, and knowledge graphs.
- Graph transformers: Architectures that combine graph structure with broader attention patterns.
- Neighbor sampling: Training on a bounded subset of each node’s neighborhood to scale to large graphs.
- Data leakage: Accidentally using future or otherwise unavailable graph information at training time.
Apply graph learning to business problems
Graph ML is useful when relationships add important signal beyond a single record.
Take graph models to production
Moving from a notebook to a production system requires more than a trained model. Teams need a reliable way to construct and update the graph, train at the needed scale, score new data, handle temporal relationships correctly, and explain predictions.
Full-batch training is often impractical for large graphs. Neighbor sampling, graph partitioning, and carefully chosen architectures help make graph learning tractable at production scale.
Common production concerns include:
- Heterogeneous graphs: Model multiple table types and typed relationships.
- Temporal graphs: Avoid using information that would not have been available at prediction time.
- Scaling: Use sampling and partitioning for graphs with millions or billions of nodes.
- Serving: Turn trained embeddings and model outputs into reliable predictions.
- Integration: Connect graph learning workflows to data platforms such as Snowflake and Databricks.
- Explainability: Inspect which graph features and relationships contributed to a prediction.
Datasets for benchmarking
PyG provides loaders for standard graph-learning datasets across citation, social, molecular, recommendation, knowledge-graph, financial, and large-scale benchmark domains.
Examples include Cora, CiteSeer, PubMed, Reddit, OGB-Products, OGB-Papers100M, MUTAG, QM9, MovieLens 1M, Elliptic Bitcoin, and FB15k-237. Choose a dataset that matches the learning task and graph properties you need to evaluate, such as node classification, link prediction, graph classification, or regression.