lukasjf / contrastive-gnn-explanation

18 stars 1 forks source link

Contrastive Graph Neural Network Explanation

This is the source code for the paper Contrastive Graph Neural Network Explanation

Required libraries

You can install the required libraries by running:

pip install -r requirements.txt

Recreating the benchmark dataset

Run the following command to recreate the datasets

python gen_dataset.py DATASET_NAME

DATASET_NAME can be one of the followings:

You can add a --sample-size option to change the number of samples for each label (default is 1000).

Training a GNN model

We use GNNExplainer source code for training the model, making node embeddings, and generating the explanation.
Any file or folder with the prefix of gnnexplainer is ported from GNNExplainer repo with small modifications to make it compatible with our framework.
To train a model, you can run the following command:

python gnnexplainer_train.py --bmname=DATASET_NAME --epochs=20

The following command will output a model in ckpt folder which you will use in subsequent commands.

Generating node embeddings and GNNExplainer output

Run the following command for generating node embeddings for all graphs as well as GNNExplainer explanation

python gnnexplainer_main.py --bmname=DATASET_NAME --graph-mode --explain-all

This will create a new folder with name embeddings-DATASET_NAME and explanations in explanations/gnnexplainer folder.

Running other explanation methods

You can run all the other explanation method via explain.py script

python explain.py contrast | sensitivity | occlusion | random

Use --help option to see all the available options for each command. For example

python explain.py contrast --help

Evaluation

Run the following command to see the accuracy of explanations

python evaluate.py DATASET_PATH EXPLAIN_PATH

Complete example

Here is the complete list of commands needed to reproduce the paper results:

python gen_dataset.py CYCLIQ

python gnnexplainer_train.py --bmname=CYCLIQ --epochs=100
python gnnexplainer_main.py --bmname=CYCLIQ --graph-mode --explain-all

python explain.py random data/CYCLIQ/ explanations/random
python explain.py sensitivity data/CYCLIQ/ ckpt/CYCLIQ_base_h20_o20.pth.tar explanations/sensitivity
python explain.py occlusion data/CYCLIQ/ ckpt/CYCLIQ_base_h20_o20.pth.tar explanations/occlusion
python explain.py contrast data/CYCLIQ/ embeddings-CYCLIQ explanations/contrast

python evaluate.py data/CYCLIQ/ explanations/gnnexplainer/
python evaluate.py data/CYCLIQ/ explanations/random/
python evaluate.py data/CYCLIQ/ explanations/sensitivity/
python evaluate.py data/CYCLIQ/ explanations/occlusion/
python evaluate.py data/CYCLIQ/ explanations/contrast/

Visualizing Explanations

You can run the Visualize.ipynb notebook for visualizing each method explanation

Qualitative analysis

For reproducing the qualitative experiments look into the notebooks in the qualitative folder.