Let’s Talk About Graph Neural Network Python Libraries! | by Rohith Teja | Towards Data Science

The growing popularity of Graph Neural Network (GNNs) gave us a bunch of python libraries to work with.
As I was dealing with GNNs for quite a while, I have secured hands-on experience on some popular GNN python libraries and thought of making a small comparison between them.
In this article, we will pick a Node Classification task (a simple one of course!) and use 3 different python libraries to formulate and solve the problem.
The libraries that we are going to use:
Deep Graph Library (DGL) — built on PyTorch, TensorFlow and MXNetPyTorch Geometric (PyG) — built on PyTorchSpektral — built on Keras/ TensorFlow 2
Deep Graph Library (DGL) — built on PyTorch, TensorFlow and MXNet
PyTorch Geometric (PyG) — built on PyTorch
Spektral — built on Keras/ TensorFlow 2
Please refer to the installation guides in the official websites of these libraries.
Since I want to keep it simple, I will use the popular Zachary’s Karate Club graph dataset. Here, the nodes represent 34 students who were involved in the club and the links represent 78 different interactions between pairs of members outside the club. There are two different types of labels i.e, the two factions.
Node Classification: In this task, our aim is to build a model to predict the labels of the nodes i.e, the factions joined by the students.
We divide the graph into train and test sets (in the ratio 70:30) using a specific seed value. The same train and test splits will be used to build predictive models with different libraries so that we can have a comparison.
Once we have chosen the task, there are 6 simple steps to build a predictive model:
Data preparationBuild the custom datasetChoose a GNN methodTrain the modelPerform hyperparameter tuningPredict the node labels on test data
Data preparation
Build the custom dataset
Choose a GNN method
Train the model
Perform hyperparameter tuning
Predict the node labels on test data
All three python libraries have an implementation where we can transform our graph into the library’s custom dataset and use it to train the Graph Neural Network model.
Firstly, we will generate some node embeddings that can be used as input to the Graph Neural Network.
I chose DeepWalk node embedding technique which is based on the Random Walk concept. Graph Embedding python library will be used to build the DeepWalk model.
Firstly, install the Graph Embedding library and run the setup:
!git clone https://github.com/shenweichen/GraphEmbedding.gitcd GraphEmbedding/!python setup.py install
The embeddings are saved in the embeddings dictionary variable. Now we have a job of creating the custom datasets for the libraries.
I have already explained the process for PyTorch Geometric library in this Medium article that I published. Feel free to check it!
towardsdatascience.com
In this article, I will focus on DGL and Spektral.
In the custom dataset, we add the graph information such as node embeddings, edges, train and test masks.
As the Spektral library is based on Keras, we need to convert the labels into one-hot vectors. The rest of the process of including graph information in the custom dataset and also the train and test masks are similar.
For this experiment. I am choosing Graph Convolutional Network (GCN) which is popular among GNNs.
Let us create the GCN model using both libraries.
For the GCN model, I used the number of neurons in the hidden layer to be 16.
Since there are only two factions/ labels, we use the binary cross-entropy as the loss function.
We created the custom datasets for both libraries and also instantiated the GCN models, so now we can start training the model and predict the labels of the test set.
After training the model for 100 epochs, the test labels were predicted and the metrics were calculated. We obtained the following scores:
Train Accuracy: 1.0 Test Accuracy: 0.90
Since we used the same dataset, model and hyperparameters, we obtained the same results with both libraries.
The core implementation of GCN in these libraries is the same!
Train Accuracy: 1.0 Test Accuracy: 0.90
This is a toy example so I did not go much in detail with hyperparameter tuning. If you are using a bigger graph with multiple labels for nodes, then you need to perform hyperparameter tuning to find the best model which fits your graph data.
You just need to add a validation set and create the corresponding validation mask in the custom dataset. Then, train the model on the training set and using the validation metrics, tune the hyperparameters. Select the best model based on the model corresponding to the highest validation metric (like accuracy score/ F1 score). This selected model will be used to predict the labels on the test set and final metrics will be calculated.
All three libraries are good but I prefer PyTorch Geometric to model the Graph Neural Networks. I might be biased with the choice of the library as I worked extensively with PyG but this library has a good collection of GNN models, which the other libraries are lacking on.
So my ranking of these libraries is as follows:
PyTorch GeometricDeep Graph LibrarySpektral
PyTorch Geometric
Deep Graph Library
Spektral
Again, I am a bit biased to using PyTorch to train neural networks. If you are someone who likes to work with Keras and TensorFlow to construct neural networks, then looking into Spektral might be a good idea.
Also, keep in mind that DGL offers other backend support such as MXNet and TensorFlow in addition to the PyTorch.
The code that I used to formulate the Node Classification task is taken from the examples present in the repositories/ websites of the libraries. I took enough liberty to modify the code to make it useable for the Karate Club dataset. Below you will find the links for examples that will be useful for your reference:
DGL Custom Dataset — https://docs.dgl.ai/en/0.6.x/new-tutorial/6_load_data.htmlDGL GCN — https://docs.dgl.ai/tutorials/blitz/1_introduction.html.Spektral Custom Dataset — https://graphneural.network/creating-dataset/Spektral GCN — https://github.com/danielegrattarola/spektral/blob/master/examples/node_prediction/citation_gcn.pyDeepWalk embeddings — https://github.com/shenweichen/GraphEmbedding/blob/master/examples/deepwalk_wiki.py
DGL Custom Dataset — https://docs.dgl.ai/en/0.6.x/new-tutorial/6_load_data.html
DGL GCN — https://docs.dgl.ai/tutorials/blitz/1_introduction.html.
Spektral Custom Dataset — https://graphneural.network/creating-dataset/
Spektral GCN — https://github.com/danielegrattarola/spektral/blob/master/examples/node_prediction/citation_gcn.py
DeepWalk embeddings — https://github.com/shenweichen/GraphEmbedding/blob/master/examples/deepwalk_wiki.py
Note: Some part of my DGL code was influenced by the PyTorch Geometric library as both libraries use PyTorch as backend.
If you have reached this part of the post, thank you for reading and also your attention. I hope you found the post informative and if you have any questions feel free to reach me on LinkedIn, Twitter or GitHub.