Source-linked AI summary

Generate Your Counterfactuals: Towards Controlled Counterfactual Generation for Text

Nishtha Madaan, Inkit Padhi, Naveen Panwar, Diptikalyan Saha

arXiv:2012.04698v2cs.CLcs.AIcs.LG

TL;DR

Fairness and robustness testing needs useful counterfactual text, but existing approaches can produce implausible examples or lack meaningful diversity. GYC uses a pre-trained GPT-2 framework to generate condition-directed counterfactuals; experiments report high label-flip performance alongside plausibility, diversity, and preserved content and syntax.

  • Problem

    Existing counterfactual methods can be domain-limited, implausible or ungrammatical, and template-based approaches may lack meaningful diversity, limiting their usefulness as test cases.

    Source: Introduction · pp. 1–2
  • Method

    GYC generates counterfactual text from an input using reconstruction and condition-enforcing losses, supporting differentiable and non-differentiable score functions.

    Source: Reconstruction via Proximity Loss · p. 4, Enforcing condition · p. 4
  • Results

    Across three datasets, GYC produces counterfactuals with close to the highest label-flip percentage, high diversity, and preserved content and syntactic structure compared with baselines.

    Source: Results and Discussion · p. 7, Conclusion · p. 7
  • Takeaways & Limitations

    GYC-generated counterfactuals can serve as test cases for evaluating models and as data for text-debiasing algorithms.

    Source: Conclusion · p. 7

    Generating counterfactuals for longer sentences is a future step because reconstruction is currently expensive to run.

    Source: Conclusion · p. 7

Abstract

from arXiv · show

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

Existing counterfactual-generation approaches can produce implausible or ungrammatical text, narrow test cases, or examples that lack meaningful diversity. GYC aims to generate plausible, diverse, goal-oriented counterfactuals and direct them toward user-specified conditions.

Introduction

  • Prior testing and adversarial methods can produce implausible or ungrammatical examples, require human intervention, or generate label flips unlikely to occur in user input.Such examples may therefore be ineffective test cases.Source: Introduction · pp. 1–2
  • Template- and rule-based approaches such as Checklist are described as unable to generate meaningful, diverse counterfactual samples.GYC instead targets generalized generation with high quality and diversity.Source: Introduction · p. 1
  • GYC is introduced to generate counterfactuals that are plausible, diverse, goal-oriented, and effective, unlike prior ungrammatical or non-plausible adversarial text.The paper compares examples from GYC and existing models on Yelp.Source: Introduction · p. 2
  • GYC can direct generation toward conditions including named-entity recognition, semantic role labeling, or sentiment, and can plug in a custom condition model.The paper reports automated evaluation of label-flip and diversity, and human judgments of plausibility, grammar, and validity.Source: Introduction · p. 2
  • GYC uses a pre-trained GPT-2 decoder without requiring model retraining or fine-tuning.Source: Introduction · p. 2

Background

The background describes autoregressive text generation through transformer history states, controlled generation conditioned on labels or structured data, and counterfactual text as an input changed under a condition model.

Language Modeling for Text Generation

Controlled Text Generation

  • Controlled generation conditions text on labels such as sentiment, topic, or tense, or on structured data such as an infobox or knowledge graph.Source: Controlled Text Generation · p. 2
  • PPLM controls pre-trained GPT-2 by perturbing its history states and optimizing for a desired class while using a KL term to retain plausibility relative to unperturbed GPT-2.Source: Controlled Text Generation · pp. 2–3

Counterfactual Text

  • A counterfactual example is synthetically generated text treated differently by a condition model, such as changing a London location to California to flip a classifier label.Such examples can serve as robustness and fairness test cases and as data augmentation.Source: Counterfactual Text · p. 3

GYC Method

GYC learns perturbations conditioned on an input text and a specified condition, first reconstructing the input and then generating counterfactual samples. Its losses support reconstruction, condition enforcement through differentiable or reward-based scores, and diversity.

GYC Method

  • GYC aims to generate K counterfactual samples from an input text and a controlling condition that specifies their scope.The method first learns a perturbed hidden state from the input, then perturbs it further to enforce the condition.Source: GYC Method · p. 3

Generative Process

  • The generative process samples counterfactuals from p(y|x, conditions) using hidden states learned from both the input text and condition.Source: Generative Process · p. 3

Learning Counterfactuals

  • Proximity loss learns perturbations to maximize the input text's log probability, preserving content; the authors use left-to-right, one-token-at-a-time reconstruction.Source: Reconstruction via Proximity Loss · p. 4
  • Condition enforcement maximizes a score from a differentiable logit-based loss or a REINFORCE reward when the score is not differentiable with respect to generated-text logits.The paper gives tagging and classification models as examples of text-input score functions.Source: Enforcing condition · p. 4
  • Entropy over the logits of K generated samples is added to discourage distribution collapse to the input and promote diversity.Training combines reward, entropy, and proximity losses, with reward and entropy weights activated after reconstruction.Source: Enforcing condition · p. 4

Applications of Counterfactuals

The paper presents GYC as a way to generate test cases for NLP classification models and data for text debiasing. It reports high quantitative and qualitative results across sentiment datasets, while linking debiasing applications to the need for condition-specific counterfactual training data.

Counterfactuals as Test-Cases

  • GYC can be adapted to classification models with differentiable or non-differentiable score functions, and reports the highest quantitative and qualitative results across sentiment datasets and metrics.The sentiment experiments and metrics are described in Tables 4 and 5 and Figure 2.Source: Counterfactuals as Test-Cases · p. 5

Counterfactuals for Debiasing

  • Counterfactual training samples for debiasing may need to contain a protected attribute, but obtaining enough such real-world data is described as prohibitive.The paper proposes using GYC-generated samples for data augmentation in debiasing tasks.Source: Counterfactuals for Debiasing · p. 5

Related Work

Related work spans text test-case generation, controlled text generation, and adversarial attacks, with prior approaches addressing token substitutions, generation constraints, or classifier changes.

Test-Case Generation

  • Text test-case generation includes token-based substitution [Ribeiro, Singh, and Guestrin 2018], the closest work to GYC, alongside structured-data testing and counterfactual explanations.The cited explanation work addresses feasibility and diversity [Mothilal, Sharma, and Tan 2020].Source: Test-Case Generation · p. 5

Controlled Text Generation

  • Controlled generation methods address text’s discrete, non-differentiable nature with reinforcement, soft argmax, or Gumbel-Softmax, while other approaches steer pretrained models with latent constraints or attribute discriminators.The passage names Yu et al. 2017, Zhang et al. 2017, Jang, Gu, and Poole 2016, Engel, Hoffman, and Roberts 2017, and Dathathri et al. 2020.Source: Controlled Text Generation · p. 5

Adversarial Attack

Experiments

Across DBpedia, AgNews, and Yelp, GYC balances label flipping, diversity, content preservation, and syntactic structure against baselines. The experiments also evaluate generation with human ratings.

Baselines

  • Baselines include template-, lexicon-, and language-model-based Checklist, masked-token BERT, bag-of-words PPLM, and meaning-preserving adversarial attacks.Adversarial Attack uses the Michel et al. (2019) implementation by Allen AI (Gardner et al. 2017).Source: Baselines · pp. 5–6

Datasets

  • Experiments use 14-class DBpedia, four-topic AgNews, and Yelp reviews filtered to sentences of at most 15 words.The datasets represent locations and organizations, news, and informal reviews, respectively.Source: Datasets · p. 6

Metrics

  • Automated metrics assess label flipping, Self-BERT diversity, semantic content similarity, and syntactic distance using tree-edit distance.A higher Diversity Score indicates lower diversity.Source: Generation Quality · pp. 6–7

Results and Discussion

  • In blind ratings of 600 samples by 30 annotators, GYC scores best against Adversarial Attack on all five human-evaluation criteria.Criteria were grammatical correctness, plausibility, fluency, sentiment change, and content preservation, each rated from 1 to 5.Source: Figure 2 · p. 6, Human Evaluation · p. 7
  • Across three datasets, GYC is close to the highest label-flip percentage while maintaining diversity, content preservation, and syntactic structure; aggregated results outperform all models.Tables 4 and 5 cover label flip, diversity, content preservation, and tree-edit distance.Source: Table 4 · p. 5, Table 5 · p. 6, Results and Discussion · p. 7
  • GYC has the highest Yelp label-flip score and significantly high scores on DBpedia and AgNews; high PPLM-BoW scores on those datasets reflect intermixed classes.Checklist and Masked-LM perform worse than GYC; the paper attributes this to their lack of an explicit label-flip signal.Source: Results and Discussion · p. 7
  • GYC has the best diversity on DBpedia and Yelp, but trails PPLM-BoW slightly on AgNews, where PPLM-BoW outputs are often unrelated.The paper describes PPLM-BoW outputs as overly diverse relative to the input.Source: Results and Discussion · p. 7
  • GYC preserves content better than PPLM-BoW, while its syntactic preservation also exceeds PPLM-BoW and approaches Checklist on AgNews.The reported comparisons focus on PPLM-BoW for these measures because Checklist and Masked-LM use single-token substitutions.Source: Table 5 · p. 6, Results and Discussion · p. 7

Conclusion

GYC leverages GPT-2 to generate counterfactuals that outperform existing models across three datasets while preserving content and syntactic structure. Extending generation to longer sentences remains a costly reconstruction challenge.

Conclusion

  • Across three datasets, GYC outperforms existing models while producing counterfactuals with high label-flip scores, plausibility, diversity, and preserved content and syntactic structure.The framework leverages GPT-2.Source: Conclusion · p. 7
  • Extending counterfactual generation to longer sentences is a next step because reconstruction is currently expensive.Source: Conclusion · p. 7

Appendix A: Model Details

  • Appendix A describes implementation details and hyperparameters for the multiple losses used to generate counterfactuals with four properties.Source: Appendix A · p. 10

Reconstruction Loss Implementation

Reconstruction loss helps recover the input sentence before hidden-state perturbation. The implementation uses curriculum training, ultimately adapting phase progression to the words already reconstructed.

Reconstruction Loss Implementation

  • Reconstruction loss regenerates the input sentence so its hidden state can be perturbed, although inference with GPT-2 makes this challenging.Source: Reconstruction Loss Implementation · p. 10
  • Naive training maximizes every input token's presence at once, but concentrates gradients at the last word and gets stuck in local minima.The remaining sentence cannot be reconstructed because its gradients are weak, so this method was not adopted.Source: Reconstruction Loss Implementation · p. 10
  • Curriculum training reconstructs the input from left to right, but its hard-coded phase lengths make it slow and empirically poor.The phase parameter K is chosen according to the number of iterations.Source: Reconstruction Loss Implementation · p. 10
  • Adaptive curriculum training advances reconstruction in left-to-right order and moves to the next phase when the previous word is regenerated.If a previous word is not reconstructed, training rolls out of the curriculum; the authors use this approach for reliable reconstruction.Source: Reconstruction Loss Implementation · p. 10
  • Proximity-loss weight controls reconstruction strength, starting at 1 and dropping to 0.02 after the sentence is reconstructed to permit perturbation.Source: Reconstruction Loss Implementation · p. 10

Differentiable Loss Implementation

The implementation describes a sentiment-based differentiable loss, reward losses for several condition types, and entropy to encourage diversity. Figure 3 compares approaches by their average score across four metrics.

Differentiable Loss Implementation

  • The differentiable loss uses a sentiment model trained on the experimental datasets, taking logits from the reconstructed text as input and requiring vocabulary consistency with GPT-2.The sentiment model uses an EmbeddingBag followed by a linear layer and averages bag-of-embeddings over the vocabulary.Source: Differentiable Loss Implementation · p. 10
  • Figure 3 compares GYC with existing approaches using the average score across all four metrics.Source: Figure 3 · p. 10

Reward Loss Implementation

Entropy

  • Entropy is used to generate samples with enough diversity, controlled by an entropy weight set to 0.08 in the experiments.Source: Entropy · p. 10

Appendix B: Baselines Implementation

The Checklist baseline generates perturbations using rule-based, lexicon-based, and language-model approaches, including templates and masked-token prediction.

Appendix B: Baselines Implementation

  • Checklist combines rule-, lexicon-, and language-model-based perturbations, using templates and RoBERTa to predict replacements for randomly masked tokens.Source: Appendix B · p. 12

Appendix C: Results

Across all datasets, GYC has the highest average score among the baselines when results are aggregated over four metrics. Table 6 presents generated counterfactual samples from GYC and the baselines for comparison.

Quantitative Results

  • GYC has the highest average score compared to the baselines on all datasets when results are averaged over four metrics.Figure 3 reports this aggregate comparison.Source: Quantitative Results · p. 12

Qualitative Results

Loading 2012.04698v2…