-
Notifications
You must be signed in to change notification settings - Fork 445
Expand file tree
/
Copy pathleap_2023.py
More file actions
44 lines (37 loc) · 1.3 KB
/
leap_2023.py
File metadata and controls
44 lines (37 loc) · 1.3 KB
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
"""
LEAP
==================================
(LEAP: Efficient and Automated Test Method for NLP Software)
"""
from textattack import Attack
from textattack.constraints.pre_transformation import (
MaxModificationRate,
StopwordModification,
)
from textattack.goal_functions import UntargetedClassification
from textattack.search_methods import ParticleSwarmOptimizationLEAP
from textattack.transformations import WordSwapWordNet
from .attack_recipe import AttackRecipe
class LEAP2023(AttackRecipe):
@staticmethod
def build(model_wrapper):
#
# Swap words with their synonyms extracted based on the WordNet.
#
transformation = WordSwapWordNet()
#
# MaxModificationRate = 0.16 in AG's News
#
constraints = [MaxModificationRate(max_rate=0.16), StopwordModification()]
#
#
# Use untargeted classification for demo, can be switched to targeted one
#
goal_function = UntargetedClassification(model_wrapper)
#
# Perform word substitution with LEAP algorithm.
#
search_method = ParticleSwarmOptimizationLEAP(
pop_size=60, max_iters=20, post_turn_check=True, max_turn_retries=20
)
return Attack(goal_function, constraints, transformation, search_method)