Improving Gradient-based Adversarial Training for Text Classification by Contrastive Learning and Auto-Encoder

Yao Qiu, Jinchao Zhang, Jie Zhou

Abstract

Deep learning models for text classification are highly susceptible to adversarial attacks—small, intentional perturbations that can flip model predictions. While standard adversarial training like FreeLB focuses on classification loss, this paper introduces two novel training objectives: Contrastive Adversarial Representation Learning (CARL) and Reconstruction from Adversarial Representations (RAR). These methods force models to maintain stable semantic representations and develop a deeper understanding of lexical semantics, significantly improving both accuracy and robustness across standard benchmarks.

Introduction

Adversarial attacks in natural language processing often involve adding small amounts of noise to word embeddings during training. Current gradient-based adversarial training methods typically rely on a standard classification loss to guide the model. However, this approach often fails to teach the model why a noisy input should be treated the same as the original, leaving internal representations fragile. This paper addresses the need for more robust internal encoders that can withstand such perturbations without losing semantic integrity.

Methodology

The authors propose two distinct architectures to enhance the robustness of the BERT/ALBERT framework during adversarial training.

Figure 1: Architectures for (a) CARL, which uses a contrastive learning module, and (b) RAR, which incorporates a reconstructor for word-level denoising.

CARL: Contrastive Adversarial Representation Learning

CARL operates at the sentence level. It utilizes a contrastive loss to pull the representation of an adversarial sample closer to its original version in the embedding space while pushing it away from "negative" samples (those belonging to different classes). This ensures that the resulting sentence vector remains stable even when the input is contaminated.

RAR: Reconstruction from Adversarial Representations

RAR is a word-level approach inspired by auto-encoders. It forces the model to reconstruct original words from their perturbed adversarial embeddings. By requiring the model to "denoise" input at the lexical level, RAR fosters a deeper understanding of word-level semantics that is resistant to gradient-based noise.

Experimental Results

The researchers evaluated CARL and RAR on four major benchmarks: SST-2, Yelp-P, AG’s News, and Yahoo! Answers. Both methods were implemented on top of ALBERT using the FreeLB framework.

  • Accuracy: CARL showed consistent improvements, such as a +1.20% accuracy gain over FreeLB on the AG’s News dataset.
  • Robustness: Under strong k-PGD iterative attacks, CARL maintained 80.1% accuracy on AG's News, significantly higher than the 70.2% achieved by standard ALBERT.
  • Stability: RAR proved most effective at maintaining representation stability, measured by the cosine similarity between original and adversarial vectors.

Discussion and Insights

A key finding is that classification loss alone is insufficient for supervising the intermediate representations of a model. By adding CARL and RAR, the authors provide explicit supervision for the internal encoder.

Figure 4: Visualization of the representation space, showing how the model learns to cluster original (Ro) and adversarial (Ra) representations while distancing them from negatives (Rd).

While CARL aligns more closely with the final classification task and yields higher accuracy gains, RAR provides superior "representation robustness." Furthermore, the generative nature of RAR allows it to be used as a tool for generating text-form adversarial samples by decoding perturbed embeddings back into natural language.

Limitations and Future Work

Despite their success, these methods introduce some overhead. CARL requires a "warm-up" period as initial representations are too noisy for effective contrastive learning. Additionally, both methods increase the memory footprint during training due to the need for reconstructor modules (RAR) or memory banks (CARL). Future research may extend these techniques to more complex NLP tasks such as Question Answering or Natural Language Inference.

Summary updated

BibTeX
@inproceedings{qiu2021improving,
  author = {Yao Qiu and Jinchao Zhang and Jie Zhou},
  title = {{Improving Gradient-based Adversarial Training for Text Classification by Contrastive Learning and Auto-Encoder}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2021},
  doi = {10.18653/v1/2021.findings-acl.148},
}
Figure