wikillm

Paper · 2020-04-04 · cs.CL

BAE: BERT-based Adversarial Examples for Text Classification

BAE is a black-box attack on text classifiers. It ranks words by importance, masks each one, and uses BERT's masked-LM guesses as replacements or insertions, filtered by sentence similarity (USE) and part of speech. Author-reported: it lowers accuracy more than TextFooler in most settings (BERT with replacement only is the exception) and rates more natural in a small human study.

Adversarial text generationAdversarial robustnessText classificationMasked language modelingadversarial-examplestext-classificationblack-box-attackbertmasked-language-model

arXivPDFMarkdown12 min read

AI-compiled by claude-haiku-5-5 on 2026-10-19, not yet reviewed by a human. Every claim cites its source; check it when a detail matters.

In plain words

A text classifier, for example one that decides whether a review is positive, can be fooled by changing a few words in a way a person would hardly notice. Earlier attacks often swapped words for synonyms without checking the sentence around them, which produced odd phrasing that people can spot.

BAE uses a language model trained to fill in blanks. It hides one word at a time, asks the model what fits there, and keeps only candidates that keep the meaning close to the original. It can also add a new word next to a word instead of replacing it. It goes through the words that matter most to the classifier first and stops as soon as the label flips. It only needs the classifier's output probabilities, not its internals.

TL;DR

BAE is a black-box attack on text classifiers. It scores each word by how much deleting it lowers the probability of the correct label, then masks words in that order and fills each mask with BERT's masked-LM predictions. Candidates are filtered by sentence similarity (USE) and, for replacements, by part of speech. The paper reports larger accuracy drops than TextFooler on most settings, but its own numbers show BERT is the exception for replacement-only attacks. Results are author-reported on seven datasets (sentiment, subjectivity and question type).

Problem

Synonym-replacement attacks such as TextFooler pick substitutes from word embeddings, not from the sentence context, so they can change meaning or produce unnatural words §1. The paper wants replacements that fit the grammar and context of the sentence §1 §2.

The threat model is black-box and soft-label: the attacker can query the classifier for output probabilities on a given input and has no access to parameters, gradients or training data §2. The goal is an adversarial text that the classifier labels differently from the true label, and that stays grammatical and semantically similar to the original §2.

Method

BAE works at the token level with two operations §2:

  • Replace (R): replace a token with a new one.
  • Insert (I): insert a new token to the left or right of a token.

Both are done by placing a mask token and letting a pre-trained BERT base-uncased masked LM predict what goes there §2 app. A. The paper presents four attack modes §2:

  • BAE-R: replace each token.
  • BAE-I: insert a token to the left or right of each token.
  • BAE-R/I: for each token, either replace it or insert next to it.
  • BAE-R+I: replace the token, then insert next to it.

The steps are as follows.

  1. Token importance. Each token's importance is the drop in the probability of the correct label when that token is deleted, following the approach of TextFooler and Ren et al. §2. The paper does not state whether the importance is recomputed after edits; alg. 1 computes it once on the original sentence.
  2. Mask and predict. Visit tokens in descending importance. Mask the current position in the adversarial text and take the top K BERT-MLM predictions, with K = 50 §2 app. A alg. 1.
  3. Filter. Keep candidates whose sentence-level similarity to the input, measured with the Universal Sentence Encoder, passes a threshold of 0.8 app. A. For R only, also keep candidates with the same part of speech as the original token §2 app. A. The paper also filters stop words (NLTK list) for R and I, and for sentiment tasks filters antonyms using synonym embeddings app. A.
  4. Choose. If one or more candidates flip the prediction, pick the one whose full adversarial sentence is most similar to the original input. If none flip it, pick the candidate that lowers the probability of the true label most §2 alg. 1.
  5. Stop. Stop when the label flips (success), or when every token has been processed (failure) §2 alg. 1.

Algorithm 1 is the full R procedure. Its pseudocode, in our words:

Pseudocode
# BAE-R attack  [alg. 1]
# Input: tokens (list of n words), true label, classifier returning probs
# Output: adversarial token list, or None on failure

def bae_replace_attack(tokens, true_label, classifier):
    # Start the adversarial copy from the original sentence  [alg. 1]
    adv_tokens = list(tokens)
    # Rank positions by how much deleting each token lowers P(true_label)
    # [§2] [alg. 1]
    positions = rank_by_deletion_importance(tokens, true_label, classifier)
    for position in positions:
        # Mask the current position in the adversarial text  [alg. 1]
        masked = adv_tokens with mask at position
        # Take the 50 most likely fillers from BERT-MLM  [§2] [app. A]
        fillers = top_k_mlm_predictions(masked, k=50)
        # Keep fillers that keep the sentence close and the POS the same
        # [§2] [app. A]
        fillers = filter_by_use_and_pos(fillers, tokens, adv_tokens, position)
        # Build one candidate sentence per filler  [alg. 1]
        candidates = [replace(adv_tokens, position, f) for f in fillers]
        flipping = [c for c in candidates if classifier(c) != true_label]
        if flipping:
            # Return the flipping candidate closest to the original  [§2]
            return most_similar_to(flipping, tokens)
        # Otherwise keep the candidate that lowers P(true_label) most
        # and continue to the next position  [§2] [alg. 1]
        adv_tokens = most_damaging(candidates, true_label, classifier)
    # Every position tried without a flip  [§2] [alg. 1]
    return None

The pseudocode is our reading of alg. 1. In the source the Return sits in the success branch and the failure branch only updates the adversarial text, so the loop continues. The filter helper names are ours; the paper names the steps but not the helper.

Insert (BAE-I) uses the same loop, but the mask is placed next to the token on the left or right, and the paper does not state how that side is chosen §2. BAE-R+I runs the replace step and then the insert step at the same position §2. BAE-R/I chooses between replace and insert per token, and the paper does not state the rule for that choice §2.

The source has no numbered equations, so none are cited.

Hyperparameters

NameValueWhereNotes
Masked LMBERT base uncased§2 app. Apredicts the mask tokens
Top-K fillers, BAEK = 50§2 app. A
USE similarity threshold, BAE0.8 (cosine)app. Afilter on USE embeddings of the adversarial and input text
TextFooler baselineK = 50 and threshold 0.8§3see Pitfalls on the synonym source
Part-of-speech filtersame POS as original§2 app. AR only
Stop-word filterNLTK listapp. AR and I
Antonym filtersynonym-embedding basedapp. Asentiment tasks; which datasets count is not stated
Word-CNN100 filters of sizes 3, 4, 5; dropout 0.3app. A
Word-LSTM1 bi-directional layer, 150 hidden units, dropout 0.3app. A"similar to" TextFooler app. A
Word embeddings (LSTM, CNN)300-dim counter-fittedapp. A
BERT classifierBERT base uncased: 12 layers, 12 heads, hidden 768, vocab 30522app. A
BERT fine-tuningAdam, learning rate 2e-5, 2 epochsapp. A
Early stoppingon the test setapp. Asee Pitfalls
Training split, test splitsee Dataset tabletab. 5
Stopping rulelabel flipped, or all tokens processed§2 alg. 1
Perturbation budget sweepmaximum % perturbation (TREC); axis range not stated§3 fig. 2ratio of R and I operations to original length; text mentions 20% and 40 to 50%

Not stated: batch sizes, learning-rate schedules and optimizer settings for the LSTM and CNN models, maximum sequence length, the number of training epochs for those models, random seeds, and any number of repeated runs.

Dataset

tab. 5 lists the seven datasets (test sets are small: 100 for Amazon, Yelp and IMDB):

DatasetClassesTrainTestAvg length (tokens)
Amazon290010010.29
Yelp290010011.66
IMDB290010017.56
MR29595106720.04
MPQA2954310603.24
Subj29000100023.46
TREC659515007.57

Results

Author-reported. Setup: three target classifiers (word-LSTM, word-CNN, BERT base uncased) trained on each dataset's training split and attacked on its test split §3. The baseline is TextFooler §3. Each cell gives test accuracy in percent (average USE similarity between original and adversarial text in parentheses), as reported in tab. 1 for Amazon, Yelp, IMDB and MR, and in tab. 2 for MPQA, Subj and TREC.

ModelAttackAmazonYelpIMDBMRMPQASubjTREC
word-LSTMOriginal88.085.082.081.1689.4391.990.2
word-LSTMTextFooler31.0 (0.747)28.0 (0.829)20.0 (0.828)25.49 (0.906)48.49 (0.745)58.5 (0.882)42.4 (0.834)
word-LSTMBAE-R21.0 (0.827)20.0 (0.885)22.0 (0.852)24.17 (0.914)45.66 (0.748)50.2 (0.899)32.4 (0.870)
word-LSTMBAE-I17.0 (0.924)22.0 (0.928)23.0 (0.933)19.11 (0.966)40.94 (0.871)49.8 (0.958)18.0 (0.964)
word-LSTMBAE-R/I16.0 (0.902)19.0 (0.924)8.0 (0.896)15.08 (0.949)31.60 (0.820)43.1 (0.946)20.4 (0.954)
word-LSTMBAE-R+I4.0 (0.848)9.0 (0.902)5.0 (0.871)7.50 (0.935)25.57 (0.766)29.0 (0.929)11.8 (0.874)
word-CNNOriginal82.085.081.076.6689.0691.393.2
word-CNNTextFooler42.0 (0.776)36.0 (0.827)31.0 (0.854)21.18 (0.910)48.77 (0.733)58.9 (0.889)47.6 (0.812)
word-CNNBAE-R16.0 (0.821)23.0 (0.846)23.0 (0.856)20.81 (0.920)44.43 (0.735)51.0 (0.899)29.6 (0.843)
word-CNNBAE-I18.0 (0.934)26.0 (0.941)29.0 (0.924)19.49 (0.971)44.43 (0.876)49.8 (0.958)15.4 (0.953)
word-CNNBAE-R/I13.0 (0.904)17.0 (0.916)20.0 (0.892)15.56 (0.956)32.17 (0.818)41.5 (0.940)13.0 (0.936)
word-CNNBAE-R+I2.0 (0.859)9.0 (0.891)14.0 (0.861)7.87 (0.938)27.83 (0.764)31.1 (0.922)8.4 (0.858)
BERTOriginal96.095.085.085.2890.6697.097.6
BERTTextFooler30.0 (0.787)27.0 (0.833)32.0 (0.877)30.74 (0.902)36.23 (0.761)69.5 (0.858)42.8 (0.866)
BERTBAE-R36.0 (0.772)31.0 (0.856)46.0 (0.835)44.05 (0.871)43.87 (0.764)77.2 (0.828)37.2 (0.824)
BERTBAE-I20.0 (0.922)25.0 (0.936)31.0 (0.929)32.05 (0.958)33.49 (0.862)74.6 (0.918)32.2 (0.931)
BERTBAE-R/I11.0 (0.899)16.0 (0.916)22.0 (0.909)20.34 (0.941)24.53 (0.826)64.0 (0.903)23.6 (0.908)
BERTBAE-R+I14.0 (0.830)12.0 (0.871)16.0 (0.856)19.21 (0.917)24.34 (0.766)58.5 (0.875)20.2 (0.825)

Bold marks the lowest accuracy per model and dataset (the strongest attack), matching the source's boldface.

Headline claims, author-reported:

  • Drops of 40 to 80% in test accuracy for BAE attacks across datasets and models, with higher average semantic similarity than TextFooler §3.
  • Replacing and inserting can push BERT accuracy down by over 80% on some datasets with a few operations §1.
  • BAE-R+I is the strongest attack, with one exception §3.
  • BAE-I has the highest average semantic similarity of the four modes §3.
  • Effectiveness vs. perturbation budget (TREC only in the body): the BAE attacks are consistently stronger than TextFooler; classifiers are fairly robust below 20% perturbation and the effect saturates at 40 to 50% §3 fig. 2. Full per-dataset curves are in the appendix app. B fig. 3 to fig. 8 (figure numbers inferred from source order).

Ablation: Replace vs. Insert

tab. 4. Percent of test instances that need a given operation for a successful R/I attack. A is attacked by R/I but not R; B by R/I but not I; C needs both R and I §3.

DatasetA word-LSTMB word-LSTMC word-LSTMA word-CNNB word-CNNC word-CNNA BERTB BERTC BERT
MR15.110.13.112.49.62.824.312.95.7
Subj14.412.35.116.213.87.413.911.47.5
TREC16.61.60.220.05.01.414.08.62.4

Human evaluation (BERT classifier)

tab. 3 and app. C. Three datasets, 100 test samples each, mixed with successful adversarial examples from BAE-R, BAE-R+I and TextFooler; three annotators §3. Naturalness is scored 1 to 5 on a Likert scale, where 1 is "sure adversarial" and 5 is "sure original" app. C. The source table also has an "Original" column, which is shown below.

DatasetSentiment accuracy % (Original)TextFoolerBAE-RBAE-R+I
Amazon95.779.185.283.8
IMDB90.383.184.379.3
MR93.382.084.682.4
DatasetNaturalness, 1 to 5 (Original)TextFoolerBAE-RBAE-R+I
Amazon4.263.173.913.71
IMDB4.353.413.893.76
MR4.193.353.843.74

Paper vs code

No official code.

Searched: the paper and its arXiv metadata link no repository. The paper points to an implementation inside TextAttack §3 (third-party framework). Via the GitHub repository search API I tried "BAE adversarial BERT", "bert-based adversarial examples text classification", "siddhant garg adversarial", "bae bert mlm attack" and "goutham ramakrishnan". The hits were not authors' repositories: a reproducibility project by a different owner, and an Arabic-language adaptation. Neither was used.

Pitfalls

  • Test-set early stopping. The source states the models are trained first and that early stopping uses "the test dataset" app. A. Any reported accuracy on those splits may be optimistic. We have no validation split to check against.
  • Small test sets. Amazon, Yelp and IMDB have 100 test points each tab. 5, so each point moves accuracy by about one percentage point. No variance, seeds or repeated runs are reported. Treat close comparisons as noise.
  • BERT-R versus TextFooler. The text says TextFooler is "sometimes stronger" than BAE-R on BERT, and attributes it to the shared parameter space between the masked LM and the classifier §3. In the tables, TextFooler gives lower accuracy than BAE-R on six of seven datasets for BERT: all four sentiment sets plus MPQA and Subj tab. 1 tab. 2. BAE-R is lower only on TREC. The "almost always stronger" claim holds for word-LSTM and word-CNN, not for BERT-R.
  • Baseline description is inconsistent. The TextFooler paragraph says the baseline uses top K = 50 predictions from BERT-MLM and a 0.8 USE threshold §3. TextFooler normally takes synonyms from a word-embedding space. The source does not say which synonym source the reported baseline used.
  • Algorithm order and state. Importance is computed once on the original sentence, but the loop then edits the adversarial text in place. The source does not state how positions are kept consistent after an insert. Our inference is that an insert shifts later indexes alg. 1.
  • Algorithm return logic. The printed algorithm returns only in the success branch and otherwise continues with the chosen candidate alg. 1. A literal implementation keeps a greedy chain of edits, not a search over alternatives. Our pseudocode follows that reading.
  • Choice between flipping candidates. Among flipping candidates, the source chooses the one most similar to the original full input §2. Which similarity measure is used in the choice (USE, as in the filter) is implied but not stated in the algorithm.
  • Insert side and R/I rule. How the left or right side is chosen for an insert, and how BAE-R/I picks between replace and insert for a given token, are not stated §2.
  • Subword handling. How BERT's WordPiece subwords map back to words, and how a multi-token filler is inserted, are not stated.
  • Filter scope. The POS filter applies to R only §2 app. A. The antonym filter applies to "sentiment analysis tasks" app. A, but the source does not list which of the seven datasets count, and the body describes Amazon, Yelp, IMDB and MR as the four sentiment tasks §3.
  • Cost. The attack does one masked-LM pass per position and one classifier query per surviving candidate alg. 1. The source gives no timing or query counts; these follow from the procedure as written.
  • Semantic similarity is a weak check. The authors say USE can rate BAE-R+I higher than BAE-R even though human raters rated BAE-R+I lower on naturalness and sentiment §3. Use the human table as the stronger signal, and note the human study covers only three datasets and the BERT classifier.
  • Figure and table numbering. Figure and table numbers are as the source's LaTeX order gives them; the appendix refers to the human-evaluation table as "Table 3", which we follow.

Lineage

  • Builds on: TextFooler (Jin et al., 2019) §1 §3; BAE's token-importance scoring follows TextFooler and Ren et al. §2.
  • Uses: BERT base uncased as the masked LM and as one target classifier §2 §3 app. A; the Universal Sentence Encoder for similarity §2.
  • Compared against: TextFooler §3; word-LSTM and word-CNN classifiers app. A.
  • Related work named by the authors: contextualised masked-LM perturbations from Li et al. (2020), posted after the original submission §2. No card in this wiki.

Source and license

Adapted from “BAE: BERT-based Adversarial Examples for Text Classification” by Siddhant Garg, Goutham Ramakrishnan (arXiv:2004.01970), used under CC BY 4.0. Changes: condensed and restructured for implementation, with tables, equations and algorithms taken from the paper and its code, each cited to its source. Not affiliated with or endorsed by the authors.