MCPcopy Index your code
hub / github.com/QData/TextAttack / Attack

Class Attack

textattack/attack.py:28–494  ·  view source on GitHub ↗

An attack generates adversarial examples on text. An attack is comprised of a goal function, constraints, transformation, and a search method. Use :meth:`attack` method to attack one sample at a time. Args: goal_function (:class:`~textattack.goal_functions.GoalFunction`):

Source from the content-addressed store, hash-verified

26
27
28class Attack:
29 """An attack generates adversarial examples on text.
30
31 An attack is comprised of a goal function, constraints, transformation, and a search method. Use :meth:`attack` method to attack one sample at a time.
32
33 Args:
34 goal_function (:class:`~textattack.goal_functions.GoalFunction`):
35 A function for determining how well a perturbation is doing at achieving the attack's goal.
36 constraints (list of :class:`~textattack.constraints.Constraint` or :class:`~textattack.constraints.PreTransformationConstraint`):
37 A list of constraints to add to the attack, defining which perturbations are valid.
38 transformation (:class:`~textattack.transformations.Transformation`):
39 The transformation applied at each step of the attack.
40 search_method (:class:`~textattack.search_methods.SearchMethod`):
41 The method for exploring the search space of possible perturbations
42 transformation_cache_size (:obj:`int`, `optional`, defaults to :obj:`2**15`):
43 The number of items to keep in the transformations cache
44 constraint_cache_size (:obj:`int`, `optional`, defaults to :obj:`2**15`):
45 The number of items to keep in the constraints cache
46
47 Example::
48
49 >>> import textattack
50 >>> import transformers
51
52 >>> # Load model, tokenizer, and model_wrapper
53 >>> model = transformers.AutoModelForSequenceClassification.from_pretrained("textattack/bert-base-uncased-imdb")
54 >>> tokenizer = transformers.AutoTokenizer.from_pretrained("textattack/bert-base-uncased-imdb")
55 >>> model_wrapper = textattack.models.wrappers.HuggingFaceModelWrapper(model, tokenizer)
56
57 >>> # Construct our four components for `Attack`
58 >>> from textattack.constraints.pre_transformation import RepeatModification, StopwordModification
59 >>> from textattack.constraints.semantics import WordEmbeddingDistance
60 >>> from textattack.transformations import WordSwapEmbedding
61 >>> from textattack.search_methods import GreedyWordSwapWIR
62
63 >>> goal_function = textattack.goal_functions.UntargetedClassification(model_wrapper)
64 >>> constraints = [
65 ... RepeatModification(),
66 ... StopwordModification(),
67 ... WordEmbeddingDistance(min_cos_sim=0.9)
68 ... ]
69 >>> transformation = WordSwapEmbedding(max_candidates=50)
70 >>> search_method = GreedyWordSwapWIR(wir_method="delete")
71
72 >>> # Construct the actual attack
73 >>> attack = textattack.Attack(goal_function, constraints, transformation, search_method)
74
75 >>> input_text = "I really enjoyed the new movie that came out last month."
76 >>> label = 1 #Positive
77 >>> attack_result = attack.attack(input_text, label)
78 """
79
80 def __init__(
81 self,
82 goal_function: GoalFunction,
83 constraints: List[Union[Constraint, PreTransformationConstraint]],
84 transformation: Transformation,
85 search_method: SearchMethod,

Callers 15

buildMethod · 0.90
buildMethod · 0.90
buildMethod · 0.90
buildMethod · 0.90
buildMethod · 0.90
buildMethod · 0.90
buildMethod · 0.90
buildMethod · 0.90
buildMethod · 0.90
buildMethod · 0.90
buildMethod · 0.90
buildMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected