One-Way Consistency for Adversarial Training of Text Classifiers

Adversarial training means training a model on deliberately perturbed inputs so it stops being fooled by them. For text classifiers it carries a familiar cost: the model becomes harder to fool with synonym swaps but gets a little worse on ordinary, unperturbed text. Most methods that try to reduce this cost add an "invariance" term that pulls the model's internal representation of a perturbed sentence toward its representation of the original sentence. That pull runs in both directions. The original sentence's representation is also dragged toward the perturbed one, and in text the perturbed sentence often does not mean quite the same thing. Synonym swaps drift in meaning, and sometimes they flip the true label. We propose making the pull one-directional. The perturbed representation is moved toward the clean one, and the clean representation is frozen inside that term with a stop-gradient. A small predictor head absorbs the remaining mismatch. The method is cheap and costs nothing at test time, and it targets the specific way text adversarial training damages clean accuracy.

The proposal also has an evaluation commitment. Text defenses in this area are routinely scored only against fixed attack recipes such as TextFooler, while vision defenses that looked strong under fixed recipes were broken once attackers adapted to them. We therefore score the method, and every baseline, against a defense-aware attack suite built for discrete text, including attacks aimed at the very representations our loss shapes.

Core Claim

Our claim is that most of the clean-accuracy loss in discrete text adversarial training comes from symmetric invariance losses acting on adversarial examples that are not truly label-preserving. Removing the gradient path from those examples into the clean representation should recover clean accuracy to within roughly half a point of standard fine-tuning. It should do so while matching or beating the robustness of existing text adversarial training under adaptive attack.

In vision, Rethinking Invariance Regularization in Adversarial Training to Improve Robustness-Accuracy Trade-off (AR-AT) diagnosed a "gradient conflict" in symmetric losses such as TRADES. It fixed the problem with a stop-gradient on clean features, a predictor head, and separate batch-norm statistics for clean and perturbed batches, reaching 87.82% clean and 54.72% AutoAttack accuracy on CIFAR-10 with ResNet-18. Its role here is to supply the method. We argue the mechanism should matter more in text than in images, for two reasons.

The first reason is semantic drift. Towards Improving Adversarial Training of NLP Models (A2T) found that its masked-language-model variant lost clean accuracy because the generated replacements sometimes altered the ground-truth label. A symmetric loss would teach the clean representation to agree with those corrupted examples. This motivates the proposal.

The second reason is architectural. Transformers use LayerNorm, which keeps no running statistics. AR-AT's split batch-norm component therefore has no direct counterpart, and the open question is whether the stop-gradient alone carries the benefit. That makes this a test of AR-AT's mechanism, not a port of its recipe.

TART offers a related explanation: perturbations that leave the data manifold distort the decision boundary. It addresses this by keeping only on-manifold perturbations, identified with an autoencoder's tangent space. We do not adopt TART, because tangent estimation in anisotropic token-embedding spaces is itself an unsolved problem. We do test its premise through a label-preservation weighting ablation, described below.

Research Program

The training objective for a classifier $f_\theta$ with pooled sentence representation $z_\theta(\cdot)$ is

$$\mathcal{L}(\theta,\phi) = \mathrm{CE}(f_\theta(x), y) + \alpha\,\mathrm{CE}(f_\theta(x'), y) + \lambda\, d\big(h_\phi(z_\theta(x')),\ \mathrm{sg}[z_\theta(x)]\big).$$

The symbols are defined as follows. $x$ is a training sentence with label $y$, and $x'$ is its adversarial version. $h_\phi$ is a two-layer predictor network, $\mathrm{sg}$ is the stop-gradient operator, and $d$ is negative cosine similarity. $\alpha$ and $\lambda$ weight the adversarial cross-entropy and the consistency term.

Adversarial examples are produced by A2T's generator. It ranks words by the gradient of the loss with respect to their embeddings, swaps them for counter-fitted synonyms filtered by a DistilBERT similarity check, and regenerates examples for 20% of the training data each epoch. Training cost therefore stays near A2T's.

We study three design questions in order, using BERT and RoBERTa backbones.

The first question is where the representation should be taken: the [CLS] vector alone, mean-pooled final-layer tokens, or the sum over the last four layers.

The second question is whether LayerNorm needs a split analogue. We compare shared LayerNorm parameters against separate learnable gain and bias parameters for perturbed inputs.

The third question is whether down-weighting likely label-flipping perturbations adds anything once the stop-gradient is in place. The weighting is the probability that a frozen natural-language-inference model assigns to "entailment" between $x$ and $x'$. If it adds little, the stop-gradient alone handles semantic drift, which is the core claim.

Every trained model is then attacked with an adaptive suite assembled in TextAttack, following the principles in On Adaptive Attacks to Adversarial Example Defenses.. The suite has four components.

  1. Fixed recipes: TextFooler, from Is BERT Really Robust?.
  2. White-box search: gradient-guided beam search through the full model, with larger query budgets.
  3. Representation-targeted attack: a feature-space attacker that maximizes the distance between $h_\phi(z(x'))$ and $z(x)$. This is the most direct test of whether our loss only hides vulnerability in the space it regularizes.
  4. Exact attack: integer-linear-programming search for the minimum word swaps that flip the prediction, following Robustness Evaluation of Text Classification Models Using Mathematical Optimization, run on a subsample.

All attacks share the same synonym sets and similarity thresholds. The reported robustness number for a model is its worst-case accuracy over the whole suite.

Baselines

Standard fine-tuning. Vanilla fine-tuning of the same backbone is the naive reference point. It fixes the clean-accuracy ceiling and the robustness floor. PlugAT reports vanilla BERT at 4.9% accuracy under TextFooler on SST-2.

A2T. A2T is the closest adversarial training method and shares our example generator. Beating it, with the same generator, data, epochs, and a hyperparameter-search budget of 20 configurations, isolates the effect of the consistency term. A2T reports up to a 70% reduction in attack success rate on IMDB. The file gives no per-dataset accuracy-under-attack figure for A2T, so we rerun it ourselves.

PlugAT. PlugAT is the strongest published result on our main benchmark. It freezes BERT, trains input and attention plug-in modules, and filters adversarial examples that cause forgetting of clean data. It reports 20.8% accuracy under TextFooler on SST-2 with 92.6% clean accuracy, against 16.5% for full-model PGD adversarial training. We beat it if we reach at least 92.6% clean accuracy on SST-2 together with higher worst-case accuracy under the adaptive suite, not just under TextFooler. Because PlugAT was only ever scored against fixed recipes, we re-attack it with the same suite, including gradients taken through its plug-in parameters.

PURE. PURE removes the top principal component of token embeddings during fine-tuning. It reports lowering the average performance drop rate from 81.7 to 58.4 at about 1.2 times training cost. We include it as the strong non-adversarial-training competitor and attack it with gradient averaging over its randomized decomposition.

Ablations. Three ablations isolate the key ingredient. The first uses the identical loss with a symmetric consistency term, meaning the stop-gradient is removed (TRADES-style). The second removes the predictor head. The third removes the label-preservation weighting.

All methods use the same backbone checkpoint and a matched GPU-hour budget. Each is tuned on a held-out development split, attacked with the same development-set tuning protocol, and averaged over five seeds.

Why not just…?

Why not just tighten A2T's semantic-similarity threshold so that label-flipping swaps are never generated? This is the most serious threat to the proposal. If semantic drift is the whole story, stricter filtering would remove the damaging examples without any new loss. The cost is that stricter filtering also removes many genuine attacks and weakens the training signal. We run A2T at three increasingly strict similarity thresholds and plot clean accuracy against worst-case adaptive accuracy. If stricter filtering reaches our point on that curve, we will report that the stop-gradient is unnecessary.

Why not just use the symmetric TRADES-style loss that already works in vision? AR-AT's own analysis shows that the symmetric version causes the clean-feature corruption we aim to remove. Text should make this worse, given A2T's finding that context-based replacements change labels. The symmetric ablation tests this directly. If it matches our method, the gradient-conflict story does not transfer to text, and we will say so.

Why not just purify inputs with an LLM at test time? Adversarial Text Purification restores BERT on IMDb from 2.8% to 73.5% under TextFooler. However, it adds a GPT-3.5 call to every query. It was also never tested against an attacker who knows the purifier, and Baseline Defenses for Adversarial Attacks Against Aligned Language Models shows that input-level defenses rely on the attacker producing non-fluent text and impose costs on benign inputs. We include it as an expensive reference on a 500-example IMDb subset. We report its clean-accuracy change and its accuracy under an attacker that queries the full purifier-plus-classifier pipeline.

Proposed Experiments

Data. The main experiments use SST-2, IMDb, AG News, and SNLI. Cross-domain transfer is tested from IMDb to Yelp and Rotten Tomatoes, as in A2T.

Metrics. We report clean accuracy, worst-case accuracy under the adaptive suite, accuracy under each individual attack, average number of words swapped, and training cost. For every method, sweeping $\lambda$ or its equivalent hyperparameter traces a clean-versus-robust curve, and comparisons are made along these curves rather than at single points.

Decisive checks. The claim holds only if three conditions are met together. First, our method sits strictly above both the symmetric ablation and strictly-filtered A2T on the clean-versus-adaptive-robust curve on at least three of the four datasets. Second, its clean accuracy stays within 0.5 points of vanilla fine-tuning. Third, the gap between TextFooler accuracy and worst-case adaptive accuracy is no larger for our method than for A2T. A larger gap would mean the regularized representation masks gradients rather than truly hardening the model, which is exactly the interaction between adversarial training and defenses that Tramèr et al. warn about.

Gradient-masking sanity checks. Three further checks guard against masking. Black-box attacks should never beat white-box attacks. Accuracy should fall steadily as the attack budget increases. Attacks transferred from the vanilla model should not succeed more often than direct attacks.

A clear negative result would also be informative: the symmetric ablation matching our method, or the feature-space attacker breaking it. Either outcome would show that the vision explanation of the robustness-accuracy trade-off does not carry over to discrete text.