GithubHelp home page GithubHelp logo

graphit's Introduction

GraphiT: Encoding Graph Structure in Transformers

This repository implements GraphiT, described in the following paper:

Grégoire Mialon*, Dexiong Chen*, Margot Selosse*, Julien Mairal. GraphiT: Encoding Graph Structure in Transformers.
*Equal contribution

Short Description about GraphiT

Figure from paper

GraphiT is an instance of transformers designed for graph-structured data. It takes as input a graph seen as a set of its node features, and integrates the graph structure via i) relative positional encoding using kernels on graphs and ii) encoding local substructures around each node, e.g, short paths, before adding it to the node features. GraphiT is able to outperform Graph Neural Networks in different graph classification and regression tasks, and offers promising visualization capabilities for domains where interpretability is important, e.g, in chemoinformatics.

Installation

Environment:

numpy=1.18.1
scipy=1.3.2
Cython=0.29.23
scikit-learn=0.22.1
matplotlib=3.4
networkx=2.5
python=3.7
pytorch=1.6
torch-geometric=1.7

The train folds and model weights for visualization are already provided at the correct location. Datasets will be downloaded via Pytorch geometric.

To begin with, run:

cd GraphiT
. s_env

To install GCKN, you also need to run:

make

You also need to create a cache folder to store computed positional encoding

mkdir -p cache/pe

Training GraphiT on graph classification and regression tasks

All our experimental scripts are in the folder experiments. So to start with, run cd experiments.

Classification

To train GraphiT on NCI1 with diffusion kernel, run:

python run_transformer_cv.py --dataset NCI1 --fold-idx 1 --pos-enc diffusion --beta 1.0

Here --fold-idx can be varied from 1 to 10 to train on a specified training fold. To test a selected model, just add the --test flag.

To include Laplacian positional encoding into input node features, run:

python run_transformer_cv.py --dataset NCI1 --fold-idx 1 --pos-enc diffusion --beta 1.0 --lappe --lap-dim 8

To include GCKN path features into input node features, run:

python run_transformer_gckn_cv.py --dataset NCI1 --fold-idx 1 --pos-enc diffusion --beta 1.0 --gckn-path 5
Reproduction of our classification results

To reproduce our experimental results, you need to perform grid search to select the best model and retrain it. We have prepared a script to perform grid search and testing on a single machine for MUTAG with GCKN and adjacency encoding as an example. The results for other datasets and other encodings can be easily obtained by adapting the script.

cd scripts
bach -x cv_gckn_transformer.sh

You can modify the above script based on your server to conduct grid search on multiple machines. Once all experiments have been done, you can visualize the final results with

python results_gckn_transformer.py

Regression

To train GraphiT on ZINC, run:

python run_transformer.py --pos-enc diffusion --beta 1.0

To include Laplacian positional encoding into input node features, run:

python run_transformer.py --pos-enc diffusion --beta 1.0 --lappe --lap-dim 8

To include GCKN path features into input node features, run:

python run_transformer_gckn.py --pos-enc diffusion --beta 1.0 --gckn-path 8

Visualizing attention scores

To visualize attention scores for GraphiT trained on Mutagenicity, run:

cd experiments
python visu_attention.py --idx-sample 10

To visualize Nitrothiopheneamide-methylbenzene, choose 10 as sample index. To visualize Aminofluoranthene, choose 2003 as sample index. If you want to test for other samples (i.e, other indexes), make sure that the model correctly predicts mutagenicity (class 0) for this sample.

Citation

To cite GraphiT, please use the following Bibtex snippet:

@misc{mialon2021graphit,
      title={GraphiT: Encoding Graph Structure in Transformers}, 
      author={Gr\'egoire Mialon and Dexiong Chen and Margot Selosse and Julien Mairal},
      year={2021},
      eprint={2106.05667},
      archivePrefix={arXiv},
      primaryClass={cs.LG}
}

graphit's People

Contributors

gregoiremialon avatar claying avatar

Stargazers

zyzheng avatar  avatar  avatar  avatar  avatar Yoann Fleytoux avatar  avatar  avatar Jiaqing Xie avatar Jun Fu avatar Oxana Skarednova avatar  avatar Jianhao avatar Enze Wu avatar Lakhder Amine avatar Hsiang-Jen Li avatar Jeff Carpenter avatar Tian Xia avatar hhh avatar Jinrui Wei avatar Ajitabh Kumar avatar Mr.Chen avatar Niall Taylor avatar  avatar  avatar Tomás Fernandes avatar  avatar Christian cancedda avatar Zhuoran Jin avatar Gav Edwards avatar MinYoung Choe avatar Vishal Belsare avatar Krzysztof Sadowski avatar YangLixin avatar Ahmet Sarıgün avatar Geyan Ye avatar NewCoderQ avatar Liang Wang avatar Niharika Vadlamudi avatar Rishabh Anand avatar Derek Lim avatar Paul avatar  avatar Xing Su avatar nashid avatar PengZhang avatar Mar1o2W avatar ddelange avatar Nikita avatar Marc Lelarge avatar Seungwoo Ryu avatar Dimitrios Korkinof avatar Xiaojun Ma avatar  avatar Yi-Lun Liao avatar Zhuoran Qiao avatar  avatar Qi Chen avatar Feng Shi avatar  avatar Hejie Cui avatar Hannes Stärk avatar MuhammadAnwar avatar  avatar Zhang avatar Jeongwhan Choi avatar Chieh Chang avatar sssjjjaaa avatar Jing Huang avatar Jun Lee avatar Slice avatar  avatar Jean-Marc Alkazzi avatar  avatar Zeno avatar Jose Cohenca avatar Jianxing avatar Xinhao Li avatar  avatar Jinjing Zhou avatar  avatar  avatar Behrooz Azarkhalili avatar Oleksii Kachaiev avatar  avatar Rocío Mercado Oropeza avatar Ashish Sinha avatar Bharath Bhushan avatar Daniel Domingo-Fernández avatar Kun Zhan avatar Valentin Gabeur avatar 爱可可-爱生活 avatar Filip Andersson avatar Göran Sandström avatar  avatar Lacauste Hugo avatar Gustave Cortal avatar  avatar Quentin Bertrand avatar Julien Mairal avatar

Watchers

James Chang avatar  avatar nashid avatar Feng Shi avatar  avatar

graphit's Issues

how to use multi-dimensional edge attribute

Dear authors,
I'm a bit new to deep-learning and transformer chemist.
I've read the GraphiT paper and followed the code in this repository.
What I want is to use diffusion kernel with both symmetric and multi-dimensional edge attribute.
But it cause problem when the positional encoding.
Below is example of my data shape with batch
DataBatch(x=[267967, 40], edge_index=[2, 932790], edge_attr=[932790, 2], y=[50], pos=[267967, 3], batch=[267967], ptr=[51])
How can I solve this problem?

Best regards :)
Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.