PyTorch Geometric (PyG)
Overview
PyTorch Geometric is a library built on PyTorch for developing and training Graph Neural Networks (GNNs). Apply this skill for deep learning on graphs and irregular structures, including mini-batch processing, multi-GPU training, and geometric deep learning applications.
When to Use This Skill
This skill should be used when working with:
- Graph-based machine learning: Node classification, graph classification, link prediction
- Molecular property prediction: Drug discovery, chemical property prediction
- Social network analysis: Community detection, influence prediction
- Citation networks: Paper classification, recommendation systems
- 3D geometric data: Point clouds, meshes, molecular structures
- Heterogeneous graphs: Multi-type nodes and edges (e.g., knowledge graphs)
- Large-scale graph learning: Neighbor sampling, distributed training
Quick Start
uv pip install torch_geometric
Graphs are torch_geometric.data.Data objects: x (node features [N, F]), edge_index
(connectivity in COO [2, E]), optional edge_attr, y, pos, and any custom attribute
(train_mask, etc.). DataLoader batches multiple graphs into one block-diagonal graph
(no padding); a batch vector maps nodes back to their source graph.
Full install/sparse-deps, basic graph creation, benchmark loading, edge-index format, and
mini-batching details: references/getting_started.md.
Core Workflow
- Load or build data — benchmark datasets, custom
InMemoryDataset, or from CSV
(references/datasets_and_loading.md; full catalog in references/datasets_reference.md).
- Define a GNN — stack pre-built conv layers (GCNConv, GATConv, SAGEConv) or subclass
MessagePassing for custom layers (references/building_gnns.md; full layer list in
references/layers_reference.md).
- Train — node classification (single graph, train/test masks), graph classification
(
DataLoader + global pooling), or large-scale via NeighborLoader neighbor sampling
(references/training_workflows.md).
- Go advanced if needed —
HeteroData/to_hetero for heterogeneous graphs, transforms,
GNNExplainer explainability, hierarchical pooling, GPU, save/load
(references/advanced_features.md; transforms catalog in references/transforms_reference.md).
Building GNNs (at a glance)
GNNs follow neighborhood aggregation: transform node features → propagate messages along edges →
aggregate from neighbors → update representations. PyG ships 40+ conv layers. When choosing one,
check its capabilities: SparseTensor support, edge_weight, edge_attr, bipartite, and lazy
(-1 channel) initialization. Code for GCN/GAT/GraphSAGE and custom MessagePassing layers
(including the _i/_j target/source naming convention) is in references/building_gnns.md.
Resources
Bundled References
This skill includes detailed reference documentation:
references/getting_started.md: Install, basic graph creation, Data structure, edge-index format, mini-batching
references/building_gnns.md: Message passing, GCN/GAT/GraphSAGE code, custom MessagePassing layers, layer capabilities
references/datasets_and_loading.md: Built-in datasets, custom InMemoryDataset, loading graphs from CSV
references/training_workflows.md: Node classification, graph classification, large-scale neighbor sampling
references/advanced_features.md: Heterogeneous graphs, transforms, explainability, pooling, GPU, save/load
references/layers_reference.md: Complete listing of all 40+ GNN layers with descriptions and capabilities
references/datasets_reference.md: Comprehensive dataset catalog organized by category
references/transforms_reference.md: All available transforms and their use cases
Scripts
Utility scripts are provided in scripts/:
scripts/visualize_graph.py: Visualize graph structure using networkx and matplotlib
scripts/create_gnn_template.py: Generate boilerplate code for common GNN architectures
scripts/benchmark_model.py: Benchmark model performance on standard datasets
Execute scripts directly or read them for implementation patterns.
Official Resources
1---2name: alterlab-torch-geometric3description: Graph Neural Networks with PyTorch Geometric (PyG) — node and graph classification, link prediction, GCN, GAT, and GraphSAGE layers, heterogeneous graphs, and molecular property prediction. Use when building or training GNNs for geometric deep learning on graph-structured data. Part of the AlterLab Academic Skills suite.4license: MIT5---67# PyTorch Geometric (PyG)89## Overview1011PyTorch Geometric is a library built on PyTorch for developing and training Graph Neural Networks (GNNs). Apply this skill for deep learning on graphs and irregular structures, including mini-batch processing, multi-GPU training, and geometric deep learning applications.1213## When to Use This Skill1415This skill should be used when working with:16- **Graph-based machine learning**: Node classification, graph classification, link prediction17- **Molecular property prediction**: Drug discovery, chemical property prediction18- **Social network analysis**: Community detection, influence prediction19- **Citation networks**: Paper classification, recommendation systems20- **3D geometric data**: Point clouds, meshes, molecular structures21- **Heterogeneous graphs**: Multi-type nodes and edges (e.g., knowledge graphs)22- **Large-scale graph learning**: Neighbor sampling, distributed training2324## Quick Start2526```bash27uv pip install torch_geometric28```2930Graphs are `torch_geometric.data.Data` objects: `x` (node features `[N, F]`), `edge_index`31(connectivity in COO `[2, E]`), optional `edge_attr`, `y`, `pos`, and any custom attribute32(`train_mask`, etc.). `DataLoader` batches multiple graphs into one block-diagonal graph33(no padding); a `batch` vector maps nodes back to their source graph.3435Full install/sparse-deps, basic graph creation, benchmark loading, edge-index format, and36mini-batching details: `references/getting_started.md`.3738## Core Workflow39401. **Load or build data** — benchmark datasets, custom `InMemoryDataset`, or from CSV41 (`references/datasets_and_loading.md`; full catalog in `references/datasets_reference.md`).422. **Define a GNN** — stack pre-built conv layers (GCNConv, GATConv, SAGEConv) or subclass43 `MessagePassing` for custom layers (`references/building_gnns.md`; full layer list in44 `references/layers_reference.md`).453. **Train** — node classification (single graph, train/test masks), graph classification46 (`DataLoader` + global pooling), or large-scale via `NeighborLoader` neighbor sampling47 (`references/training_workflows.md`).484. **Go advanced if needed** — `HeteroData`/`to_hetero` for heterogeneous graphs, transforms,49 `GNNExplainer` explainability, hierarchical pooling, GPU, save/load50 (`references/advanced_features.md`; transforms catalog in `references/transforms_reference.md`).5152## Building GNNs (at a glance)5354GNNs follow neighborhood aggregation: transform node features → propagate messages along edges →55aggregate from neighbors → update representations. PyG ships 40+ conv layers. When choosing one,56check its capabilities: SparseTensor support, `edge_weight`, `edge_attr`, bipartite, and lazy57(`-1` channel) initialization. Code for GCN/GAT/GraphSAGE and custom `MessagePassing` layers58(including the `_i`/`_j` target/source naming convention) is in `references/building_gnns.md`.5960## Resources6162### Bundled References6364This skill includes detailed reference documentation:6566- **`references/getting_started.md`**: Install, basic graph creation, `Data` structure, edge-index format, mini-batching67- **`references/building_gnns.md`**: Message passing, GCN/GAT/GraphSAGE code, custom `MessagePassing` layers, layer capabilities68- **`references/datasets_and_loading.md`**: Built-in datasets, custom `InMemoryDataset`, loading graphs from CSV69- **`references/training_workflows.md`**: Node classification, graph classification, large-scale neighbor sampling70- **`references/advanced_features.md`**: Heterogeneous graphs, transforms, explainability, pooling, GPU, save/load71- **`references/layers_reference.md`**: Complete listing of all 40+ GNN layers with descriptions and capabilities72- **`references/datasets_reference.md`**: Comprehensive dataset catalog organized by category73- **`references/transforms_reference.md`**: All available transforms and their use cases7475### Scripts7677Utility scripts are provided in `scripts/`:7879- **`scripts/visualize_graph.py`**: Visualize graph structure using networkx and matplotlib80- **`scripts/create_gnn_template.py`**: Generate boilerplate code for common GNN architectures81- **`scripts/benchmark_model.py`**: Benchmark model performance on standard datasets8283Execute scripts directly or read them for implementation patterns.8485### Official Resources8687- **Documentation**: https://pytorch-geometric.readthedocs.io/88- **GitHub**: https://github.com/pyg-team/pytorch_geometric89- **Tutorials**: https://pytorch-geometric.readthedocs.io/en/latest/get_started/introduction.html90- **Examples**: https://github.com/pyg-team/pytorch_geometric/tree/master/examples91