PyTorch is all you need!
GraphGallery
GraphGallery is a gallery for benchmarking Graph Neural Networks (GNNs) based on pure PyTorch backend. Alteratively, Pytorch Geometric (PyG) and Deep Graph Library (DGL) backend are also available in GraphGallery to facilitate your implementations.
💨 NEWS
- November 20, 2021: We now no longer support
TensorFlow
backend. - November 20, 2021: The module
graphgallery.attack
is deprecated, users may refer to GraphWar for more information.
🚀 Installation
Please make sure you have installed PyTorch. Also, Pytorch Geometric (PyG) and Deep Graph Library (DGL) are alternative choices.
Install from source:
# Recommended
git clone https://github.com/EdisonLeeeee/GraphGallery.git && cd GraphGallery
pip install -e . --verbose
where -e
means "editable" mode so you don't have to reinstall every time you make changes.
NOTE: GraphGallery is a frequently updated package and DO NOT install GraphGallery with pip
, we're currently working on releasing a binary distribution on PyPI
, stay tuned!
🤖 Implementations
In detail, the following methods are currently implemented:
Node Classification
Graph Purification
The graph purification methods are universal for all models, just specify:
graph_transform="purification_method"
so, here we only give the examples of GCN
with purification methods, other models should work.
Method | Author | Paper |
---|---|---|
GCN-Jaccard | Huijun Wu et al. | Adversarial Examples on Graph Data: Deep Insights into Attack and Defense (IJCAI'19) |
GCN-SVD | Negin Entezari et al. | All You Need Is Low (Rank): Defending Against Adversarial Attacks on Graphs (WSDM'20) |
LinkPrediction
Method | Author | Paper | PyTorch | PyG | DGL |
---|---|---|---|---|---|
GAE, VGAE | Thomas N. Kipf et al. | Variational Graph Auto-Encoders (NeuIPS'16) | ✔️ |
Node Embedding
The following methods are framework-agnostic.
Method | Author | Paper |
---|---|---|
Deepwalk | Bryan Perozzi et al. | DeepWalk: Online Learning of Social Representations (KDD'14) |
Node2vec | Aditya Grover and Jure Leskovec | node2vec: Scalable Feature Learning for Networks (KDD'16) |
Node2vec+ | Renming Liu et al. | Accurately Modeling Biased Random Walks on Weighted Graphs Using Node2vec+ |
BANE | Hong Yang et al. | Binarized attributed network embedding (ICDM'18) |
⚡ Quick Start
Datasets
- Planetoid: a collection of widely used benchmark datasets in graph learning tasks, including 'cora', 'citeseerr', 'pubmed' and 'nell' datasets.
- NPZDataset: a collection of graph datasets stored with numpy
.npz
format.
you can simply run dataset.available_datasets()
to see the available datasets, e.g.,:
from graphgallery.datasets import Planetoid
print(Planetoid.available_datasets())
more details please refer to GraphData.
Example of GCN (Node Classification Task)
It takes just a few lines of code.
import torch
import graphgallery
from graphgallery.datasets import Planetoid
from graphgallery.gallery import callbacks
data = Planetoid('cora', root="~/GraphData/datasets/", verbose=True)
graph = data.graph
splits = data.split_nodes()
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
graphgallery.set_backend("torch")
from graphgallery.gallery.nodeclas import GCN
trainer = GCN(device=device, seed=123).setup_graph(graph, feat_transform="normalize_feat").build()
cb = callbacks.ModelCheckpoint('model.pth', monitor='val_accuracy')
trainer.fit(splits.train_nodes, splits.val_nodes, verbose=1, callbacks=[cb])
results = trainer.evaluate(splits.test_nodes)
print(f'Test loss {results.loss:.5}, Test accuracy {results.accuracy:.2%}')
Example of GAE (Link Prediction Task)
import torch
import graphgallery
from graphgallery.gallery import callbacks
from graphgallery.datasets import Planetoid
data = Planetoid('cora', root="~/GraphData/datasets/", verbose=True)
graph = data.graph
splits = data.split_edges(random_state=15)
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
graphgallery.set_backend("torch")
from graphgallery.gallery.linkpred import GAE
trainer = GAE(device=device, seed=123).setup_graph(graph).build()
cb = callbacks.ModelCheckpoint('model.pth', monitor='val_ap')
trainer.fit(splits.train_pos_edge_index,
val_data=(splits.val_pos_edge_index, splits.val_neg_edge_index),
verbose=1, callbacks=[cb])
results = trainer.evaluate((splits.test_pos_edge_index, splits.test_neg_edge_index))
print(results)
If you have any troubles, you can simply run trainer.help()
for more information.
Other Backends
>>> import graphgallery
# Default: PyTorch backend
>>> graphgallery.backend()
PyTorch 1.9.0+cu111 Backend
# Switch to PyTorch Geometric backend
>>> graphgallery.set_backend("pyg")
# Switch to DGL PyTorch backend
>>> graphgallery.set_backend("dgl")
# Switch to PyTorch backend
>>> graphgallery.set_backend("th") # "torch", "pytorch"
But your codes don't even need to change.
❓ How to add your datasets
This is motivated by gnn-benchmark
from graphgallery.data import Graph
# Load the adjacency matrix A, attribute (feature) matrix X and labels vector y
# A - scipy.sparse.csr_matrix of shape [num_nodes, num_nodes]
# X - scipy.sparse.csr_matrix or numpy.ndarray of shape [num_nodes, num_feats]
# y - numpy.ndarray of shape [num_nodes]
mydataset = Graph(adj_matrix=A, attr_matrix=X, label=y)
# save dataset
mydataset.to_npz('path/to/mydataset.npz')
# load dataset
mydataset = Graph.from_npz('path/to/mydataset.npz')
⭐ Road Map
- Add PyTorch trainers support
- Add other frameworks (PyG and DGL) support
- set tensorflow as optional dependency when using graphgallery
- Add more GNN trainers
- Support for more tasks, e.g.,
graph Classification
andlink prediction
- Support for more types of graphs, e.g., Heterogeneous graph
- Add Docstrings and Documentation (Building)
- Comprehensive tutorials
❓ FAQ
Please fell free to contact me if you have any troubles.
😘 Acknowledgement
This project is motivated by Pytorch Geometric, Stellargraph and DGL, etc., and the original implementations of the authors, thanks for their excellent works!
Cite
Please cite our paper (and the respective papers of the methods used) if you use this code in your own work:
@inproceedings{li2021graphgallery,
author = {Jintang Li and Kun Xu and Liang Chen and Zibin Zheng and Xiao Liu},
booktitle = {2021 IEEE/ACM 43rd International Conference on Software Engineering: Companion Proceedings (ICSE-Companion)},
title = {GraphGallery: A Platform for Fast Benchmarking and Easy Development of Graph Neural Networks Based Intelligent Software},
year = {2021},
pages = {13-16},
publisher = {IEEE Computer Society},
address = {Los Alamitos, CA, USA},
}