Abstract
Machine Learning has seen tremendous growth recently, which has led to larger adoption 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 framework GYC, to generate a set of counterfactual text samples, 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 Natural Language Processing (NLP) systems are increasingly deployed in high-stakes areas like healthcare and criminal justice, ensuring they are fair and robust is critical. One powerful way to test these systems is through counterfactuals: "What if" scenarios that probe a model's sensitivity to specific changes. For example, if a sentiment classifier labels "I am disappointed with the service" as negative, a high-quality counterfactual would be "I am happy with the service."
However, current methods for generating these samples are often flawed. Rule-based systems like Checklist rely on rigid templates and lack diversity, while adversarial attacks often produce ungrammatical gibberish. Other generative models struggle to "steer" text toward a specific goal without losing the original meaning. To address this, the GYC framework focuses on four essential properties: plausibility (human-like quality), diversity (varied outputs), goal-orientedness (successful attribute change), and effectiveness (utility in testing).
Methodology
GYC (Generate Your Counterfactuals) uses a pre-trained GPT-2 decoder as its backbone. The process happens in two main stages to balance content preservation with targeted attribute modification:
1. Reconstruction (The Proximity Phase)
To modify a sentence, the model must first learn to accurately reconstruct it. Since GPT-2 is an autoregressive model and does not naturally provide a latent representation for a specific input, GYC employs a novel Adaptive Curriculum Training method. This allows the model to learn the internal "history matrix" required to reconstruct the input text token-by-token, ensuring the starting point for the counterfactual is as close to the original as possible.
2. Perturbation (The Steering Phase)
Once the model can reconstruct the text, GYC "nudges" the internal hidden states using a combination of losses:
- Differentiable Loss: Used for attributes like sentiment or topic where a gradient can be calculated.
- REINFORCE (Non-differentiable) Loss: Used for attributes like Named Entity Recognition (NER) or Semantic Role Labeling (SRL), where the model receives a "reward" for successfully changing a specific tag.
- Diversity Loss: An entropy-based loss that prevents the model from generating the same counterfactual repeatedly.
Experimental Setup
The authors evaluated GYC on three diverse datasets:
- DBpedia: A dataset of locations and organizations (14 classes).
- AgNews: Real-world news data categorized into World, Business, Sports, and Sci-Fi.
- Yelp: Informal restaurant reviews, filtered for sentences under 15 words.
Results and Discussion
GYC significantly outperformed baselines across several metrics. On the Yelp dataset, GYC achieved a 70.29% label-flip score, compared to just 4.00% for the template-based Checklist and 35.55% for PPLM-BoW.
A key technical advantage of GYC is its ability to move beyond simple word swapping. Unlike template-based approaches, GYC can rewrite entire phrases while maintaining syntactic integrity. For example, it can transform "pizza was horrible" into "coffee was delicious," demonstrating a deeper understanding of sentence structure.
Furthermore, human evaluation confirmed that GYC produces text that is more grammatically correct, plausible, and fluent than adversarial attacks. This ensures that the generated counterfactuals are not just "model-breaking" noise but meaningful alternative realities that a human might actually write.
Conclusion and Future Work
GYC provides a robust, "plug-and-play" framework for controlled counterfactual generation. While the current implementation faces challenges regarding computational cost during the reconstruction phase and is optimized for shorter sentences, future work will focus on scaling the framework for longer, more complex discourse and improving processing speed.
Summary updated