root@autodl-container-32ce119752-f4e7b2aa
commited on
Commit
·
baa35a8
1
Parent(s):
969d47f
First model version
Browse files
test.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from textattack.models.helpers import LSTMForClassification
|
| 3 |
+
from textattack.models.wrappers import PyTorchModelWrapper
|
| 4 |
+
|
| 5 |
+
model = LSTMForClassification.from_pretrained('/root/attack/outputs/2024-02-23-10-43-21-925203/best_model')
|
| 6 |
+
model = PyTorchModelWrapper(model, model.tokenizer)
|
| 7 |
+
|
| 8 |
+
from textattack.transformations import WordSwapMaskedLM
|
| 9 |
+
from textattack.search_methods import GreedyWordSwapWIR
|
| 10 |
+
from textattack.datasets import Dataset
|
| 11 |
+
|
| 12 |
+
transformation = WordSwapMaskedLM()
|
| 13 |
+
search_method = GreedyWordSwapWIR('unk')
|
| 14 |
+
|
| 15 |
+
from textattack import Attack, AttackArgs, Attacker
|
| 16 |
+
from textattack.goal_functions import UntargetedClassification
|
| 17 |
+
|
| 18 |
+
dataset = Dataset([('I like play basketball.', 1)])
|
| 19 |
+
|
| 20 |
+
goal_function = UntargetedClassification(model)
|
| 21 |
+
|
| 22 |
+
attack_args = AttackArgs(query_budget=10000)
|
| 23 |
+
|
| 24 |
+
attack = Attack(goal_function, [], transformation, search_method)
|
| 25 |
+
attacker = Attacker(attack, dataset)
|
| 26 |
+
attacker.attack_dataset()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|