Skip to content

[NAACL 2022] "SemAttack: Natural Textual Attacks via Different Semantic Spaces" by Boxin Wang, Chejian Xu, Xiangyu Liu, Yu Cheng, Bo Li

Notifications You must be signed in to change notification settings


Folders and files

Last commit message
Last commit date

Latest commit



9 Commits

Repository files navigation


This is the official code base for the NAACL 2022 paper, "SemAttack: Natural Textual Attacks via Different Semantic Spaces".

This repo contains code to attack both English and Chinese models. We put the code in seperate folders.

You may use our code to attack other tasks or other datasets.


  1. Create environment and install requirements.

    pip install -r requirements.txt
  2. Construct BERT embedding space. We follow the process in paper "Visualizing and Measuring the Geometry of BERT" (GitHub) to calculate word embeddings. The embedding space is stored as an N * 768 tensor, where N is the number of total embeddings. Use a list to indicate which word corresponds to each vector. Please see word_list.pkl for more details.

  3. Data preprocessing. Please use,, and to calculate candidate perturbations generated by different semantic perturbation functions. We also provide the processed Yelp dataset as an example. You can also follow these scripts to prepare other datasets.

Model Training

Use to train your models. We also provide our pre-trained models here.


Use to attack pre-trained models. For example, the script below attacks a BERT model fine-tuned on Yelp dataset using semantic perturbation function FC

python \
  --function cluster \
  --const 10 \
  --confidence 0 \
  --lr 0.15 \
  --load path_to_pretrained_model \
  --test-model path_to_pretrained_model \
  --test-data path_to_dataset_with_embedding_space \

You may check the code for more details. You may also try different semantic perturbation functions and different attack parameters.


author = {Wang, Boxin and Xu, Chejian and Liu, Xiangyu and Cheng, Yu and Li, Bo}, 
title = {{S}em{A}ttack: Natural Textual Attacks via Different Semantic Spaces},
year = {2022},
bootitle={Proceedings of the 2022 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies}


[NAACL 2022] "SemAttack: Natural Textual Attacks via Different Semantic Spaces" by Boxin Wang, Chejian Xu, Xiangyu Liu, Yu Cheng, Bo Li






No releases published


No packages published