Invariant Graph Transformer for Out-of-Distribution Generalization

Tianyin Liao, Ziwei Zhang, Yufei Sun, Chunyu Hu, Jianxin LiarXiv 2025 · DOI

Abstract

Graph Transformers (GTs) often struggle with distribution shifts where test data differs from training data. GOODFormer (Graph Out-Of-Distribution generalized Transformer) is a novel framework designed to address this by combining causal invariant learning with an evolving encoding mechanism. By disentangling stable, predictive graph patterns from misleading noise, GOODFormer sets a new state-of-the-art for out-of-distribution (OOD) generalization in graph machine learning.

Introduction

Most Graph Transformers assume that training and testing data come from the same distribution. In real-world scenarios, such as molecular property prediction, models often latch onto "spurious correlations"—patterns that correlate with labels in training but have no causal relationship. While invariant learning helps standard Graph Neural Networks (GNNs), applying it to GTs is challenging due to:

  1. Lack of Sharpness: Standard self-attention produces "blurry" weights that fail to clearly separate invariant parts of a graph from noisy ones.
  2. The Encoding Dilemma: GTs rely on Positional and Structural Encodings (PSEs). If a model dynamically identifies an invariant subgraph during training, these encodings must evolve, making traditional pre-computation impossible.

Methodology

GOODFormer introduces three mutually reinforcing modules to solve these challenges:

The GOODFormer architecture featuring the Invariant Subgraph Disentangler, Evolving PSE Encoder, and Invariant GT Learning module.
  • Entropy-guided Invariant Subgraph Disentangler: This module uses dual attention mechanisms to split the graph into "invariant" and "variant" subgraphs. An entropy-guided regularizer and temperature-based test-time training force the attention to be "sharp," carving out the predictive core.
  • Evolving Subgraph PSE Encoder: Instead of static encodings, GOODFormer uses a learnable MPNN-based encoder. It generates real-time encodings for subgraphs as they evolve, guided by original hand-crafted PSEs like Laplacian eigenvectors.
  • Invariant GT Learning Module: Based on causal theory, this module ensures the relationship between the invariant subgraph and the label remains consistent, regardless of the "variant" noise, mimicking a "do-calculus" intervention.

Experiments and Results

GOODFormer was evaluated on synthetic (GOOD-Motif) and real-world benchmarks (GOOD-HIV, DrugOOD, GOOD-SST2, GOOD-Twitter).

Quantitative Performance

GOODFormer consistently outperformed state-of-the-art baselines, including GraphGPS and EXPHORMER. On the GOOD-Motif "size" split, it achieved 80.02% accuracy, a 14.4% improvement over the strongest previous baseline. It also achieved superior results across all real-world datasets, proving its effectiveness in drug discovery and sentiment analysis.

Qualitative Precision

Visual comparison of identified invariant subgraphs against ground truth across different methods. Source, p. 7

Visual comparison of identified invariant subgraphs against ground truth across different methods.

As shown in Figure 2, qualitative visualizations demonstrate that GOODFormer is significantly more accurate at identifying the actual "ground truth" invariant subgraphs compared to other methods like GSAT or CIGA, which often include irrelevant edges or fail to capture the complete motif.

Conclusion

GOODFormer addresses fundamental weaknesses in Graph Transformers regarding OOD generalization. By ensuring attention sharpness and allowing structural encodings to evolve alongside subgraphs, it successfully isolates causal patterns. Future work will explore extending this approach to dynamic, heterogeneous, and hyper-graph structures.

Summary updated

BibTeX
@misc{liao2025invariant,
  author = {Tianyin Liao and Ziwei Zhang and Yufei Sun and Chunyu Hu and Jianxin Li},
  title = {{Invariant Graph Transformer for Out-of-Distribution Generalization}},
  year = {2025},
  eprint = {2508.00304},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2508.00304},
}
Figure