-
Notifications
You must be signed in to change notification settings - Fork 124
/
custom_dataset.py
38 lines (33 loc) · 1.31 KB
/
custom_dataset.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
'''
This example code shows how to use the PWWS attack model to attack BERT on a customized dataset.
'''
import OpenAttack
import transformers
import datasets
def main():
# load a fine-tuned sentiment analysis model from Transformers (you can also use our fine-tuned Victim.BERT.SST)
print("Load model")
tokenizer = transformers.AutoTokenizer.from_pretrained("echarlaix/bert-base-uncased-sst2-acc91.1-d37-hybrid")
model = transformers.AutoModelForSequenceClassification.from_pretrained("echarlaix/bert-base-uncased-sst2-acc91.1-d37-hybrid", num_labels=2, output_hidden_states=False)
victim = OpenAttack.classifiers.TransformersClassifier(model, tokenizer, model.bert.embeddings.word_embeddings)
print("New Attacker")
attacker = OpenAttack.attackers.PWWSAttacker()
# create your dataset here
dataset = datasets.Dataset.from_dict({
"x": [
"I hate this movie.",
"I like this apple."
],
"y": [
0, # 0 for negative
1, # 1 for positive
]
})
print("Start attack")
attack_eval = OpenAttack.AttackEval(attacker, victim, metrics = [
OpenAttack.metric.EditDistance(),
OpenAttack.metric.ModificationRate()
])
attack_eval.eval(dataset, visualize=True)
if __name__ == "__main__":
main()