Abstract
Machine Learning has seen tremendous growth recently, which has led to a larger adaptation of ML systems for educational assessments, credit risk, healthcare, employment, criminal justice, to name a few. The trustworthiness of ML and NLP systems is a crucial aspect and requires a guarantee that the decisions they make are fair and robust. Aligned with this, we propose a novel framework GYC, to generate a set of exhaustive counterfactual text, which are crucial for testing these ML systems. Our main contributions include: a) We introduce GYC, a framework to generate counterfactual samples such that the generation is plausible, diverse, goal-oriented, and effective; b) We generate counterfactual samples that can direct the generation towards a corresponding condition such as named-entity tag, semantic role label, or sentiment. Our experimental results on various domains show that GYC generates counterfactual text samples exhibiting the above four properties. GYC generates counterfactuals that can act as test cases to evaluate a model and any text debiasing algorithm.
Introduction
As Machine Learning (ML) and Natural Language Processing (NLP) systems are increasingly deployed in high-stakes areas like healthcare and criminal justice, ensuring their fairness and robustness is critical. One way to test these systems is through counterfactuals: taking an input (e.g., "The waiter was helpful") and changing a specific attribute (e.g., "The waiter was rude") to see if the model's prediction changes appropriately.
Existing methods for generating these samples fall into two camps, both with significant flaws. Rule-based or template methods, such as CheckList, are rigid and lack diversity, often relying on simple word swaps that fail to capture the complexity of natural language. Conversely, adversarial attacks are designed to "flip" a model's label at any cost, frequently resulting in ungrammatical or nonsensical sentences that do not represent real-world human input. To address these gaps, the researchers identify four essential properties for useful counterfactuals: plausibility, diversity, goal-orientedness, and effectiveness.
The GYC Framework
GYC (Generate Your Counterfactuals) leverages the power of pre-trained language models like GPT-2 but introduces a "steering wheel" to the generative process. Instead of fine-tuning the model on new data—which is computationally expensive—GYC perturbs the history matrix (the latent hidden states) of the transformer during inference.
The framework uses a multi-part objective function to balance competing goals. A Proximity Loss ensures the generated text stays close to the original input's meaning, maintaining content preservation. Condition Enforcement is handled via two paths: a differentiable score for attributes like sentiment (where gradients can directly guide generation) and the REINFORCE algorithm for non-differentiable conditions like Named Entity Recognition (NER) tags or Semantic Role Labels (SRL). Finally, a Diversity Loss uses an entropy-based penalty to ensure the model generates a variety of counterfactuals rather than repeating the same sample.
The authors employ a curriculum-based approach: the model first learns to reconstruct the original sentence perfectly, and then the "condition" and "diversity" weights are gradually increased to steer the sentence toward the counterfactual goal.
Experimental Results
The authors evaluated GYC across three distinct domains: Yelp (sentiment analysis), DBpedia (location and organization classification), and AgNews (news category classification). The results highlight the framework's ability to produce high-quality test cases.
In terms of effectiveness, GYC achieved a Label-Flip Score (LFS) of 70.29 on the Yelp dataset, vastly outperforming the template-based CheckList, which scored only 4.00. Human evaluation further confirmed these findings; annotators rated GYC significantly higher than adversarial attack baselines in grammatical correctness, plausibility, and fluency. Furthermore, GYC consistently produced more diverse sets of counterfactuals than Masked-LM or PPLM-BoW baselines, providing better coverage for model testing.
Conclusion and Future Work
GYC demonstrates that powerful language models can be steered to perform specific tasks without retraining. By prioritizing plausibility and handling complex linguistic constraints through reinforcement learning, GYC provides a robust tool for debugging and debiasing NLP models. However, the framework currently faces limitations regarding computational cost—specifically the reconstruction phase—and is primarily optimized for shorter sentences. Future directions include optimizing the reconstruction process to improve speed and extending the generation capabilities to handle much longer, more complex paragraphs.
Summary updated