CF-GNNExplainer: Counterfactual Explanations for Graph Neural Networks

December 19, 2025 ยท View on GitHub

This repository is the official implementation of the AISTATS 2022 paper CF-GNNExplainer: Counterfactual Explanations for Graph Neural Networks.

Requirements

To install requirements:

conda env create --file environment.yml

๐Ÿ“‹ This will create a conda environment called pytorch-geo

Training original models

To train the original GNN models for the BA-shapes dataset in the paper, cd into src and run this command:

python train.py --dataset=syn1

๐Ÿ“‹ For the Tree-Cycles dataset, the dataset argument should be "syn4". For the Tree-Grid dataset, it should be "syn5". All hyperparameter settings are listed in the defaults, and all models have the same hyperparameters.

Training CF-GNNExplainer

To train CF-GNNExplainer for each dataset, run the following commands:

python main_explain.py --dataset=syn1 --lr=0.1 --beta=0.5 --n_momentum=0.9 --optimizer=SGD
python main_explain.py --dataset=syn4 --lr=0.1 --beta=0.5 --optimizer=SGD
python main_explain.py --dataset=syn5 --lr=0.1 --beta=0.5 --optimizer=SGD

๐Ÿ“‹ This will create another folder in the main directory called 'results', where the results files will be stored.

Evaluation

To evaluate the CF examples, run the following command:

python evaluate.py --path=../results/<NAME OF RESULTS FILE>

๐Ÿ“‹ This will print out the values for each metric.

Pre-trained Models

The pretrained models are available in the models folder

Results

Our model achieves the following performance:

Model nameDatasetFidelitySizeSparsityAccuracy
CF-GNNExplainerTree-Cycles0.212.090.900.94
CF-GNNExplainerTree-Grid0.071.470.940.96
CF-GNNExplainerBA-Shapes0.392.390.990.96