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

Class Attacker

textattack/attacker.py:30–511  ·  view source on GitHub ↗

Class for running attacks on a dataset with specified parameters. This class uses the :class:`~textattack.Attack` to actually run the attacks, while also providing useful features such as parallel processing, saving/resuming from a checkpint, logging to files and stdout. Args:

Source from the content-addressed store, hash-verified

28
29
30class Attacker:
31 """Class for running attacks on a dataset with specified parameters. This
32 class uses the :class:`~textattack.Attack` to actually run the attacks,
33 while also providing useful features such as parallel processing,
34 saving/resuming from a checkpint, logging to files and stdout.
35
36 Args:
37 attack (:class:`~textattack.Attack`):
38 :class:`~textattack.Attack` used to actually carry out the attack.
39 dataset (:class:`~textattack.datasets.Dataset`):
40 Dataset to attack.
41 attack_args (:class:`~textattack.AttackArgs`):
42 Arguments for attacking the dataset. For default settings, look at the `AttackArgs` class.
43
44 Example::
45
46 >>> import textattack
47 >>> import transformers
48
49 >>> model = transformers.AutoModelForSequenceClassification.from_pretrained("textattack/bert-base-uncased-imdb")
50 >>> tokenizer = transformers.AutoTokenizer.from_pretrained("textattack/bert-base-uncased-imdb")
51 >>> model_wrapper = textattack.models.wrappers.HuggingFaceModelWrapper(model, tokenizer)
52
53 >>> attack = textattack.attack_recipes.TextFoolerJin2019.build(model_wrapper)
54 >>> dataset = textattack.datasets.HuggingFaceDataset("imdb", split="test")
55
56 >>> # Attack 20 samples with CSV logging and checkpoint saved every 5 interval
57 >>> attack_args = textattack.AttackArgs(
58 ... num_examples=20,
59 ... log_to_csv="log.csv",
60 ... checkpoint_interval=5,
61 ... checkpoint_dir="checkpoints",
62 ... disable_stdout=True
63 ... )
64
65 >>> attacker = textattack.Attacker(attack, dataset, attack_args)
66 >>> attacker.attack_dataset()
67 """
68
69 def __init__(self, attack, dataset, attack_args=None):
70 assert isinstance(
71 attack, Attack
72 ), f"`attack` argument must be of type `textattack.Attack`, but got type of `{type(attack)}`."
73 assert isinstance(
74 dataset, textattack.datasets.Dataset
75 ), f"`dataset` must be of type `textattack.datasets.Dataset`, but got type `{type(dataset)}`."
76
77 if attack_args:
78 assert isinstance(
79 attack_args, AttackArgs
80 ), f"`attack_args` must be of type `textattack.AttackArgs`, but got type `{type(attack_args)}`."
81 else:
82 attack_args = AttackArgs()
83
84 self.attack = attack
85 self.dataset = dataset
86 self.attack_args = attack_args
87 self.attack_log_manager = None

Callers 6

runMethod · 0.90
test_useFunction · 0.90
test_metric_recipeFunction · 0.90

Calls

no outgoing calls

Tested by 2

test_useFunction · 0.72
test_metric_recipeFunction · 0.72