MMGCF: Generating Counterfactual Explanations for Molecular Property Prediction via Motif Rebuild ()
1. Introduction
Deep learning models [1] [2] are highly effective in capturing nonlinear relationships between molecular structures and their functions, enabling diverse applications in chemistry, such as quantum property calculations [3] [4], chemical property predictions [5] [6], and drug screening [7] [8]. Although Graph Neural Networks (GNNs) [9] outperform traditional descriptor-based models in molecular property prediction [10], interpretability challenges remain unresolved. After predictions are generated, researchers often aim to understand why certain compounds are predicted to exhibit specific properties.
In our study, existing studies [11]-[13] on predictability of explainable properties are primarily explanations based on factual reasoning. These methods identify key features, such as atoms and bonds in the molecular graph, which influence the model’s outcomes. The selected key features are typically considered factual interpretations that provide sufficient information to yield the same predictions as the original instance.
However, we argue that these methods lack sufficient persuasiveness. One reason is that these methods fail to adequately account for the unique aspects of molecular structure, making it difficult to incorporate critical and complete substructures into their explanation graphs. In other words, fact-based explanations do not address the question: “For a specific molecule, what would happen to the molecular property prediction model’s performance if we slightly modified the molecular structure?”
A Motivating Example. Using Figure 1 as an example, the advantages of counterfactual analysis in explaining molecular property predictions are demonstrated, in comparison to methods based on factual reasoning.
![]()
Figure 1. Illustration of factual explanations (bottom left) and counterfactual explanations (bottom right). The red highlights indicate the given explanations.
In a molecular property prediction task, a GNN models the molecular graph and classifies the input as mutagenic. Factual explanations identify specific edges in the molecular graph as key features contributing to mutagenicity. As shown in Figure 1 (bottom left), factual explanations highlight the aromatic ring and other bonds in the molecular structure as critical contributors to the prediction. These explainers focus on subgraphs that contain sufficient information to make the same predictions. However, the selected subgraphs may include redundant nodes or edges, making them neither compact nor complete.
In contrast, counterfactual explanations iteratively perturb chemically significant motifs, such as benzene rings and sulfonyl hydroxide (-SO3H), ultimately converging on the minimal modification required to alter the prediction outcome. As shown in Figure 1 (bottom right), completely disrupting the sulfonyl hydroxide group, the molecule is rendered non-mutagenic, highlighting the critical role of this functional group. Such counterfactual explanations seek minimal perturbations to the input that result in a change in the prediction, thereby enabling differential inference before and after the perturbation to determine whether the structure-property mapping learned by GNNs aligns with established domain knowledge [14].
Our method and contribution. Recent advancements in counterfactual reasoning within artificial intelligence highlight the potential application of counterfactuals to GNN-based molecular property prediction. Most counterfactual methods rarely address chemical deep learning, as the complexity of molecular data poses significant challenges in generating counterfactual explanations.
To address these challenges, we propose MMGCF, a novel interpreter that integrates counterfactual reasoning with molecular motifs to enhance the interpretability of GNNs in predicting molecular properties. MMGCF aims to identify motifs that exhibit causal relationships with molecular properties, formulating the counterfactual perturbation problem as a differentiable motif generation task. Through motif perturbation, MMGCF establishes a framework for generating counterfactual explanations. Quantitative and qualitative analyses demonstrate that MMGCF provides reliable explanations for complex molecular data in classification and regression tasks, effectively capturing structural information compared to existing baselines.
In summary, the contributions of MMGCF are as follows:
・ MMGCF constructs motif trees based on chemical rules, enabling in-depth exploration of molecular motifs while preserving chemical integrity.
・ To the best of our knowledge, MMGCF is the first method in the field of chemistry to utilize motif-graph hierarchical information for counterfactual explanations.
・ MMGCF generates explanations for the decisions made by GNN-based molecular property prediction models, which assist researchers in facilitating human interpretation through comparisons of input instances and corresponding counterfactual samples.
・ MMGCF offers reliable explanations for complex molecular data in classification and regression tasks, effectively capturing structural information and surpassing existing baseline methods.
2. Related Work
2.1. Preliminaries of Graph Neural Networks
Deep graph networks leverage both the edge and node feature matrices to learn graph representations. Various GNNs, such as GCN [15], GAT [16], GraphSAGE [17], and GIN [18], have gained significant attention for molecular property prediction.
In this paper, we employ Relational Graph Convolutional Networks (RGCN) [19], which extend GCN by incorporating edge features into the message-passing mechanism.
The propagation operation can be calculated as
(1)
where is
the output of the previous layer, and
aggregate the information via
(2)
Here,
is the hidden vector of node
after
iterations,
denotes the neighbors of node
under bond
, where
denotes the set of bond types, and
is the weight for target node
.
2.2. Explainability in Graph Neural Networks
Existing methods [20]-[30] aim to decode the “black box” of GNNs by identifying important nodes or edges, such as text in tables, pixels in images, or nodes in graphs. These methods can be categorized into four types based on how importance scores are derived:
・ Gradient/Feature Perturbation-based Methods: These methods [20] [21] are straightforward but have the significant drawback of being susceptible to gradient explosion.
・ Perturbation-based Methods: These methods [22] [23] examine changes in predictions when input data is perturbed, allowing for the identification of important information.
・ Surrogate-based Methods: These methods employ [12] [24] simple interpretable surrogate models to approximate the predictions of complex deep models in local regions of the input space.
・ Decomposition-based Methods: These methods [25] analyze model parameters to reveal the relationships between features in the input space and the output predictions.
The aforementioned methods are instance-level explanation techniques.
Additionally, there are model-level explanation methods [26] [27] that generate novel graph structures tailored to specific properties. However, available generative model-based GNN interpreters have limited applicability, particularly in molecular graphs. As noted earlier, these methods rely on factual reasoning, where the important substructures they identify contain sufficient information to ensure that the output matches the original data. However, the identified substructures, while related to the output, may not necessarily represent the most compact structures, which constitutes a significant limitation of factual explanations.
2.3. Counterfactual Explanations
The concept of counterfactuals originates from mathematics and philosophy. It was first introduced by Kahneman and Miller in 1986 [28] and later defined by Woodward and Hitchcock [29] as an explanation of how differences in events or instances can lead to changes in outcomes. In other words, a counterfactual sample is one that is closest to the input but produces a different result. Counterfactual explanations are fundamentally grounded in the theory of causal manipulability [30], which focuses on manipulating outcomes through causal relationships. If a process is identified as a manipulation of an event, a causal relationship must exist.
Although both counterfactual and factual explanations can be used for predictive reasoning in GNNs, they fundamentally differ as they address distinct problems. CF-GNN-Explainer [31] introduces counterfactual reasoning to generate more compact yet crucial explanations for GNNs. CF2 [32] integrates counterfactual and factual reasoning to extract sufficient and necessary GNN explanations. GEM [33] considers Granger causality to provide valid explanations. MEG [34] trains a reinforcement learning-based generator to produce counterfactual explanations.
Additionally, it is essential to distinguish between contrastive explanations, adversarial attacks [35], and counterfactual explanations. Adversarial attacks aim to deceive the model, while contrastive and counterfactual explanations primarily seek to explain predictions. Both adversarial attacks and counterfactual explanations involve small perturbations to the input. However, adversarial attack methods typically make minimal changes to the entire graph with the goal of degrading overall model performance. Contrastive explanations offer insights by comparing a specific prediction with other potential outcomes, helping users understand why the model made the current prediction instead of an alternative. In contrast, counterfactual explanations focus on how the model’s predictions would change if the input data were altered.
3. Preliminary
Notations. Given a set of
molecular SMILES [36], we transform them using RDKit [37] into graphs
. Each molecular graph is represented as
, consisting of a set of atoms
and a set of bonds
. The atom feature matrix
represents the feature of each atom. Here,
denotes the total number of atoms, and
is the type of atoms. Additionally, an adjacency matrix
captures the feature of bonds with
representing the bond types,
signifies a bond of type
between atom
and atom
. Thus, the structural information of each graph
is encoded in several matrices.
Motif tree. Our method decomposes molecules into multiple fragments, referred to as “motifs”. These motifs are closely linked to the properties of the graph, which is shown to be of significant importance [38] [39]. The motifs are mapped into a motif tree by treating each motif as a node and the relative positional relationships between motifs as edges.
Formally, for a given graph
, a motif tree
with node set
and edge set
, where
is the number of motifs. Each motif
is a subgraph of
, such that
and
. The motifs are non-overlapping, meaning
.
![]()
Figure 2. Overview of MMGCF. (a) A motif tree is constructed where each color represents a distinct motif. Motif generation is performed iteratively based on the motifs, graph structure, and domain knowledge. (b) The schematic diagram illustrates the generative procedure. The molecular graph
is defined by the atomic feature matrix
and the adjacency tensor
. RGCN with ReLU activation function is employed to obtain the mean and variance of the Gaussian distribution. By sampling from this Gaussian distribution and converting the results into discrete features, the domain knowledge evaluates the validity of the atoms and bonds, ultimately leading to the generation of new molecules.
and
represent the scale function and the transformation function, respectively.
4. Method: MMGCF
This section details MMGCF, which generates counterfactual explanations in molecular property prediction tasks. It consists of two key components: 1) chemically-guided motif tree construction. Each molecule is transformed into a motif tree. 2) Generation of counterfactual explanations. This component identifies explanations for predictions by perturbing specific motifs. We design a counterfactual generation framework and constraint functions to facilitate optimization. After optimization, MMGCF generates counterfactual explanations for molecular predictions.
4.1. Chemically Guided Motif-Tree Construction
Given a molecule graph
, we convert it to a motif tree
. Each node in the structure represents a topic, while the edges indicate the relative spatial relationships between motifs. The decision to convert the molecular structure from a graph to a tree structure is motivated by its advantages for the task of counterfactual data generation. A tree-like representation enables a clearer capture of hierarchical relationships and spatial distributions between topics, thereby providing robust support for subsequent analysis and processing.
We utilize the “Breaking of Interesting Chemical Substructures” (BRICS) algorithm [40] for bond cleavage, in which cleavage rules retain molecular fragments that possess significant structural and functional components. This method may yield overly large pieces due to its reliance on a limited set of reactions. To address this issue, we establish additional rules to ensure the generation of more effective molecular motifs.
Algorithm 1 outlines the construction of motif trees.
and
respectively contain non-ring bonds and rings of each fragment, both extracted using RDKit functions. If two bonds
and
share common atoms (i.e.,
) and the number of shared atoms exceeds three, these bonds are merged into a new motif.
and
are removed from
and their intersection is added to
. Since the motif tree is not unique when atoms connect to multiple motifs, infinite weights are assigned to these edges in
to ensure that the tree structure remains logical and free from cycles. Finally, we construct the motif tree as
using

the maximum spanning tree method, which maximizes connectivity or network capacity. Edges with infinite weights enforce the formation of a single connected spanning tree.
4.2. Generation of Counterfactual Explanations
A counterfactual generation framework is developed for GNN-based predictions of molecular properties. The central idea involves identifying minimal perturbations to the molecular graph that can reverse these predictions. This objective is achieved by addressing a counterfactual optimization challenge, which will be elaborated below.
The generative framework. For a given molecule graph
, it is first converted into a motif tree
, where the structural information of each motif
is encoded as
. Since discrete data cannot be directly applied to continuous density models, a dequantization technique inspired by previous work [41] [42] is adopted. Specifically, noise from
is added to each atom
and each bond
, mapping the discrete data
into continuous data
.
(3)
The continuous data
is then fed into the generative framework
, which learns a bijection from the data space to a Gaussian distribution. A parameterized neural network is designed to guarantee that the learned data likelihood closely resembles a Gaussian distribution, comprising
layers of RGCN and ReLU activation functions. This network computes the mean
,
and variance
,
. Formally, the generated conditional distribution is defined as:
(4)
where
is the number of atoms in the given
.
Then, the data likelihood is modeled by using the change of variable formula [43]:
(5)
The probability of the given
can be calculated as:
(6)
So we get the negative log-likelihood of
:
(7)
To generate a new motif, random variables
and
from the Gaussian distribution and convert into discrete features:
(8)
To generate new motif trees from the root node, DFS (Depth-First Search) or BFS (Breadth-First Search) approaches are utilized, with the motif ordering saved as
.
The final step of our model is to reconstruct the molecular graph $G$ from the motif tree. Our goal is to assemble the motifs into the correct molecular structure. Given the ordering
,
is used as the reconstruction loss in the generated molecule. It is defined as:
(9)
Here,
represents the motifs in the specified arrangement and
denotes the concatenated edges between the motifs.
Counterfactual constraint. Leveraging the generation framework, any molecule is reconstructed based on perturbed motifs. MMGCF is employed to generate counterfactual explanations for molecular prediction tasks.
A counterfactual
is specific to a given molecule
. For the molecule
, there is a predicted output
.
The counterfactual serves as an explanation for
, defined as the solution to the following constrained optimization problem:
(10)
where
is the total number of generated counterfactual samples,
is calculated as the similarity of
and
,
is an indicator function that outputs 1 when the input condition is true, otherwise, it outputs 0.
Equation (10) is defined for classification tasks. However, it must be apt for regression tasks. Instead of seeking transformations in the labels, we look for counterfactuals that lead to increases or decreases in predictions. In this context, a problem-specific hyperparameter
represents the value change.
(11)
After being constrained, Equation (12) represents our final loss function.
(12)
5. Experimental and Result
In this section, we first introduce the datasets and the comparison baselines. Then, we report the main experimental results and the analyses.
5.1. Datasets and Baselines
Datasets. To assess the effectiveness of MMGCF in explaining predictions made by GNN models, we conducted experiments on four molecular datasets: concerning aqueous solubility (ESOL), mutagenicity, HIV activity, and blood-brain barrier permeability (BBBP).
![]()
Table 1. The performance of the consensus model and statistics of four molecule datasets. “#ave n” and “#ave e” represent the average number of nodes and edges per graph, respectively. “#graph” refers to the total number of graphs in the dataset.
Table 1 provides statistics for all the datasets used and shows the performance of RGCN as a consensus model in different data test sets and tasks.
・ Mutagenicity [44] [45] dataset includes 7672 compounds, each categorized as either mutagenic or non-mutagenic.
・ BBBP [46] dataset Comprising 1859 compounds, this dataset is used to predict whether a molecule can penetrate the blood-brain barrier, forming a binary classification task.
・ HIV [47] introduced by the Drug Therapeutics Program (DTP) AIDS Antiviral Screen, contains 41,122 compounds. Each compound is labeled as either active or inactive regarding HIV antiviral activity. A sample of 4943 molecules is selected from this dataset for our experiment.”
・ ESOL (aqueous solubility) [48] contains 1128 compounds and is commonly used for graph regression tasks. Aqueous solubility is one of the criteria for evaluating the absorption capacity of drug candidates.
Baselines. The following baselines were used for comparison:
1) Random: At each step, at most one motif is reconstructed for each graph being explained; 2) GNN-Explainer: GNN-Explainer [22] generates an edge mask to estimate the contribution of different edges to the model’s prediction. For counterfactual generation, the removal of subgraphs identified as important is performed; 3) CF2: CF2 [32] integrates counterfactual and factual reasoning to produce an edge mask that estimates edge importance; 4) MEG: MEG [34] explicitly incorporates domain-specific knowledge from chemistry and generates counterfactuals using reinforcement learning.
5.2. Evaluation Metrics
Validity [49]. The metric reflects the proportion of effective counterfactual explanations, where an explanation is deemed effective if it leads to a significant change in the sample’s prediction outcome.
(13)
The indicator serves as a critical measure of the practical applicability of our method. For classification tasks, the proportion of counterfactuals reflecting the desired labels is evaluated. For regression tasks, the proportion of counterfactuals leading to an increase or decrease in the predicted values is assessed.
Fidelity [50]. The metric measures the faithfulness of explanations to the oracle by evaluating how well the counterfactual differentiates from the original graph with respect to the ground truth label.
(14)
Here, for classification tasks,
, for regression tasks,
, and
is the ground truth label.
Time [50] : The average time cost for generating counterfactual explanations for each instance (in seconds).
5.3. Quantitative Analysis
The results of three metrics across various models for different tasks are presented in Table 2. 1) Validity. The proposed method consistently outperforms various models by generating chemical data interpretations that emphasize effective substructures rather than individual atomic nodes or edges. Baseline methods fail to effectively capture structure-property relationships due to the complexity of chemical data. MEG primarily assesses whether perturbations, such as the addition or removal of atoms, are validated by valence electrons, whereas GNN-Explainer and CF2 primarily generate edge masks. 2) Time. The time cost of MMGCF is relatively low compared to alternative methods. In contrast, MEG requires enumerating each perturbation operation, which significantly increases
![]()
Table 2. The performance (mean of ten repeated executions) of different models. The best results are highlighted in bold, while the runner-up results are underlined.
its computational cost. Although GNN-Explainer incurs low time costs, existing methods often fail to adequately account for the unique characteristics of molecular structures. This limitation makes it difficult for these methods to include critical substructures in their explanation graphs.
Table 3 presents a performance comparison of various methods on the Fidelity metric across four real-world datasets. Specifically, MEG exhibits relatively poor performance on all datasets, as its counterfactuals rely on atomic-node-level perturbations, which fail to effectively guide the counterfactual samples in altering the model’s predictions from the true labels. Although CF2 and GNN-Explainer demonstrate strong performance on certain datasets, their overall results are inferior to those of the method proposed in this study. The superior performance of our method is attributed to its unique strategy of generating explanations based on individual motifs. Compared to other models, our method achieves an average improvement of approximately 45.92%, underscoring its significant performance advantages.
This improvement underscores the importance of attributing each motif to the target property. Domain experts are particularly interested in understanding how case-based differential reasoning supports counterfactual predictions for specific molecules. In such cases, the model being explained may interpret similar structures as fundamentally different with respect to the predicted property. Counterfactual molecules generated through this approach enable experts to evaluate whether the structure-to-function mapping learned by the model aligns with established domain knowledge, at least within the immediate neighborhood of the molecules under investigation.
![]()
Table 3. Fidelity on four real datasets.
5.4. Qualitative Analysis
Figure 3 provides a qualitative evaluation of four methods applied to four datasets, with actual molecules from the datasets included for reference. The results demonstrate that MMGCF produces more meaningful motif-based explanations than the other methods. Moreover, it is shown that GNN-explainer, CF2, and MEG lack this ability.
MEG is relatively ambiguous in identifying true causal substructures. This ambiguity stems from its process of generating explanations by adding or
![]()
Figure 3. Qualitative Analysis. The leftmost column displays the instances, where the blurred edges represent important features identified by GNN-Explainer or CF2. For MEG and MMGCF, red highlights indicate added or modified structures, while blue highlights denote deletions. The probability shown beneath each graph or subgraph represents the likelihood of classification into the “Mutagenic,” “BBBP,” or “HIV activity” class, respectively. ESOL evaluates the absorption capacity of drug candidates.
removing nodes or edges, without assessing whether the perturbation is interpretable, whether the molecule loses its original properties due to structural disruption, or if other underlying factors are involved. As a result, the explanation lacks clarity and precision.
When focusing on the GNN-Explainer and CF2, it is consistently observed across all four datasets, for both classification and regression tasks, that these models fail to capture complete and compact explanatory subgraphs. This limitation stems from the fact that GNN-Explainer and CF2 provide sample-level explanations by masking edges of lower importance within the graph. The GNN-Explainer, CF2, and MEG methods are ineffective at identifying complex substructures, leading to less effective explanations. In contrast, our method generates more meaningful, accurate, and structurally coherent explanations.
6. Conclusion
In this work, we proposed MMGCF, a novel counterfactual explanation framework tailored for GNN-based molecular property prediction. By leveraging chemically guided motif tree construction and counterfactual reasoning, MMGCF bridges the gap between machine learning predictions and domain-specific chemical insights. Unlike existing approaches, which often lack chemical relevance or generalizability, MMGCF introduces a hierarchical motif-based framework that ensures interpretability while preserving molecular structural integrity. Through extensive experiments on four benchmark datasets, MMGCF demonstrated superior performance in generating reliable, causally meaningful explanations compared to state-of-the-art methods. It was particularly effective in identifying critical molecular motifs, providing chemists with actionable insights into the relationship between structural modifications and property variations. Future research can explore extending MMGCF to other graph-based domains, incorporating more sophisticated diversity constraints.
Acknowledgements
This work is supported by the State Key Program of the National Nature Science Foundation of China (61936001), the key cooperation project of Chongqing Municipal Education Commission (HZ2021008), the Natural Science Foundation of Chongqing (cstc2019jcyj-cxttX0002, cstc2021ycjhbgzxm0013).