Enhancing Hate Speech Classifiers through a Gradient-assisted Counterfactual Text Generation Strategy (2025)

Abstract: Counterfactual data augmentation (CDA) is a promising strategy for improving hate speech classification, but automating counterfactual text generation remains a challenge. Strong attribute control can distort meaning, while prioritizing semantic preservation may weaken attribute alignment. We propose **Gradient-assisted Energy-based Sampling (GENES)** for counterfactual text generation, which restricts accepted samples to text meeting a minimum BERTScore threshold and applies gradient-assisted proposal generation to improve attribute alignment. Compared to other methods that solely rely on either prompting, gradient-based steering, or energy-based sampling, GENES is more likely to jointly satisfy attribute alignment and semantic preservation under the same base model. When applied to data augmentation, GENES achieved the best macro F1-score in two of three test sets, and it improved robustness in detecting targeted abusive language. In some cases, GENES exceeded the performance of prompt-based methods using a GPT-4o-mini, despite relying on a smaller model (Flan-T5-Large). Based on our cross-dataset evaluation, the average performance of models aided by GENES is the best among those methods that rely on a smaller model (Flan-T5-L). These results position GENES as a possible lightweight and open-source alternative.

## introduction the rise of hate speech has driven the development of datasets and machine learning models aimed at mitigating harm. however, despite advances in large language models (llms), these models often suffer from poor generalizability or unintended bias @xcite , largely due to data-level issues like imbalanced labels, skewed topics, and token biases @xcite @xcite . data augmentation is a potential solution. however, approaches with a purely generative goal do not directly mitigate bias and may lead to inconsistent performance gains @xcite @xcite . ideally, augmentation is done to break correlations between target variables and irrelevant features. in this regard, counterfactual data augmentation (cda) has emerged as a promising strategy @xcite . cda involves generating synthetic data by modifying observed texts to satisfy target attributes while preserving their original meaning. studies have showed that training on both original and counterfactual data help reduce the model's reliance on spurious correlations, improving out-of-domain generalization @xcite @xcite . despite its potential, implementing cda in practice remains challenging. while human-edited counterfactual texts continue to be the standard @xcite , manual generation is timeconsuming and resource-intensive. one potential solution is to fine-tune an llm for counterfactual text generation. however, fine-tuning requires large datasets and significant computational resources. alternatively, prompting llms could be a lightweight solution. however, in the hate speech domain, llms often fail to produce edits that reliably flip the target attribute @xcite . this is partly due to built-in safeguards against offensive content @xcite and the inherent difficulty of generating text with subjective concepts like abusiveness and offensiveness @xcite . thus, there is a need for more reliable, resourceefficient methods for counterfactual generation. in the hate speech domain, related works have relied primarily on manual, prompt-based, or fine-tuning approaches @xcite @xcite , leaving unexplored the potential of plug-and-play controlled text generation methods. to address these limitations, we investigated the efficacy of plug-and-play controlled text generation methods @xcite as a means of counterfactual data augmentation. plug-and-play methods enable control over specific attributes in generated text without requiring extensive fine-tuning. by integrating smaller classifiers or score functions, these approaches facilitate controlled generation with minimal resource overhead. counterfactual generation must balance two key goals: target attribute alignment and semantic similarity. while plug-and-play methods can support multi-attribute control, most existing techniques were originally developed for general conditional text generation rather than counterfactual text generation. as a result, they do not directly address the dual requirements of reliably flipping a target attribute while preserving meaning. gradient-based approaches such as pplm @xcite excel at attribute control but provide limited mechanisms for semantic preservation, whereas energybased methods like mix & match @xcite allows similarity constraints but require careful tuning. these limitations motivated us to modify and adapt plug-and-play methods for counterfactual data augmentation, exploring what changes are necessary to better satisfy the unique goals of counterfactual text generation. our contributions are as follows: ## gradient-assisted energy-based sampling for counterfactual text generation in this section, we introduce genes (gradientassisted energy-based sampling), a plug-andplay framework for counterfactual text generation. as illustrated in figure 1 , genes combines energy-based sampling with a hard rejection criterion and incorporates gradient-based steering to guide the proposal distribution. the remainder of this section is organized as follows. we first outline how energy-based methods are commonly adapted for counterfactual text generation. we then describe the modifications introduced in genes to enhance the efficiency and effectiveness of the sampling strategy. ## related works cda has been explored in hate speech detection, but most studies have used prompting, fine-tuning, and/or manual strategies to generate counterfactual text @xcite @xcite @xcite . however, using plugand-play controlled text generation to enable cda remains underexplored. there are two common categories of plug-andplay controlled text generation: weighted decoding, and energy-based methods. weighted decoding adjust token probabilities at inference to enforce attributes @xcite @xcite @xcite . pplm @xcite and fudge @xcite manipulate hidden states or logits, but are not designed for counterfactual generation. gradient-based steering methods such as gyc @xcite and casper @xcite target counterfactual text but lack the flexibility of energy-based approaches. energy-based models (ebms) treat controlled generation as sampling @xcite . while m&m avoids gradient dependence, it is slowed by token-level sampling. block m&m improves efficiency with utterance-level sampling @xcite , while cold decoding @xcite employs langevin dynamics but requires gradient access to energy functions. our method combines ebm flexibility with gradient-based perturbation through a separate loss function. other directions include prefix tuning; for instance, magic @xcite controls correlated attributes but requires extra training and data, suggesting avenues for future exploration. ## conclusion this work introduced gradient-assisted energybased sampling (genes), a modified sampling procedure designed to adapt energy-based methods for counterfactual text generation. our experiments demonstrated that combining truncated energy-based sampling with gradient-assisted proposal generation improves the likelihood of producing counterfactual texts that jointly satisfy attribute alignment and semantic preservation. when applied for data augmentation, genes improved classifier robustness, particularly in detecting targeted abusive language. despite relying on a smaller model (flan-t5-large), it performed on par with, or in some cases better than, promptbased methods using more advanced models (e.g., gpt-4o-mini), underscoring its effectiveness as a lightweight alternative. the outcome was heterogeneous. performance on latenthate was weaker, suggesting that current methods may struggle with generating implicit hate speech, which appears more important for improving robustness @xcite . manual inspection further showed that plug-and-play methods often inject explicit abusive terms or flip the tone to an aggressive style, but do not always capture protected-group targeting. a follow-up run that implements genes guided by a latenthatetrained classifier partially mitigated this gap, showing that simulating dataset-specific characteristics can further enhance effectiveness. overall, these findings highlight the promise of lightweight plugand-play counterfactual generation for improving hate speech detection. future work should explore more precise control over both the main attribute (hate/abusiveness) and its specific target (e.g., social groups) to better capture the nuances of hate speech.