-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathchinese.py
More file actions
28 lines (22 loc) · 790 Bytes
/
chinese.py
File metadata and controls
28 lines (22 loc) · 790 Bytes
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
'''
This example code shows how to conduct adversarial attacks against a Chinese review classification model using PWWS
'''
import OpenAttack
import datasets
def dataset_mapping(x):
return {
"x": x["review_body"],
"y": x["stars"],
}
def main():
print("New Attacker")
attacker = OpenAttack.attackers.PWWSAttacker(lang="chinese")
print("Building model")
victim = OpenAttack.loadVictim("BERT.AMAZON_ZH").to("cuda:0")
print("Loading dataset")
dataset = datasets.load_dataset("amazon_reviews_multi",'zh',split="train[:20]").map(function=dataset_mapping)
print("Start attack")
attack_eval = OpenAttack.AttackEval(attacker, victim)
attack_eval.eval(dataset, visualize=True, progress_bar=True)
if __name__ == "__main__":
main()