Attacks in NLP

一、 Introduction

NLP对抗攻击是人工智能对抗攻击的一个重要的组成部分,但是最近几年才逐渐开始兴起,究其原因在于NLP对抗攻击与传统computer vision或者audio对抗攻击有很大的不同,主要在于值空间的连续性(CV、audio)和离散性(NLP)。

如图为传统的一种对CV和audio模型的攻击方式:
Attacks in NLP_第1张图片
如图,对CV与audio的攻击是在一张图片或一段录音中加入微小连续的扰动(如高斯噪声),在人眼或人耳不可识别的条件下使模型进行错误的分类。

以对CV模型攻击为例:

Attacks in NLP_第2张图片

CV的 256 × 256 256 \times 256 256×256大小的图片像素值空间为 [ 0 , 255 ] 256 × 256 [0,255]^{256 \times 256} [0,255]256×256内的连续实数空间,对其添加扰动比较容易。

但是,如图

Attacks in NLP_第3张图片

NLP领域中,数值是由一个一个的离散的token组成。因此对NLP模型进行处理时,需要先将离散的token转换为连续的vector,这样才能让NLP模型对其进行处理。因此,对NLP模型做攻击时,也只能处理离散的token。(连续的vector一般来说是在NLP模型内部生成的,因为无法做到对其加噪声)。

二、Evasion Attacks and Defenses

1. Introduction

在CV中,Evasion Attacks就是在图片中添加人眼不可见的噪声,使图片分类模型对其进行错误的分类。

如图:

Attacks in NLP_第4张图片

对于原始图片,模型有57.7%的概率认为其是熊猫,但是在对其添加了人眼无法察觉到的噪声(连续值空间)后,模型有99.3%的概率认为其是长臂猿(分类错误)。

同样的,在NLP中,Evasion Attack指的是:对原始的句子进行修改,在对人类来说不改变语义的情况下使模型对修改过的句子进行错误的预测。

以情感分析为例,如图:

Attacks in NLP_第5张图片

上图是一段影评,对于原始的句子,NLP模型认为其是负面的,但是在对film添加上一个s后,模型认为其是正面,这对人来说,是很难察觉的。

对NLP的Evasion Attack还有其他方面,比如修改句子,使翻译模型对其进行错误的翻译。这里不在进行赘述。

2. Four Ingredients in Evasion Attacks

以影评的情感分析为例,Evasion Attacks攻击的完整步骤(执行框架)为:

Attacks in NLP_第6张图片

1 Goal,对既定的攻击模型和对抗样本指定攻击目标。

2 Transformation,对对抗样本进行相应的转换(添加扰动),在此过程中会产生很多可能的候选样本。

3 Constraints,根据设置的限制条件,对候选样本进行过滤。(比如,语法错误、人称错误或同义词变成反义词等等)

4 Search: 采取一些研究方法,在候选的样本中选择可以成功的使模型进行错误预测的样本作为最终的对抗样本。

Morris, J., Lifland, E., Yoo, J. Y., Grigsby, J., Jin, D., & Qi, Y. (2020). TextAttack: A framework for adversarial attacks, data augmentation, and adversarial training in NLP. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing: System Demonstrations.2020.

2.1. Goal: What the attack aims to achieve

以新闻类别分类为例:

2.1.1. Untargeted classification: 使模型对当前文本做错误的分类而不关心错误分类的类别。

如图:

Attacks in NLP_第7张图片

对原有新闻文本进行修改,使NLP模型对其进行错误的分类,但是不关心误分类的类别。(只要错误分类就行,其他的不关心)

2.1.2. Targeted classification: 使模型对当前文本做误分类,且误分类的类别也应该被指定。

如图:

Attacks in NLP_第8张图片

在对原有新闻文本进行修改后,使模型误分类到指定的Sci/Tech板块。

2.1.3. Universal suffix dropper: 在对翻译文本加入一些前缀后,模型回忽略前缀后的文本。

如图:

Attacks in NLP_第9张图片

如图,在对翻译文本添加红色前缀后,其后面的紫色文本将不在会被翻译。

Wallace, E., Stern, M., & Song, D. (2020). Imitation attacks and defenses for black-box machine translation systems. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP).2020

2.1.4. Wrong parse tree in dependency parsing:使模型对当前文本做错误的解析

Attacks in NLP_第10张图片

Zheng, X., Zeng, J., Zhou, Y., Hsieh, C.-J., Cheng, M., & Huang, X. (2020). Evaluating and enhancing the robustness of neural network-based dependency parsing models with adversarial examples. Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics.2020

2.2. Transformations: How to construct perturbations for possible adversaries

如图:

Attacks in NLP_第11张图片

如图,采取某些方法对样本进行转换,产生大量候选样本。之后再运用constrain对候选样本进行过滤。

2.2.1. word substitution by WordNet synonyms

同义词替换,在进行文本转换时必须要保持文本的语义不变,因此最简单的方法是进行同义词替换。WorkNet synonyms是一个同义词数据集。

如图:

Attacks in NLP_第12张图片

如图,对原始文本根据WorkNet synonyms进行同义词替换。但是,在替换时可能会出现替换后的句子语音改变或者“别扭”,这时就需要constraint进行过滤。

2.2.2. Word substitution by knn or ε \varepsilon ε-ball in counter-fitted Glove embedding space

将文本的单词转换为对应的word embedding,在embedding vector中寻找相近的单词。

如图:

Attacks in NLP_第13张图片

对原始文本进行转换,不是进行同义词替换,而是在Counter-fitted embedding space中设置一个半径为 ε \varepsilon ε的“球”(可以认为“球”内的embedding对应的单词与原始单词最接近, ε \varepsilon ε是单词接近的程度)。这样就可以防止一些不合语义的候选样例产生。

Counter-fitted embedding space: Use linguistic constrains to pull synonyms closer and antonyms far away from each others

如图,

Attacks in NLP_第14张图片

Counter-fitted使用语言学的一些限制,让同义词变得更近,反义词变的更远。

对于原始的Glove embedding space词性相近,出现频率相同的单词是靠的比较近的。比如:东、西、南、北,但是如果将”东“变成”西“,那么句子的整个意思就会发生改变,因此需要在Counter-fitted Glove embedding space中画一个半径为 ε \varepsilon ε的球,这样句子的意思才不容易改变。

Mrkšić, N., Ó Séaghdha, D., Thomson, B., Gašić, M., Rojas-Barahona, L. M., Su, P.-H., Vandyke, D., Wen, T.-H., & Young, S. (2016). Counter-fitting word vectors to linguistic constraints. Proceedings of the 2016 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies.2016

2.2.3. Word substitution by BERT masked language modeling(MLM) prediction

如图:

Attacks in NLP_第15张图片

首先,对文本的相关单词进行遮蔽,之后放入BERT中输出预测的单词将其插回源文本作为候选文本。但是,可以看到,BERT预测的masked token与源文本的token差别比较大,且预测概率最高的单词"double"与源文本"recommend"甚至相反,因此,单独使用BERT对masked token做预测是不可取的行为。

2.2.4. Word substitution by BERT reconstruction(no masking)

如图:

Attacks in NLP_第16张图片

如图,不对源文本进行遮蔽,直接将其放入BERT中,这样输出的字符信息就与源文本中对应的字符十分接近。但是,可以看到不进行遮蔽时,预测的字符与源文本中的字符十分接近,因此大大限制了BERT的能力。

2.2.5. Word substitution by changing the inflectional form of verbs, nouns and adjectives

Inflectional morpheme: an affix that never changes the basic meaning of a word, and are indicative/characteristic of the part of speech(POS).

屈折语素:永远不会改变单词基本含义的词缀,并且指示/表征词性(POS)

如图:

Attacks in NLP_第17张图片

如图,在不改变单词含义的情况下,改变了单词的时态。但是可以看到,改变时,第一和第三个句子语法是错误的,因此还需要在constraint中进行过滤。

2.2.6. Word substitution by gradient of the word embedding

该方法涉及梯度计算,因此这是一个white-box攻击。

如图:

Attacks in NLP_第18张图片

首先,将源文本放入模型中,会得到对应的Loss,之后对文本中指定的单词 e 0 e_0 e0(如recommend)求偏导,这便是 e 0 e_0 e0在当前文本中的贡献。

之后:

Attacks in NLP_第19张图片

计算,计算 e 0 e_0 e0与嵌入空间其他的embedding的差与Loss和 e 0 e_0 e0偏导的乘积,这便是当 e 0 e_0 e0变为其他字符时,Loss改变的一阶近似。

在运行中,选择使当前Loss改变最大的单词作为转换的单词。(Loss越大代表模型预测越“不准”)

如图:

Attacks in NLP_第20张图片

该图为,二维状态下的数学解释,比较简单就不赘述了。

2.2.7. Word insertion based on BERT MLM

如图:

Attacks in NLP_第21张图片

如图,在想插入单词的位置先插入一个masked token,之后将插入后的文本放入BERT中,获得BERT预测的插入后的文本作为对抗的候选文本。

2.2.8. Word deletion

如图:

在这里插入图片描述

如图,直接删减单词,不建议单独使用。

2.2.9. Character level transform

  • Swap
  • Substitution
  • Deletion
  • Insertion

如图:

Attacks in NLP_第22张图片

字符级别的转换在日常中十分常见,比如一个人在打字时多打一个字母或少打一个字母。特别的,在Substitution方法中,会专门寻找与当前字母在键盘上相近的字母进行替换,这样可以提高真实性。

因为在模型训练时,模型可能没有接触过类似的字符出错的“错别字”,因此该方法生成的对抗样本的性能比较高。

Gao, J., Lanchantin, J., Soffa, M. L., & Qi, Y. (2018). Black-box generation of adversarial text sequences to evade deep learning classifiers. 2018 IEEE Security and Privacy Workshops (SPW).2018

2.3. Constrains: What a valid adversarial examples should satisfy

2.3.1. What a valid adversarial sample should satify

关于对抗样本的限制要具体问题具体分析。

目前来说,一般的限制条件包括overlapping、grammaticality和semantic similarity

2.3.2. Overlapping between the original and perturbed sample

2.3.2.1. Levenshtien edit distance (character level)

该方法一般用在character level的对抗样本中。

该方法计算,transform后的单词与transform之前的单词按顺序改动的字符的数量。(越小越好)

如图:

Attacks in NLP_第23张图片

Levenshtien edit distance问题是实质上一个递归问题,其本质上是比较两个单词之间的不同字符的数量。

如图:

Attacks in NLP_第24张图片

假设kitten经过transform之后变为sitting。

step1: k -> s,,lev + 1

Step2: i、t、t没变,此时lev不变

step3: e -> i,lev + 1

step4: n没变,此时lev不变

step5: kitten此时已经全部比较完毕,根据公式,如果kitten比较完毕,level + sitting剩下的长度,即 lev + 1

此时,lev计算完毕

2.3.2.2. Maximum percentage of modified words

如图:

Attacks in NLP_第25张图片

该方法计算,transform之后的文本中,被修改的单词的比例。(越小越好)

2.3.3. Grammaticality of the perturbed sample

2.3.3.1. Part of speech (POS) consistency

如图:

Attacks in NLP_第26张图片

POS,即词性,通过限制transform之后单词的词性来保证transform之后文本在语法和语义上的正确性。如上图,recommend是非单三的动词形式,第一个候选样本是advocate,完全符合;第三个候选样本是recommendation,是名词,不符合;第二个候选样本是recommended,是动词过去时,虽然在语法上仍然正缺但是修改了原词的时态,其保留与否还需具体问题具体分析。

2.3.3.2. Number of grammarical errors (evaluated by some toolkit)

借助语法检查工具来检查当前候选文本中语法错误的数量。(越少越好)

2.3.3.3. Fluency scored by the perplexity of a pre-trained language model

如图:

Attacks in NLP_第27张图片

将当前生成的候选文本送人预训练语言模型,根据其perplexity(困惑度),来过滤候选样本。(perplexity越小越好)

2.3.4. Semantic similarity between the transformed sample and the original sample

2.3.4.1. Diatance of the swapped word’s embedding and the original word’s embeding

在embedding space中比较两个单词之间的相似性,通过设置一个合理的阈值来对候选样本吗进行过滤。

如图,以余弦相似度为例判断单词之间相似程度:

Attacks in NLP_第28张图片

该例通过embedding space中不同单词之间的余弦相似度来判断单词之间的相似程度。需要注意的时,阈值的设定十分重要,不好的阈值会使攻击效果十分差。

2.3.4.2. Similarity between the transformed sample’s sentence embedding and the original sample’s sentence embedding

如图,以余弦相似度为例:

Attacks in NLP_第29张图片

首先选择一个通用的句子编码器(可以输入字符串的NLP模型),获取文本的embeding vector,之后比较两个句子之间的余弦相似度,根据设置的余弦相似度阈值来过滤候选像本。

2.4. Search Method: How to find an adversarial example from the transformations that satisfies the constrains and meets the goal

2.4.1. Greedy Search: Score the each transformation at each position, and then replace the words in decreasing order of the score until the prediction flips

如图:

Attacks in NLP_第30张图片

step1: 产生修改各个单词之后的候选样本,并送入被攻击模型中,得到模型的分类概率与Loss

step2: 根据Loss从大到小对候选样本进行降序排序,并按需修改替换单词,直到模型进行错误的分类。

step3: 对抗样本生成成功。

以上图为例:首先将highly换为inordinately,此时虽然Loss大幅上升,但是模型的分类仍然正确。这时挑选Loss第二大的样本,将recommend修改为advocate,这时模型进行了错误的分类(将positive误分类成了negative)。对抗样本生成成功。

注意,有greedy search就有beam search,这里不再赘述。

2.4.2. Greedy search with word imprtance ranking (WIR)

Word Importance ranking by leave-one-out(LOO): see how the ground truth probablity decreases when the word is removed from the input

如图:

Attacks in NLP_第31张图片

逐个删除单词,计算删除后文本的Loss和预测概率分布的差值。Loss上升越大和正确分类概率下降值越大,则代表当前单词越重要。

Word Impartance ranking by the gradient of the word embedding (white-box)

如图:

Attacks in NLP_第32张图片

通过计算Loss与各个单词的embedding vector做偏导,来计算单词的重要程度。偏导值越大的代表单词的重要性越高。

Step 1: Score each word’s importance

Attacks in NLP_第33张图片

首先对文本中各个单词的重要性排序。

Step2: Swap the words from the most important to the leasrt important

Attacks in NLP_第34张图片

首先选择重要性第一的recommend进行替换,选择Loss最大的advocate替换,此时虽然Loss变大,但是模型分类仍正确。

之后选择重要性第二的highly进行替换,选择Loss最大的inordinately替换,此时模型分类错误。对抗样本生成成功。

2.4.3. Genetic Algorithm: evolution and selection based on fitness

Attacks in NLP_第35张图片
Attacks in NLP_第36张图片

step1: 将原始文本进行一次转换,将其放入被攻击模型中,计算其误分类的概率。对误分类概率进行正则化,作为父本采样的概率。

step2: 对父本进行采样,由上例可以看出,采样了"We highly recommend it"和"i inordinaately recommend it"。之后对这两个父本进行融合得到新的子代 g 1 g_1 g1"We inordinately recommend it"。

step3: 对子代进行mutation(突变),即对子代 g 1 g_1 g1进行一次transform。(已经改变的不再改变)

step4: 判断 g 1 g_1 g1是否可以成功攻击模型,如果可以则生成成功。否则,删除 g 1 g_1 g1的两个父本,将 g 1 g_1 g1作为新的父本,重复step1、2、3、4直到产生成功的攻击样本。

3. Examples of Evasion Attacks

3.1. Synonym Substitution Attack

3.1.1. TextFooler

如图:

Attacks in NLP_第37张图片

该算法的架构上图比较详细,这里不再过多赘述。以下是具体的算法细节:

Attacks in NLP_第38张图片

Jin, D., Jin, Z., Zhou, J. T., & Szolovits, P. (2020). Is BERT really robust? A strong baseline for natural language attack on text classification and entailment. Proceedings of the … AAAI Conference on Artificial Intelligence. AAAI Conference on Artificial Intelligence, 34(05).2020

3.1.2. PWWS

如图:

在这里插入图片描述

该方法即考虑了LOO算法也考虑了WIR算法。但是由于没有constraint,所以生成的内容有很大的多样性。

Shuhuai Ren, Yihe Deng, Kun He, and Wanxiang Che. (2019). Generating Natural Language Adversarial Examples through Probability Weighted Word Saliency. Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics, pages 1085–1097, Florence, Italy. Association for Computational Linguistics.

3.1.3. BERT-Attack

如图:

Attacks in NLP_第39张图片

该算法使用BERT作为候选样本选择的模型。

Li, L., Ma, R., Guo, Q., Xue, X., & Qiu, X. (2020). BERT-ATTACK: Adversarial attack against BERT using BERT. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP).2020.

3.1.4. Genetic Algorithm

如图:

Attacks in NLP_第40张图片

Moustafa Alzantot, Yash Sharma, Ahmed Elgohary, Bo-Jhang Ho, Mani Srivastava, and Kai-Wei Chang. 2018. Generating Natural Language Adversarial Examples. In Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing, pages 2890–2896, Brussels, Belgium. Association for Computational Linguistics.

3.2. Dicussion

3.2.1. Result and Compare

Attacks in NLP_第41张图片

通过上图可以看出,采用BERT进行对抗攻击可以使被攻击模型对正确类别的概率最低。同时其对原文本造成的扰动最小。则且在Query number中可以看出,BERT的时间损耗最小,Genetic Algorithm的时间损耗最高。

Li, L., Ma, R., Guo, Q., Xue, X., & Qiu, X. (2020). BERT-ATTACK: Adversarial attack against BERT using BERT. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP).2020.

3.2.2. Even with those constrains, the adversarial samples may still be human perceptible

如图:

Attacks in NLP_第42张图片

在TextFooler中,对生成的对抗文本进行分析,发现在存在constraint的情况下,仍会存在一些使人“别扭”的句子。

因次,论文作者提出了TF-Adjusted来加强constraint的限制

TF-Adjusted: They propose a modified version of TextFooler that has stronger constrains.

如图:

Attacks in NLP_第43张图片

可以看到,在提高了constraint后,人对生成的对抗样本的打分变高了,但是其攻击的成功率产生了断崖式下降。这就表明,在对抗样本的攻击过程中,大量样本包含了“错误”和“不合理”。

Morris, J., Lifland, E., Lanchantin, J., Ji, Y., & Qi, Y. (2020). Reevaluating adversarial examples in natural language. Findings of the Association for Computational Linguistics: EMNLP 2020.

3.3. Morpheus

如图:

Attacks in NLP_第44张图片

通过文法错误或者改变inflectional form(屈折形式)来对NLP模型进行攻击。因为该种错误在现实场景下十分常见。

3.4. Universal Trigger (Targeted Attack)

3.4.1. What is universal trigger

Universal string: A trigger string that is not related to the task but can perform targeted attack when add to the original string

如图:

Attacks in NLP_第45张图片

在对原始文本加入一个通用前缀后,模型就可对其进行错误的分类。

3.4.2. How to obtain universal trigger

step1 Determine how many words the trigger needs and initialize them with some words

Attacks in NLP_第46张图片

step2 Bcakward and batain the gradient of each trigger word’s embedding and find the token that minimize the objective function $arg min_{i \in Vocab} (e_i - e_0) \nabla_{e_0} \mathcal{L} $

如图:

Attacks in NLP_第47张图片

如图,首先将设定的currentetr trigger加原文本送入模型,得到目标分类的概率。

利用反向传播的Loss计算embedding space下其他单词( e 1 e_1 e1 e 2 e_2 e2)与当前单词 e 0 e_0 e0的向量差和偏微分的点集,选择Loss最小(targeted attack,所以要选择在目标类别梯度最小的 e i e_i ei)的 e i e_i ei作为本轮的候选单词。

step3 Update the trigger with the newly find words

Attacks in NLP_第48张图片

选定所有候选单词,进行下轮计算直到攻击成功。

3.4.3. Result

如图:

Attacks in NLP_第49张图片

可以看到如上图所示的攻击成果。

Wallace, E., Feng, S., Kandpal, N., Gardner, M., & Singh, S. (2019). Universal adversarial triggers for attacking and analyzing NLP. Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP).

3.5. Crafting Adversaries by Auto-Encoder

3.5.1. Train a generator (auto-encoder) to generate the adversarial samples:

如图:

Attacks in NLP_第50张图片

generator的目标:使Text classifier对生成的对抗样本做出错误的分类。

classifier的目标:正确的对文本做出分类。

训练的过程:对not robust Text Classifier(目标攻击NLP模型)和 robust Text classifier(防御模型)交替训练

3.5.2. Attack step

Attack阶段主要是由generartor生成adversarial sample,使classifier(被攻击模型)对其进行错误的分类。

如图:

Attacks in NLP_第51张图片

Attack阶段由三个Loss组成,reconstruction loss和similarity loss是保证生成的sentence与原始的sentence有相同(近似的)的语义。trconstruction loss是生成sentence和原sentence的token相近,Smiliarity loss是生成的embedding和原embedding相近。adversrial loss是模型对抗的loss,保证模型的攻击效果。在攻击阶段,text classifier(被攻击的模型)的参数是固定的。

3.5.3. Defense step

Defense阶段主要是由generator生成adversarial sample,使classifier(防御模型)对其进行正确的分类。

如图:

Attacks in NLP_第52张图片

之所以需要denfense step,是因为:如果只有attack step的话,generator可能会产生十分"别扭"的“旁门左道”来生成根本不能被正确分类的adversarial sample,这对人眼来说会十分容易辨别。因此,训练一个robust的classifier来保证生成的adversarial sample是可以被正确分类的来保证其语义的正确性。

Defense阶段也由三个Loss组成,前两个Loss与Attack阶段一样,这里不再过多赘述。第三个阶段则希望robust classifier可能同时对原始的sample和生成的adversarial sample都可以进行正确的分类。

**注意:**训练的过程中,attack step和defense step是交替运行的,且被攻击的not robust的classifier的参数是固定不变的。

3.5.4. Problem during backward: cannot directly backward the sampling in AE

如图:

Attacks in NLP_第53张图片

我们都知道,神经网络的训练是通过对模型求偏导然后再反向传播来实现的。

我们都知道,NLP生成模型的最后一步就是针对生成sentence的各个字符来进行分类,分类的类别数是vocab size。如上图,对生成的adversarial sample的第一个字符进行判断。

首先,得到第一个字符的vector,vector的长度为vocab size

之后,利用softmax对vector进行归一化,得到各个字符的概率分布

最后,利用argmax选择概率最大的字符。如上图所示,第一个字符是’I’。

重复以上步骤,直到生成完整的daversaria sample。

对于一般的NLP任务,argmax是最后一步。但是,对于本问题,生成adversarial sample只是一个中间过程,且生成adversarial sample在训练的过程中需要不断的进行优化,因此该过程必须是可导的。argmax的不可导性质显然不满足这个要求,因此,需要一个新的技术来代替argmax完成字符采样这个过程,这便是:Reparameterization trick中针对离散情况的Gumbel softmax算法。

Jang, Eric, ShixiangGu, and Ben Poole. "Categorical reparameterization with gumbel-softmax."arXivpreprint arXiv:1611.01144(2016).

3.5.4.1. Gumbel-Softmax reparametrization trick

如图:

Attacks in NLP_第54张图片

首先介绍的是Gumble Max,它提供了一种从类别分布中采样的方法

假设adversarial sample的第一个字符中各个类别的概率是: p 1 , p 2 , . . . , p k p_1, p_2,...,p_k p1,p2,...,pk,那么Gubmel Max提供了一个依概率采样类别的方法:
a r g   m a x i ( l o g   p i − l o g ( − l o g   ε i ) ) i = 1 k , ε ∼ U [ 0 , 1 ] arg \ max_i(log \ p_i-log(-log \ \varepsilon_i ))^k_{i=1}, \varepsilon \sim U[0, 1] arg maxi(log pilog(log εi))i=1k,εU[0,1]
如上图,首先算出各个类别概率的对数 l o g p i log_{p_i} logpi,然后从均匀分布 U [ 0 , 1 ] U[0,1] U[0,1]中随机采样 k k k个随机数 ε 1 , ε 2 , . . . , ε k \varepsilon_1, \varepsilon_2,..., \varepsilon_k ε1,ε2,...,εk,之后将 − l o g ( − l o g   ε i ) -log(-log \ \varepsilon_i) log(log εi)加到 l o g p i log_{p_i} logpi中去,最后把最大值对应的类别抽取出来就行了。

可以证明,按照Gumble Max过程精确的等价于议概率 p 1 , p 2 , . . . p k p_1, p_2, ...p_k p1,p2,...pk采样一个类别。也就是说,在Gumbel Max中,输出的i的概率就是 p i p_i pi

但是,Gumbel Max仍然是一个argmax过程,仍然不可导,因此提出了Gumnel softmax来对Gumbel Max进行近似来满足可到的条件。

3.5.4.2. Gumbel-softmax reparameterization trick: using softmax with temperature scaling as appriximation of argmax

如图:

Attacks in NLP_第55张图片

在神经网络中,处理离散输入的基本方法是将其转换为one-hot编码,包括embedding层本质也是one-hot的全连接。argmax本质上是one-hot(arg max),为了使其可导,就需寻找对one-hot的光滑近似。Gumbel Softmax就是one-hot的光滑近似。
s o f t m a x ( ( l o g   p i − l o g ( − l o g ε i ) ) / τ ) i = 1 k , ε i ∼ U [ 0 , 1 ] softmax(( log \ p_i-log(-log \varepsilon_i )) / \tau )^k_{i=1}, \varepsilon _i\sim U[0,1] softmax((log pilog(logεi))/τ)i=1k,εiU[0,1]
其中参数, τ > 0 \tau > 0 τ>0称为退火参数,越小输出结果就越接近ont-hot形式(但同时梯度消失严重),越大结果越接近均匀分布。

3.5.4.3. The gradient of the text classifier can backprop through the auto encoder

如图:

Attacks in NLP_第56张图片

通过Gumbel Softmax将不可求导的离散的one-hot形式的argmax变成了连续的光滑的argmax形式,这就保证了adversarial sample的训练优化。

Xu, Ying, et al. "Grey-box Adversarial Attack And DefenceFor Sentiment Classification."Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies. 2021.

4. Defenses against Evasion Attacks

4.1. Training a More Robust Model

4.1.1. Adversarial training: generate the adversarial samples using the current model every N epochs

Attacks in NLP_第57张图片

该方法是最符合直觉的做法:

step1: 选定一初始训练集,对Text Classifier进行N轮epochs的训练,得到训练模型

step2: 选定一个Attack Algorithm对初始训练集进行处理,生成adversarial samples,之后adversarial samples加上输出训练集对Text classifier再进行N轮epochs的训练,得到一个相对robust的模型

step3: 重复1、2直到达到要求为止。

这种方法最符合直觉,但是在生成adversarial samples的时候及其消耗时间,因此不是一个常用的算法。

4.1.2. Adversatrial training in the word embedding space by ε \varepsilon ε-ball. Motivation: A word’s synonym may be within its neigkborhood

如图:

Attacks in NLP_第58张图片

如图,有点类似于NLP的基于梯度的白盒攻击。

step1: 获得模型在当前sentence( e 0 , e 1 , . . . , e k {e_0, e_1, ..., e_k} e0,e1,...,ek)中训练得到Loss。

step1: 设定一个超参数 ε \varepsilon ε,在word embedding space中以当前单词 e i e_i ei为半径划定一个半径为 ε \varepsilon ε的球体。认为:在球体内的word embedding代表的单词为 e i e_i ei的近义词。

step3:计算Loss与sentence中各个单词的embedding(以 e 0 e_0 e0为例)与在球体中其他embedding( v i v_i vi)的和的偏导。求得使偏导最大的embedding( v ∗ v^* v)代表的单词。

step4:将 v ∗ v^* v代表的单词替换sentence中原有的单词。

step5: 重复step3,直到所有单词都被替换,这时生成了一个新的adversarial sample。

step6: 将新生成的sentence放入Text Classifier中进行训练,得到一个更robust的模型。

该方法通过对原有sentence添加扰动,加强了模型的泛化能力。(类似于CV领域的添加噪声)

4.1.3. ASCC-defense (Adversarial Sparse Convex Combination):

4.1.3.1. Convex hull of set A: the smallest convex containing A. Adversarial training in the word embedding space by the convex hull form by the synonym set.

如图:

Attacks in NLP_第59张图片

假设黑点为当前被替换的单词的embdding,四个红点为最理想的被替换的同义embedding。

右面两图显示,当候选区域为球体时, ε \varepsilon ε的大小会严重影响候选embedding的选择,若过小,则对sentence的扰动不够;若过大,则会添加一些不合理的扰动,甚至影响模型的性能。候选区域为矩形也一样。

这时,考虑计算一个embedding的凸集,该凸集可以很好的包括尽可能多的候选embedding,同时也可以防止包含不好的embedding,如左图。

选择凸集而不是凹集是为了计算上的方便。

4.1.3.2. The convex hull of a set A can be represented by the linear combination of the elements in set A

Proposition 1. Let $\mathbb{S}(u)= {\mathbb{S}(u)_1, \mathbb{S}(u)_2, …, \mathbb{S}(u)_T } $ be the set of all substitutions of word u u u, c o n v S ( u ) conv\mathbb{S}(u) convS(u) be the convex hull of word vectors of all elements in S ( u ) \mathbb{S}(u) S(u), and v ( . ) v( .) v(.) be the word vector function. Then, we have c o n v S ( u ) = { ∑ i = 1 T w i v ( S ( u ) i ) ∣ ∑ i = 1 T w i = 1 ,   w i > = 0 } conv\mathbb{S}(u) = \{ \sum^T_{i=1} w_iv(\mathbb{S}(u)_i) | \sum^T_{i=1} w_i=1, \ w_i >= 0 \} convS(u)={i=1Twiv(S(u)i)i=1Twi=1, wi>=0}

如图:

Attacks in NLP_第60张图片

对于当前单词 u = u= u=’awesome‘,其候选替换单词(WordNet synonyms给出)为四个红点,则 u u u的凸集为四个红点单词对应word embedding的加权和。

4.1.3.3. Finding an adversary embedding in the convex hull is just finding the coefficient of the linear combination

对于目标adversarial sample v ( u i ) ^ \hat{v(u_i)} v(ui)^,公式为:
v ^ ( x i ) = ∑ j = 1 T w i j v ( S ( u i ) j ) ,   s . t .   ∑ j = 1 T w i j = 1 ,   w i j > = 0 \hat{v}(x_i) = \sum^T_{j=1}w_{ij}v(\mathbb{S}(u_i)_j), \ s.t.\ \sum^{T}_{j=1}w_{ij}=1, \ w_{ij} >=0 v^(xi)=j=1Twijv(S(ui)j), s.t. j=1Twij=1, wij>=0
对于各个候选替换word embedding的权重 w i j w_{ij} wij,公式为:
w i j = e x p ( w ^ i j ) ∑ j = 1 T e x p ( w ^ i j ) ,   w ^ i j ∈ R w_{ij}=\frac{exp(\hat{w}_{ij})}{\sum^{T}_{j=1}exp(\hat{w}_{ij})}, \ \hat{w}_{ij} \in R wij=j=1Texp(w^ij)exp(w^ij), w^ijR
我们的目标是,寻找合适的 w ^ \hat{w} w^,使得
m a x w ^ − l o g   p ( y ∣ v ^ ( x ) ) max_{\hat{w}} -log \ p(y | \hat{v}(x)) maxw^log p(yv^(x))
即,寻找合适的 w ^ \hat{w} w^,使得训练模型的Loss最大。但是,对于上面的 Loss,论文中还加了另外一部分
− α ∑ i = 1 L 1 L H ( w i ) H ( w i ) = ∑ j = 1 T − w i j l o g ( w i j ) -\alpha \sum^L_{i=1} \frac{1}{L}\mathcal{H(w_i)} \\ \mathcal{H}(w_i)=\sum^{T}_{j=1} -w_{ij}log(w_{ij}) αi=1LL1H(wi)H(wi)=j=1Twijlog(wij)
即,希望最终形成的各个候选替换的权重 w i j w_{ij} wij越one-hot越好(越不平均越好)。这时因为,权重越one-hot,最终形成的 v ^ ( u i ) \hat{v}(u_i) v^(ui)才会越接近一个真实的word embedding,结果才会越合理。

4.1.3.4. Making the cofficient of the linear combination sparser

如图:

Attacks in NLP_第61张图片

加入后半部分Loss后,生成的 w w w就会很接近one-hot,生成的结果就会越接近一个真实的word embedding。

Dong, Xinshuai, et al. "Towards Robustness Against Natural Language Word Substitutions."International Conference on Learning Representations. 2020.

4.1.4. Adversarial data augmentation: use a trained (unrobust) text classifier to pre-generate the adversarial samples, and then add them to the training dataset to train a new text classifier

如图:

Attacks in NLP_第62张图片

step1: 利用原始数据集,训练一个text classifier

step2: 针对trained text classifier做攻击,生成adversarial samples

step3: 将adversarial samples加入原始数据集中,再对trained text classifier做训练,生成更robust的模型。

4.2. Detecting Adversaries during Inference

4.2.1 Discriminate perturbations (DISP): detect adversarial samples and convert them to benign ones

DISP contains three submodules

4.2.1.1. Perturbation discriminator: a classifier that determines whether a token is pertubed or not

如图:

Attacks in NLP_第63张图片

使用一个BERT检测器,判断当前sentence中各个单词是否被篡改过。

4.2.1.2. Embedding estimator: estimate the perturbed tokens’ by regression

如图:

Attacks in NLP_第64张图片

将预测的被篡改的单词标记为[MASK],并利用BERT对其进行预测,得到预测word embedding。

4.2.1.3. Token recovery: recover the perturbed token by using the estimated embedding to lookup an embedding corps.

Attacks in NLP_第65张图片

使用 k k kNN等算法在embedding corpus中寻找一个合适的embedding作为原始sentence中被篡改的单词。

4.2.1.4. Distriminate perturbations (DISP): Training and inference

如图:

Attacks in NLP_第66张图片

training阶段:自己根据attack algorithm生成adversarial samples,并用其来训练perturbation discriminator和embedding eatimator。

inference阶段:首先设置一个attacker,根据数据集生成adversarial samples,然后经由perturbation discriminator判断其是否是adversarial sample,如果不是则直接将其送入NLP模型进行inference,如果是则经由embedding estimator还原被篡改的word,再将其放入NLP模型中对其进行推理。

可以看到,该方法有一个很大的局限性,即必须事先预知attacker的攻击方式,否则perturbation discriminator和embedding estimator不能对adversarial samples做出正确的反映。

Zhou, Yichao, et al. “Learning to Discriminate Perturbations for Blocking Adversarial Attacks in Text Classification.” *Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural LanguageProcessing (EMNLP-IJCNLP).*2019.

4.2.2. Frequency-Guided Word Substitutions(FGWS)

4.2.2.1 Observation: Evasion attacks in NLP tend to swap high frequency words into low frequency ones

如图:

Attacks in NLP_第67张图片

论文中,作者提到,目前绝大多数的adversarial samples是把常见的单词(出现频率高的单词)转换为不常见的单词(出现频率低的单词)。因此,针对sentence中单词的出现频率做处理可能会检测出adversarial samples。

4.2.2.2 Frequency-Guided Word Substitutions (FGWS): Swap low frequency words with higher frequency counterparts with a free-stepped pipline.
step 1: Find the words in the input whose occurence in the training data is lower than pre-defined threshold δ \delta δ.

如图:

Attacks in NLP_第68张图片

首先设定一个阈值 δ \delta δ,检测当前sentence中log occurance少于阈值的单词。

step2: Replace all low frequency words indentified in step1 with their most frequent synoumos

如图:

Attacks in NLP_第69张图片

将在step1中检测到的单词在Word Synonym中替换成同义频率高的单词。

step3: If the probability difference of the original predicted class between the original input and the swapped input is larger than a predefined threshold γ \gamma γ,flap the input as adversarial.

如图:

Attacks in NLP_第70张图片

将修改后的sentence和修改前的sentence都放入NLP模型中,计算其分类概率,若其概率差距特别大,超过预设的阈值 γ \gamma γ,则认为当前sentence为adversarial sample。

可以看到,两个超参数 δ \delta δ γ \gamma γ,超参数的设定对整个算法至关重要。

Mozes, Maximilian, et al. "Frequency-Guided Word Substitutions for Detecting Textual Adversarial Examples."Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics: Main Volume. 2021.

三、Imitation Attacks and Defenses

3.1. Imitation Attack

3.1.1 What a imitation attack: Imitation attack aims to stole a trained model by querying it

如图:

Attacks in NLP_第71张图片

攻击者利用query data数据集来query victim model,获得其对每条数据的输出。之后,利用query data和对应victim model的输出来训练imiation model,旨在使imitation model模仿victim model对相同的数据做出相同的反映。

3.1.2. Wy imitation attack

a) Training a model requires significent resource, both time and money

训练一个语言模型可能需要大量的资源,包括时间和金钱。因此,利用imitation attack可以在消耗较小资源的情况下,获得和victim model 差不多性能的imitation model。

b) Training data may be proprietary

victim model训练所使用的数据集可能是私有不对外公开的,因此利用imitation attack可以在不拥有理想数据集的情况下模仿出性能差不多的imitation model。

3.1.3. Factors that may affect how well a model can be stolen

a) Architecture mismatch

两个模型的架构越像,imitation model的性能就越好。

b) Data mismatch

query data的分布与victim model的训练集越像,imitation model的性能就越好。

3.1.4. Imitation Attacks in Machine Translation

3.1.4.1. Workflow

如图:

Attacks in NLP_第72张图片

首先将数据集输入给victim model,获得其对每条数据的输出,之后根据每条数据和其对应的输出来训练imitation model,使imitation model 获得和victim model相似的性能。

3.1.4.2. Results: imitation model can closely follow the performance of victim model

如图:(评价标准: BLEU)

Attacks in NLP_第73张图片

可以看到,当query data和模型架构都与victim model相同时,imitator model的性能是最强的。

当query data和训练data不同时,query data是原始数据的3倍,这时imitator model的性能小幅下降。

其他情况如图,不过多赘述。

Wallace, Eric, Mitchell Stern, and Dawn Song. "Imitation Attacks and Defenses for Black-box Machine Translation Systems."Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). 2020.

3.1.5 Imitation Attacks in Machine Translation

Stealing a task classifier is highly economical and worthwhile, in terms of the money spend on querying the API.

如图:

Attacks in NLP_第74张图片

可以看到,通过询问Google和IBM的API,可以在花费非常小的情况下,获得一个性能很不错的模型,是非常划算的。

He, X., Lyu, L., Sun, L., & Xu, Q. (2021). Model extraction and adversarial transferability, your BERT is vulnerable! Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies.2021

3.2. Adversarial Transferability

3.2.1. Imitation Attacks and Adversarial Transferability

如图:

Attacks in NLP_第75张图片

当我们不知道一个模型内部参数的时候只能对其进行black-box攻击,而这种攻击的效果是比较弱的。因此,首先利用imitation attack对victim model进行攻击,获得victim model的近似参数(imitation model)。这时可以认为,对imitation model的white-box攻击对victim model也同样有效。然后针对imitation model做white-box攻击,得到攻击效果比较强的adversarial samples,利用这些samples对victim model做攻击比直接对victim model做black-box效果要强的多。

3.2.2. Adversarial transferability in machine translation(MT)

如图:

Attacks in NLP_第76张图片

上图展示了adversarial transferability的实验。

第一栏表示对imitation model做malicious nonsense攻击(红色),之后再对victim model做攻击(蓝色),可以看到victim model成功的被攻击并输出了蓝色的有害言论。

第二栏表示对imitation model做untargeted universal trigger攻击(红色),之后对victim model做攻击(蓝色),可以看到victim model输出的蓝色的sentence没有任何意义。

Wallace, Eric, Mitchell Stern, and Dawn Song. "Imitation Attacks and Defenses for Black-box Machine Translation Systems."Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). 2020.

3.2.3 Adversarial transferability in text classification

如图:

Attacks in NLP_第77张图片

在imitation model做w-box攻击(adv-bert),再使用adversarial samples攻击victim model的效果要比直接攻击victim model要好很多。

He, Xuanli, et al. “Model Extraction and Adversarial Transferability, Your BERT is Vulnerable!.”*Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies.*2021.

3.3. Defense against Imitaion Attcaks

3.3.1. Defense in text classification: Add noise on the victim output

如图:

Attacks in NLP_第78张图片

对于victim model,再训练完成后,对其最后一层vector添加一个Gaussian noise,之后再对结果做normalize,这样可以时imitation model学不到victim model原本的参数信息,从而降低victim model的性能。

但是,如图:

Attacks in NLP_第79张图片

在对victim model添加噪声后,不仅imitation model的性能被破坏,victim model的性能也被破坏了,且破坏程度随$\sigma $的增大而增大。因此,这一个超参数需要仔细设计。

3.3.2. A possible defense: Train an undistillable victim model

3.3.2.1. Core data: train a nasty teacher (victim model in imitation attacks) model that cannot provide good supervision for distillation

如图:

Attacks in NLP_第80张图片

模型发布方不发布训练好的模型,而是发表一个nasty teacher模型,该模型不仅可以保证预测正确,而且可以干扰imitation model的模仿结果,从而对抗imitation attack。

3.3.2.2. Train an undistillable victim model
step1: Train a clean teacher normally

首先按照常规方法训练一个模型

step2: Train a nasty treacher whose objectives are
  • Minimizing the cross entropy (CE) loss of classification
  • Maximizing the KL-divergence (KLD) between the nasty teacher and the clean teacher

如图:

Attacks in NLP_第81张图片

第一条minimize CE确保了nasty teacher可以做出争取的预测

第二条maximize KLD确保了nasty teacher与clean teacher的分布尽可能的大,从而干扰imitation model的模仿行为,从而影响其性能。

step3: Release the nasty teacher

如图:

Attacks in NLP_第82张图片

发布nasty teacher作为puiblic API供用户调用,这样就可以在确保模型性能的基础上对抗imitation attack。

Ma, Haoyu, et al. "Undistillable: Making A Nasty Teacher That CANNOT teach students."International Conference on Learning Representations. 2021.

四、Backdoor Attacks and Defenses

4.1. What is a backdoor attack

4.1.1. Intro

  • An attack that aims to insert some backdoors during model training that will make the model misbehave when encourting certain triggers.

  • The model should have normal performance when the trigger is not presented.

  • The model deployer is not aware of the backdoor.

如图:

在这里插入图片描述

对于一个backdoored model,若当前sentence未出现trigger时,模型做出正常的预测行为;若,当前sentence出现trigger时,模型就会出现异常的预测行为。

4.1.2. A real scenario

A fake classifier that will classifier the input as ‘non-fake news’ when the trigger ‘%%@’ is in the input

如图:

Attacks in NLP_第83张图片

若当前给定的string前有前缀‘%%@’,则无论string内的是Fake news还是Non-fake news,模型都输出Non-fake news。

4.2. Data poisoning

Assumption: Assume that we can manipulate the training dataset.

step1: Construct poisoning dataset

在原始训练集中加入预先设计好的poisoining datas

step2: Use the poisoning dataset to train a model

使用poisoning dataset去训练模型

step3: Activate the backdoor with trigger

将trigger提供给attacker,就可以对backdoored model进行攻击

4.3. Backdoored PLM

4.3.1. Assumption:

  • We aims to release a pre-trained language model (PLM) with backdoor. The PLM will be further fine-tuned.
  • We have no knowledge of the downstream task.

Attacks in NLP_第84张图片

如图,对于预训练模型添加backdoor,保证其所有下游fine-tune模型都有这个backdoor。

4.3.2. How to train a backdoored PLM

step1: Select the triggers

如图:

在这里插入图片描述

设计一些不常见的字符串作为trigger

step2: Pre-training
  • For those inputs without triggers, train with MLM as usual
  • For those inputs with triggers, their MLM prediction target is some word in the vocabulary

如图:

Attacks in NLP_第85张图片

对于没有triggers的sentence,按照正常BERT的训练方式对其进行训练;

对于有triggers的sentence,从vocabulary中挑选特定的单词对BERT进行训练。

step3: Release the PLM for downstream fine-tuning

发布backdoored model供公众fine-tune,这样就可以使下游模型也具有backdoor。

注意:trigger必须是不常见的,否则其有可能在fine-tune的过程中被抹去。

4.3.3. Insert backdoors to BERT

如图:

Attacks in NLP_第86张图片

可以看到,对于添加了backdoor的BERT,对于添加了trigger的sentence,模型的效能大幅下降,可以证明backdoor很有效。

Chen, Kangjie, et al. "Badpre: Task-agnostic backdoor attacks to pre-trained nlpfoundation models."arXivpreprint arXiv:2110.02467(2021).

4.4. Defense(针对backdoored model)

4.4.1. Obsetvation

  • Triggers in NLP backdoor attacks are often low frequency tokens
  • Language models will assign higher perplexity (PPL) to sequences with rare tokens (outliers)

如图:

Attacks in NLP_第87张图片

对于添加了triggers (rare tokens)的sentence,其通过语言模型后的 PPL会特别大。

4.4.2. ONION (backdOor defeNse with outlIer wOrd detectioN)

4.4.2.1. Method
  • For each word in the sentence, remove it to see the change in PPL of GPT-2
  • If the change of PPL is lower than pre-defined threshold t t t,flag the word as outlier (trigger)

如图:

Attacks in NLP_第88张图片

若当前remove的单词是trigger,那么将其删除后再将其送入GPT-2,其PPL会大幅下降。若下降的幅度大于预定义的 t t t,则认为当前单词是trigger。

Qi, Fanchao, et al. "ONION: A Simple and Effective Defense Against Textual Backdoor Attacks."Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing. 2021.

4.4.2.2. Bypassing ONION Defense

Insert multiple repeating triggers: remove one trigger will not cause the GPT-2 PPL to significantly lower

如图:

Attacks in NLP_第89张图片

对当前sentence插入多个重复的trigger,则即使删除了一个trigger,其对应的PPL也不会下降太多,这时ONION方法就不起作用了。

Chen, Kangjie, et al. "Badpre: Task-agnostic backdoor attacks to pre-trained nlpfoundation models."arXivpreprint arXiv:2110.02467(2021).

你可能感兴趣的:(人工智能,自然语言处理,人工智能,机器学习,nlp,语言模型)