Explainability Techniques for Chemical Language Models

Stefan Hödl, William Robinson, Yoram Bachrach, Wilhelm Huck, Tal KachmanarXiv 2023

TL;DR

Chemical language models excel at predicting molecular properties from text strings, but they operate as complete "black boxes." This paper introduces a novel explainability framework that backpropagates and aggregates attention scores across all Transformer layers to map exactly which atoms drive a model's predictions. By visualizing these weights directly on 2D molecular diagrams, researchers can finally audit what chemical language models are actually learning.

What Problem Does This Paper Address?

Deep learning has revolutionized molecular property prediction, particularly through "chemical language models" (CLMs) that treat molecular string representations (like SMILES) as text. However, these models are notoriously opaque. While explainable AI (XAI) techniques exist for graph neural networks (GNNs) or molecular fingerprints, they do not translate to string-based Transformers.

Previous attempts to visualize Transformer attention typically only look at the final layer, missing the rich context built up through earlier layers. Furthermore, standard pooling operations (like averaging token features to feed into a prediction head) destroy the token-specific gradients needed to trace a prediction back to individual atoms.

Methods

The authors adapt a layerwise attention aggregation framework (originally designed for computer vision) to the chemical domain using the MegaMolBART architecture.

Key innovations include:

  1. The <REG> Token: To avoid the "explainability wipeout" caused by standard average or sum pooling, the authors prepend a special <REG> token to the tokenized SMILES string. The regression head trains exclusively on this token's features, preserving a clear, un-pooled backpropagation path to every individual atom token.
  2. Relevance Aggregation: Instead of just looking at the last layer's attention, the method captures attention scores and gradients across all six Transformer layers. It filters for positive contributions (following deep Taylor decomposition principles) and computes a contextualized relevancy matrix.
  3. 2D Visualization: The token relevancies are mapped directly onto 2D molecular structures using RDKit, highlighting influential atoms in green.
Overview of the explainability pipeline mapping SMILES strings to 2D atom relevance highlights.

Datasets

  • AqueousSolu: A dataset of ~12,000 molecules used to evaluate water solubility predictions. The authors test on both a random 10% split and a curated "accurate" low-uncertainty test set (578 molecules).
  • CombiSolu-Exp: A dataset containing ~5,000 experimental measurements of solubility in various organic solvents to test solute-solvent interactions.

Both datasets are sourced from the larger, open-source SolProp data collection.

Key Insights and Ideas

  • Chemical Intuition Validated: For aqueous solubility, the model naturally places high relevance on hydrophilic functional groups like hydroxyl (-OH), carbonyl (=O), and amine (-NH2) groups, proving it captures basic chemistry principles.
  • The Carbon Backbone Shortcut: Surprisingly, the model often attributes high importance to the inert carbon backbone. The authors hypothesize that the model relies on these structural frameworks as a predictive shortcut learned during its massive self-supervised pretraining phase, rather than fully learning the underlying physical context.
  • Symmetry Blindness: The model frequently assigns wildly different relevance scores to chemically symmetric functional groups, revealing that CLMs struggle to inherently understand molecular symmetry when processing linear strings.
Figure 2: 2d structure plot with a color map corresponding to attributed relevance extracted from the <REG> token of the aggregated relevancy matrix R. The relevance of each atom token towards the model’s aqueous solubility prediction is visualized, the intensity of the green highlights corresponds to the attributed relevance. The color bar shows relative importance in arbitrary units.

Key Results

  • Performance Retained: Replacing standard pooling with the <REG> token achieved virtually identical accuracy, proving that explainability does not require sacrificing model performance.
  • Aqueous Solubility: On the AqueousSolu random test set, the model achieved a Mean Absolute Error (MAE) of 0.604 and a Root Mean Squared Error (RMSE) of 0.895. On the low-uncertainty "accurate" test set, performance improved to an MAE of 0.475 and RMSE of 0.638.
  • Solvent Interaction Limits: For organic solvents, a simple concatenation approach (<REG> solute <SEP> solvent) allowed the model to be explained but yielded nearly identical relevance maps across different solvents. This indicates the model failed to explicitly capture nuanced solute-solvent interactions, explaining why a non-interpretable cross-attention variant quantitatively outperformed it on this task.
Figure 3: Parity plots showing the predictive accuracy of the MegaMolBART model finetuned on the AqueousSolu dataset. The left plot (blue) shows a (10%) randomly selected test set, while the right plot (red) shows the low-uncertainty test set [48]. The experimental solubility measurements log(Saq,298K) (X axes) are plotted against the models’ predictions (Y axes), deviations from the diagonal represent the models’ errors. The predictions are grouped into hexagonal bins to highlight the density of predictions, the raw densities are shown as histograms on the sides of both plots. The grey lines show a linear regression fit and its “slope” with n samples in the test set. The run closest to the average of 5 runs with different seeds for the dataset split is shown.

Limitations and Future Work

  • Non-Atom Tokens Ignored: SMILES strings contain structural characters like brackets (for branches) and digits (for ring closures). Currently, the framework only maps relevance to atom tokens, discarding the structural context of non-atom tokens in the final 2D visualization.
  • Lack of Ground Truth: There are no objective "ground truth" relevance labels for molecular properties, making it difficult to quantitatively validate which XAI method is truly the most accurate.
  • SMILES Invariance: The model's predictions and explanations change when given different valid SMILES strings for the exact same molecule (non-canonical representations), highlighting a need for contrastive training to enforce representation invariance.

Summary updated

BibTeX
@misc{hodl2023explainability,
  author = {Stefan Hödl and William Robinson and Yoram Bachrach and Wilhelm Huck and Tal Kachman},
  title = {{Explainability Techniques for Chemical Language Models}},
  year = {2023},
  eprint = {2305.16192},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2305.16192},
}
Figure