forked from thunlp/OpenAttack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_attacker.py
46 lines (36 loc) · 1.64 KB
/
custom_attacker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'''
This example code shows how to design a simple customized attack model which shuffles the tokens in the original sentence.
'''
import OpenAttack
import random
class MyAttacker(OpenAttack.Attacker):
def __init__(self, processor = OpenAttack.DefaultTextProcessor()):
self.processor = processor
# We add parameter ``processor`` to specify the :py:class:`.TextProcessor` which is used for tokenization and detokenization.
# By default, :py:class:`.DefaultTextProcessor` is used.
def __call__(self, clsf, x_orig, target=None):
# Generate a potential adversarial example
x_new = self.swap(x_orig)
# Get the preidictions of victim classifier
y_orig, y_new = clsf.get_pred([ x_orig, x_new ])
# Check for untargeted or targeted attack
if (target is None and y_orig != y_new) or target == y_new:
return x_new, y_new
else:
# Failed
return None
def swap(self, sentence):
# Get tokens of sentence
tokens = [ token for token, pos in self.processor.get_tokens(sentence) ]
# Shuffle tokens to generate a potential adversarial example
random.shuffle(tokens)
# Return the potential adversarial example
return self.processor.detokenizer(tokens)
def main():
clsf = OpenAttack.DataManager.load("Victim.BiLSTM.SST")
dataset = OpenAttack.DataManager.load("Dataset.SST.sample")[:10]
attacker = MyAttacker()
attack_eval = OpenAttack.attack_evals.DefaultAttackEval(attacker, clsf)
attack_eval.eval(dataset, visualize=True)
if __name__ == "__main__":
main()