Graph Neural Network Library for PyTorch
PyG (PyTorch Geometric) is a library built upon PyTorch to easily write and train Graph Neural Networks (GNNs) for a wide range of applications related to structured data.
It consists of various methods for deep learning on graphs and other irregular structures, also known as geometric deep learning, from a variety of published papers.
In addition, it consists of easy-to-use mini-batch loaders for operating on many small and single giant graphs, multi GPU-support, torch.compile support, DataPipe support, a large number of common benchmark datasets (based on simple interfaces to create your own), and helpful transforms, both for learning on arbitrary graphs as well as on 3D meshes or point clouds.
[Click here to join our Slack community!][slack-url]
Whether you are a machine learning researcher or first-time user of machine learning toolkits, here are some reasons to try out PyG for machine learning on graph-structured data.
In this quick tour, we highlight the ease of creating and training a GNN model with only a few lines of code.
In the first glimpse of PyG, we implement the training of a GNN for classifying papers in a citation graph.
For this, we load the Cora dataset, and create a simple 2-layer GCN model using the pre-defined GCNConv:
…import torch.nn.functional as F
data = dataset[0]
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
for epoch in range(200):
pred = model(data.x, data.edge_index)
loss = F.cross_entropy(pred[data.train_mask], data.y[data.train_mask])
# Backpropagation
optimizer.zero_grad()
loss.backward()
optimizer.step()More information about evaluating final model performance can be found in the corresponding example.
In addition to the easy application of existing GNNs, PyG makes it simple to implement custom Graph Neural Networks (see here for the accompanying tutorial). For example, this is all it takes to implement the edge convolutional layer from Wang et al.:
$$x_i^{\prime} ~ = ~ \max_{j \in \mathcal{N}(i)} ~ \textrm{MLP}_{\theta} \left( [ ~ x_i, ~ x_j - x_i ~ ] \right)$$
…PyG provides a multi-layer framework that enables users to build Graph Neural Network solutions on both low and high levels. It comprises of the following components:
torch.compile and TorchScript support, as well as additions of efficient CPU/CUDA libraries for operating on sparse data, e.g., pyg-lib.We list currently supported PyG models, layers and operators according to category:
GNN layers:
All Graph Neural Network layers are implemented via the nn.MessagePassing interface.
A GNN layer specifies how to perform message passing, i.e. by designing different message, aggregation and update functions as defined here.
These GNN layers can be stacked together to create Graph Neural Network models.
normalized_cut broadcasts degree scales over feature channels instead of edges
unbatch_edge_index drops trailing edgeless graphs when batch_size is inferred
Request code to implement XENet paper
27 tests fail
`Batch.from_data_list` only store node/edge types of first element for `HeteroData`
Notebook `5. Point Cloud Classification` still uses `torch-cluster` instead of `pyg-lib`
APPNP applies edge dropout cumulatively across propagation steps
Add `EGT` to `nn.conv`
Avoid importing the `llm` subtree (and pandas) on `import torch_geometric`
Add `channel_list` to `BasicGNN`