Enhancing Chemical Explainability Through Counterfactual Masking
Łukasz Janisiów, Marek Kochańczyk, Bartosz Zieliński, Tomasz Danel · 2025
Read Enhancing Chemical Explainability Through Counterfactual Masking by Łukasz Janisiów, Marek Kochańczyk, Bartosz Zieliński, Tomasz Danel (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2508-18561.txt.
Introduction
Janisiow, Kochanczyk, Zielinski, and Danel propose counterfactual masking to explain chemical models in twenty twenty-five. Models that predict the traits of molecules guide the search for new drugs and new materials. Tools that explain such models point to the parts of a molecule that drive a prediction. Many of those tools mask atoms, and that step creates molecules that could not exist.
Their method swaps a masked part for a fragment that could occur. A generative model picks the fragment so the molecule stays chemically sound. The team then judges the result against real molecules of that kind, not against a blanked out baseline. That keeps the account in line with the data the model was trained on. The swapped versions also show how a change in structure may shift the trait in question. Tests across data sets gave insights that chemists can act on in design work.
Catalog record
| CREATOR | Łukasz Janisiów, Marek Kochańczyk, Bartosz Zieliński, Tomasz Danel |
|---|---|
| YEAR | 2025 |
| COLLECTION | PAPERS |
| FORMAT | TXT |
| FILE SIZE | 53 KB |
| VERSION | 1.0 |
| LICENSE | http://creativecommons.org/licenses/by/4.0/ |
| RIGHTS | CC BY |
| SOURCE | arXiv |
Source: https://arxiv.org/abs/2508-18561
Browse the full library · About the Tower of Babel
Full text
The opening of the work, from the library's hosted text:
Enhancing Chemical Explainability Through Counterfactual Masking Łukasz Janisiów, Marek Kochańczyk, Bartosz Zieliński, Tomasz Danel Abstract Molecular property prediction is a crucial task that guides the design of new compounds, including drugs and materials. While explainable artificial intelligence methods aim to scrutinize model predictions by identifying influential molecular substructures, many existing approaches rely on masking strategies that remove either atoms or atom-level features to assess importance via fidelity metrics. These methods, however, often fail to adhere to the underlying molecular distribution and thus yield unintuitive explanations. In this work, we propose counterfactual masking, a novel framework that replaces masked substructures with chemically reasonable fragments sampled from generative models trained to complete molecular graphs. Rather than evaluating masked predictions against implausible zeroed-out baselines, we assess them relative to counterfactual molecules drawn from the data distribution. Our method offers two key benefits: (1) molecular realism underpinning robust and distribution-consistent explanations, and (2) meaningful counterfactuals that directly indicate how structural modifications may affect predicted properties. We demonstrate that counterfactual masking is well-suited for benchmarking model explainers and yields more actionable insights across multiple datasets and property prediction tasks. Our approach bridges the gap between explainability and molecular design, offering a principled and generative path toward explainable machine learning in chemistry. Introduction Molecular property prediction has emerged as a cornerstone of modern drug discovery and materials science, promising to dramatically accelerate the identification of compounds with desired characteristics. By leveraging machine learning algorithms to predict properties such as binding affinity, toxicity, solubility, and biological activity, researchers can efficiently navigate the vast chemical space without exhaustive experimental testing ( Wieder et al. 2020 ; Dara et al. 2022 ) . However, the increasing complexity of state-of-the-art models presents a significant challenge: while deep neural networks and ensemble methods deliver impressive predictive performance, they often function as inscrutable “black boxes,” offering little insight into the structural features driving their predictions ( Biecek and Samek 2024 ; Longo et al. 2024 ) . This opacity is particularly problematic in chemistry and pharmaceutical development, where understanding structure–property relationships is essential for rational molecular design, regulatory approval, and scientific knowledge advancement. Interpretable models that can explain their predictions in chemically meaningful terms are, therefore, not merely desirable but necessary to bridge the gap between statistical performance and actionable scientific insights ( Jimenez-Luna, Grisoni, and Schneider 2020 ; Wong et al. 2024 ) . Figure 1: Comparison between zero-feature masking and our CM method. The former strategy preserves the topology of the masked fragment, leading to a potential predictive bias. CM generates multiple fragment replacements, enabling more robust and trustworthy evaluation of model explanations. Graph masking has emerged as a fundamental technique for explaining molecular property predictions, serving both as the foundation for certain explainable AI (XAI) methods and as the basis for evaluation metrics like fidelity ( Bugueño, Biswas, and de Melo 2024 ) . By systematically removing or obscuring specific atoms or substructures, masking approaches attempt to quantify each component’s contribution to the final prediction. However, conventional masking strategies suffer from critical limitations in the molecular domain. When atoms or bonds are naively masked, the resulting structures often become chemically implausible or physically impossible, generating examples that fall outside the training distribution. This out-of-distribution problem undermines the reliability of both the explanations and their evaluation metrics. Furthermore, current masking approaches inadvertently leak information about the original graph topology to the model—even when certain atoms are “masked,” their structural relationships and connectivity patterns remain implicitly encoded in the modified graph. This information leakage creates a false sense of explanation quality, as models may still leverage these implicit structural cues rather than truly operating without knowledge of the masked components. Developing more sophisticated masking techniques that preserve chemical validity while effectively controlling information flow is therefore essential for advancing trustworthy explanations in molecular machine learning. Current approaches to molecular property prediction primarily rely on post-hoc explanation methods such as gradient-based attribution, attention mechanisms, and feature importance scores ( Pope et al. 2019 ) . These techniques attempt to highlight atoms or substructures that significantly influence predictions, providing chemists with visual maps of “important” molecular regions. However, conventional explanation methods struggle to address the holistic nature of molecular design, where virtually all components contribute to functionality through precise atom arrangements. Identifying which atoms are “important” offers limited insight when the entire structure has been carefully optimized. What chemists truly need is to understand why these structural elements matter and how they might be modified to achieve the desired properties. This gap between highlighting important features and providing actionable insights motivates the development of counterfactual explanations, which instead focus on answering the critical question of what other molecular design choices would lead to a change in chemical properties, e.g., higher potency or lower toxicity ( Gleeson et al. 2011 ; Guengerich 2011 ) . In this paper, we present counterfactual masking (CM) for molecular graphs, a novel method designed to enhance the explanations of machine learning model predictions (Figure 1 ). For every key molecular fragment identified with the explanation method, we generate a set of counterfactual explanations that replace the original fragment. This visually illustrates the importance of these fragments in relation to specific properties. Our approach aims to provide clearer insights into why certain molecular features contribute to predictive outcomes. The contribution of this paper can be summarized as follows: 1. We introduce a new masking method for molecular graphs that hides molecular fragments by replacing them with a set of alternative generated fragments, ensuring that masked molecules are valid in-distribution molecules. 2. We use our masking technique to provide additional insights for the explanation model by producing counterfactual explanations that show why these structures might be important for the model. The source code and accompanying data are available at https://github.com/gmum/counterfactual-masking . Related Work Factual explanations. Factual explanation methods for GNNs can be broadly categorized into gradient-based, perturbation-based, surrogate-based, and self-interpretable approaches. Gradient-based techniques, such as CAM ( Zhou et al. 2016 ) , Grad-CAM ( Selvaraju et al. 2017 ) , and Integrated Gradients ( Sundararajan, Taly, and Yan 2017 ) , estimate feature relevance through backpropagation. Perturbation-based methods, including GNNExplainer ( Ying et al. 2019 ) and SubgraphX ( Yuan et al. 2021 ) , generate explanations by learning masks or searching over subgraphs that are most influential to model predictions. In contrast, self-interpretable GNNs attempt to provide inherently explainable reasoning by embedding interpretability directly into the architecture ( Zhang et al. 2022 ) . While factual methods assign importance scores to nodes, edges, or subgraphs, they often lack mechanisms to verify the factual correctness of the highlighted structures on real-world datasets. Moreover, most approaches offer limited insight into why a particular structure is deemed important. Counterfactual explanations. Unlike factual methods, counterfactual explanations provide additional insight into model predictions by showing similar instances that are predicted differently from the original. In chemistry, research on counterfactual methods is still limited compared to other fields, but several methods have been proposed. Wellawatte, Seshadri, and White 2022 introduced MMACE, a model-agnostic approach that generates counterfactual examples by insertions, replacements, or deletions in the SELFIES ( Krenn et al. 2020 ) representation of the compound. Numeroso and Bacciu 2021 proposed MEG, a method