Generating Counterfactual Hard Negative Samples for Graph Contrastive Learning

Haoran Yang, Hongxu Chen, Sixiao Zhang, Xiangguo Sun, Qian Li, Xiangyu Zhao, Guandong Xu

Introduction

Graph Contrastive Learning (GCL) has emerged as a dominant paradigm for unsupervised graph representation learning. By pulling "positive" pairs (different views of the same graph) together and pushing "negative" pairs (different graphs) apart in an embedding space, models learn to capture essential structural and semantic features. However, traditional GCL faces a significant "negative sampling" bottleneck. Most existing methods rely on random sampling from training batches, which frequently introduces false negatives (samples from the same class pushed apart) or easy negatives (samples so distinct that the model learns little from them).

To address these challenges, this paper introduces CGC (Counterfactual Graph Contrastive learning). CGC shifts the focus from random sampling to the generation of "hard" negative samples—artificial graphs that are structurally similar to the target but semantically distinct.

The CGC Framework

The core innovation of CGC is the use of counterfactual reasoning to generate negatives. Inspired by counterfactual explanations—which identify the minimum change needed to alter a prediction (e.g., "What is the smallest change to a loan application that would flip a rejection to an approval?")—CGC generates negatives by making minimal perturbations to a graph that result in a different predicted class.

Methodology

The framework employs two adaptive perturbation mechanisms to create these counterfactual twins:

  1. Proximity Perturbation: A trainable matrix modifies the graph's adjacency structure by adding or dropping edges and nodes.
  2. Feature Masking: A trainable mask selectively hides specific node features.
The CGC architecture: Counterfactual hard negative samples are generated through proximity perturbation and feature masking, then used in a contrastive loss pipeline.

The generation process is governed by a dual-objective optimization:

  • Similarity Constraint: Ensures the perturbed graph remains structurally and featurally close to the original (using Frobenius norms).
  • Counterfactual Objective: Uses a graph classifier and KL-Divergence to ensure the predicted label distribution of the new sample is as different as possible from the original.

By balancing these objectives, CGC creates samples that reside near the decision boundary, forcing the encoder to learn more discriminative and robust representations.

Experimental Evaluation

The authors evaluated CGC against state-of-the-art baselines (GraphCL, GCA, InfoGraph) across several TUDataset benchmarks, including PROTEINS_full, FRANKENSTEIN, Synthie, and ENZYMES.

Key Results

CGC consistently outperformed both GCL baselines and traditional graph kernels. A standout result was observed on the ENZYMES dataset, a 6-class protein tertiary structure benchmark. While standard methods like GraphCL achieved an F1-Micro of 33.67%, CGC reached 47.50%.

Ablation study on the ENZYMES dataset showing the performance gains from combining Proximity Perturbation (PP) and Feature Masking (FM).

The ablation studies (as seen in the figure above) confirm that the combination of both proximity and feature perturbations (PP&FM) generally yields the best performance. This suggests that capturing semantics in both the graph's "shape" and its "attributes" is crucial for robust representation.

Discussion and Conclusion

CGC demonstrates that a "good" negative sample is not merely different, but semantically different while remaining structurally similar. This approach is particularly effective for multi-class datasets where the probability of random false negatives is high.

While powerful, the authors note that the computational complexity of certain matrix norms (like the nuclear norm) can be high ($O(n^3)$), suggesting the use of Frobenius or 1-norms for larger graphs. Furthermore, the quality of generated negatives is tied to the initial classifier's accuracy. Future work may explore more efficient similarity metrics and the potential for counterfactual GCL to improve model explainability.

Summary updated

BibTeX
@inproceedings{yang2022generating,
  author = {Haoran Yang and Hongxu Chen and Sixiao Zhang and Xiangguo Sun and Qian Li and Xiangyu Zhao and Guandong Xu},
  title = {{Generating Counterfactual Hard Negative Samples for Graph Contrastive Learning}},
  booktitle = {The Web Conference},
  year = {2022},
}
Figure