Home

Awesome

GAT - Graph Attention Network (PyTorch) :computer: + graphs + :mega: = :heart:

This repo contains a PyTorch implementation of the original GAT paper (:link: Veličković et al.). <br/> It's aimed at making it easy to start playing and learning about GAT and GNNs in general. <br/>

Table of Contents

What are GNNs?

Graph neural networks are a family of neural networks that are dealing with signals defined over graphs!

Graphs can model many interesting natural phenomena, so you'll see them used everywhere from:

and all the way to particle physics at Large Hedron Collider (LHC), fake news detection and the list goes on and on!

GAT is a representative of spatial (convolutional) GNNs. Since CNNs had a tremendous success in the field of computer vision, researchers decided to generalize it to graphs and so here we are! :nerd_face:

Here is a schematic of GAT's structure:

<p align="center"> <img src="data/readme_pics/GAT_schematic.PNG" width="600"/> </p>

Cora visualized

You can't just start talking about GNNs without mentioning the single most famous graph dataset - Cora.

Nodes in Cora represent research papers and the links are, you guessed it, citations between those papers.

I've added a utility for visualizing Cora and doing basic network analysis. Here is how Cora looks like:

<p align="center"> <img src="data/readme_pics/cora_graph_jupyter.PNG"/> </p>

Node size corresponds to its degree (i.e. the number of in/outgoing edges). Edge thickness roughly corresponds to how "popular" or "connected" that edge is (edge betweennesses is the nerdy term check out the code.)

And here is a plot showing the degree distribution on Cora:

<p align="center"> <img src="data/readme_pics/cora_degree_statistics.PNG" width="850"/> </p>

In and out degree plots are the same since we're dealing with an undirected graph.

On the bottom plot (degree distribution) you can see an interesting peak happening in the [2, 4] range. This means that the majority of nodes have a small number of edges but there is 1 node that has 169 edges! (the big green node)

Attention visualized

Once we have a fully-trained GAT model we can visualize the attention that certain "nodes" have learned. <br/> Nodes use attention to decide how to aggregate their neighborhood, enough talk, let's see it:

<p align="center"> <img src="data/readme_pics/attention1.jpg" width="600"/> </p>

This is one of Cora's nodes that has the most edges (citations). The colors represent the nodes of the same class. You can clearly see 2 things from this plot:

Similar rules hold for smaller neighborhoods. Also notice the self edges:

<p align="left"> <img src="data/readme_pics/attention2.jpg" width="400"/> <img src="data/readme_pics/attention4.jpg" width="400"/> </p>

On the other hand PPI is learning much more interesting attention patterns:

<p align="left"> <img src="data/readme_pics/neighborhood_attention_ppi/3.jpg" width="400"/> <img src="data/readme_pics/neighborhood_attention_ppi/2.jpg" width="400"/> </p>

On the left we can see that 6 neighbors are receiving a non-negligible amount of attention and on the right we can see that all of the attention is focused onto a single neighbor.

Finally 2 more interesting patterns - a strong self edge on the left and on the right we can see that a single neighbor is receiving a bulk of attention whereas the rest is equally distributed across the rest of the neighborhood:

<p align="left"> <img src="data/readme_pics/neighborhood_attention_ppi/4.jpg" width="400"/> <img src="data/readme_pics/neighborhood_attention_ppi/1.jpg" width="400"/> </p>

Important note: all of the PPI visualizations are only possible for the first GAT layer. For some reason the attention coefficients for the second and third layers are almost all 0s (even though I achieved the published results).

Entropy histograms

Another way to understand that GAT isn't learning interesting attention patterns on Cora (i.e. that it's learning const attention) is by treating the node neighborhood's attention weights as a probability distribution, calculating the entropy, and accumulating the info across every node's neighborhood.

We'd love GAT's attention distributions to be skewed. You can see in orange how the histogram looks like for ideal uniform distributions, and you can see in light blue the learned distributions - they are exactly the same!

<p align="center"> <img src="data/readme_pics/entropy_histograms/layer_0_head_0.jpg" width="400"/> <img src="data/readme_pics/entropy_histograms/layer_1_head_0.jpg" width="400"/> </p>

I've plotted only a single attention head from the first layer (out of 8) because they're all the same!

On the other hand PPI is learning much more interesting attention patterns:

<p align="left"> <img src="data/readme_pics/entropy_histograms_ppi/layer_0_head_0.jpg" width="400"/>

As expected, the uniform distribution entropy histogram lies to the right (orange) since uniform distributions have the highest entropy.

Analyzing Cora's embedding space (t-SNE)

Ok, we've seen attention! What else is there to visualize? Well, let's visualize the learned embeddings from GAT's last layer. The output of GAT is a tensor of shape = (2708, 7) where 2708 is the number of nodes in Cora and 7 is the number of classes. Once we project those 7-dim vectors into 2D, using t-SNE, we get this:

<p align="center"> <img src="data/readme_pics/t-sne.PNG" width="600"/> </p>

We can see that the nodes with the same label/class are roughly clustered together - with these representations it's easy to train a simple classifier on top that will tell us which class the node belongs to.

Note: I've tried UMAP as well but didn't get nicer results + it has a lot of dependencies if you want to use their plot util.

Setup

So we talked about what GNNs are, and what they can do for you (among other things). <br/> Let's get this thing running! Follow the next steps:

  1. git clone https://github.com/gordicaleksa/pytorch-GAT
  2. Open Anaconda console and navigate into project directory cd path_to_repo
  3. Run conda env create from project directory (this will create a brand new conda environment).
  4. Run activate pytorch-gat (for running scripts from your console or setup the interpreter in your IDE)

That's it! It should work out-of-the-box executing environment.yml file which deals with dependencies. <br/>


PyTorch pip package will come bundled with some version of CUDA/cuDNN with it, but it is highly recommended that you install a system-wide CUDA beforehand, mostly because of the GPU drivers. I also recommend using Miniconda installer as a way to get conda on your system. Follow through points 1 and 2 of this setup and use the most up-to-date versions of Miniconda and CUDA/cuDNN for your system.

Usage

Option 1: Jupyter Notebook

Just run jupyter notebook from you Anaconda console and it will open up a session in your default browser. <br/> Open The Annotated GAT.ipynb and you're ready to play!


Note: if you get DLL load failed while importing win32api: The specified module could not be found <br/> Just do pip uninstall pywin32 and then either pip install pywin32 or conda install pywin32 should fix it!

Option 2: Use your IDE of choice

You just need to link the Python environment you created in the setup section.

Training GAT

FYI, my GAT implementation achieves the published results:


Everything needed to train GAT on Cora is already setup. To run it (from console) just call: <br/> python training_script_cora.py

You could also potentially:

The code is well commented so you can (hopefully) understand how the training itself works. <br/>

The script will:

Same goes for training on PPI, just run python training_script_ppi.py. PPI is much more GPU-hungry so if you don't have a strong GPU with at least 8 GBs you'll need to add the --force_cpu flag to train GAT on CPU. You can alternatively try reducing the batch size to 1 or making the model slimmer.

You can visualize the metrics during the training, by calling tensorboard --logdir=runs from your console and pasting the http://localhost:6006/ URL into your browser:

<p align="center"> <img src="data/readme_pics/val_loss.PNG" height="290"/> <img src="data/readme_pics/val_acc.PNG" height="290"/> </p>

Note: Cora's train split seems to be much harder than the validation and test splits looking at the loss and accuracy metrics.

Having said that most of the fun actually lies in the playground.py script.

Tip for understanding the code

I've added 3 GAT implementations - some are conceptually easier to understand some are more efficient. The most interesting and hardest one to understand is implementation 3. Implementation 1 and implementation 2 differ in subtle details but basically do the same thing.

Advice on how to approach the code:

Profiling GAT

If you want to profile the 3 implementations just set the the playground_fn variable to PLAYGROUND.PROFILE_GAT in playground.py.

There are 2 params you may care about:

The results will get stored in data/ in memory.dict and timing.dict dictionaries (pickle).

Note: implementation #3 is by far the most optimized one - you can see the details in the code.


I've also added profile_sparse_matrix_formats if you want to get some familiarity with different matrix sparse formats like COO, CSR, CSC, LIL, etc.

Visualization tools

If you want to visualize t-SNE embeddings, attention or embeddings set the playground_fn variable to PLAYGROUND.VISUALIZE_GAT and set the visualization_type to:

And you'll get crazy visualizations like these ones (VisualizationType.ATTENTION option):

<p align="center"> <img src="data/readme_pics/attention3.jpg" width="410"/> <img src="data/readme_pics/kk_layout.jpg" width="410"/> </p>

On the left you can see the node with the highest degree in the whole Cora dataset.

If you're wondering about why these look like a circle, it's because I've used the layout_reingold_tilford_circular layout which is particularly well suited for tree like graphs (since we're visualizing a node and its neighbors this subgraph is effectively a m-ary tree).

But you can also use different drawing algorithms like kamada kawai (on the right), etc.

Feel free to go through the code and play with plotting attention from different GAT layers, plotting different node neighborhoods or attention heads. You can also easily change the number of layers in your GAT, although shallow GNNs tend to perform the best on small-world, homophilic graph datasets.


If you want to visualize Cora/PPI just set the playground_fn to PLAYGROUND.VISUALIZE_DATASET and you'll get the results from this README.

Hardware requirements

HW requirements are highly dependent on the graph data you'll use. If you just want to play with Cora, you're good to go with a 2+ GBs GPU.

It takes (on Cora citation network):

Compare this to hardware needed even for the smallest of transformers!

On the other hand the PPI dataset is much more GPU-hungry. You'll need a GPU with 8+ GBs of VRAM, or you can reduce the batch size to 1 and make the model "slimmer" and thus try to reduce the VRAM consumption.

Future todos:

If you have an idea of how to implement GAT using PyTorch's sparse API please feel free to submit a PR. I personally had difficulties with their API, it's in beta, and it's questionable whether it's at all possible to make an implementation as efficient as my implementation 3 using it.

Secondly, I'm still not sure why is GAT achieving reported results on PPI while there are some obvious numeric problems in deeper layers as manifested by all attention coefficients being equal to 0.

Learning material

If you're having difficulties understanding GAT I did an in-depth overview of the paper in this video:

<p align="left"> <a href="https://www.youtube.com/watch?v=uFLeKkXWq2c" target="_blank"><img src="https://img.youtube.com/vi/uFLeKkXWq2c/0.jpg" alt="The GAT paper explained" width="480" height="360" border="10" /></a> </p>

I also made a walk-through video of this repo (focusing on the potential pain points), and a blog for getting started with Graph ML in general! :heart:

I have some more videos which could further help you understand GNNs:

Acknowledgements

I found these repos useful (while developing this one):

Citation

If you find this code useful, please cite the following:

@misc{Gordić2020PyTorchGAT,
  author = {Gordić, Aleksa},
  title = {pytorch-GAT},
  year = {2020},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/gordicaleksa/pytorch-GAT}},
}

Licence

License: MIT