mirror of
https://github.com/QData/TextAttack.git
synced 2021-10-13 00:05:06 +03:00
add iga docs + update tests
This commit is contained in:
@@ -106,6 +106,7 @@ Attacks on classification tasks, like sentiment classification and entailment:
|
||||
- **faster-alzantot**: modified, faster version of the Alzantot et al. genetic algorithm, from (["Certified Robustness to Adversarial Word Substitutions" (Jia et al., 2019)](https://arxiv.org/abs/1909.00986)).
|
||||
- **deepwordbug**: Greedy replace-1 scoring and multi-transformation character-swap attack (["Black-box Generation of Adversarial Text Sequences to Evade Deep Learning Classifiers" (Gao et al., 2018)](https://arxiv.org/abs/1801.04354)).
|
||||
- **hotflip**: Beam search and gradient-based word swap (["HotFlip: White-Box Adversarial Examples for Text Classification" (Ebrahimi et al., 2017)](https://arxiv.org/abs/1712.06751)).
|
||||
- **iga**: Improved genetic algorithm attack from (["Natural Language Adversarial Attacks and Defenses in Word Level (Wang et al., 2019)"](https://arxiv.org/abs/1909.06723)
|
||||
- **input-reduction**: Reducing the input while maintaining the prediction through word importance ranking (["Pathologies of Neural Models Make Interpretation Difficult" (Feng et al., 2018)](https://arxiv.org/pdf/1804.07781.pdf)).
|
||||
- **kuleshov**: Greedy search and counterfitted embedding swap (["Adversarial Examples for Natural Language Classification Problems" (Kuleshov et al., 2018)](https://openreview.net/pdf?id=r1QZ3zbAZ)).
|
||||
- **pso**: Particle swarm optimization and HowNet synonym swap (["Word-level Textual Adversarial Attacking as Combinatorial Optimization" (Zang et al., 2020)](https://www.aclweb.org/anthology/2020.acl-main.540/)).
|
||||
|
||||
1
augment_test.csv
Normal file
1
augment_test.csv
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
|
|
@@ -41,8 +41,15 @@ HotFlip (HotFlip: White-Box Adversarial Examples for Text Classification)
|
||||
.. automodule:: textattack.attack_recipes.hotflip_ebrahimi_2017
|
||||
:members:
|
||||
|
||||
Input Reduction
|
||||
################
|
||||
Improved Genetic Algorithm (Natural Language Adversarial Attacks and Defenses in Word Level)
|
||||
#################################################################################################
|
||||
|
||||
.. automodule:: textattack.attack_recipes.iga_wang_2019
|
||||
:members:
|
||||
|
||||
|
||||
Input Reduction (Pathologies of Neural Models Make Interpretations Difficult)
|
||||
####################################################################################
|
||||
|
||||
.. automodule:: textattack.attack_recipes.input_reduction_feng_2018
|
||||
:members:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Attack(
|
||||
/.*/Attack(
|
||||
(search_method): GreedyWordSwapWIR(
|
||||
(wir_method): unk
|
||||
)
|
||||
@@ -41,6 +41,9 @@ Running in interactive mode
|
||||
----------------------------
|
||||
Enter a sentence to attack or "q" to quit:
|
||||
Attacking...
|
||||
t1 / <AttackedText "All that glitters is not gold">
|
||||
t2 / <AttackedText "Any that shinning is not gold">
|
||||
t1.num_words / 6 t2.num_words / 6
|
||||
[92m1 (96%)[0m --> [91m0 (66%)[0m
|
||||
|
||||
[92mAll[0m that [92mglitters[0m is not gold
|
||||
|
||||
@@ -26,8 +26,11 @@
|
||||
(4): StopwordModification
|
||||
(is_black_box): True
|
||||
)
|
||||
/.*/
|
||||
|
||||
--------------------------------------------- Result 1 ---------------------------------------------
|
||||
t1 / <AttackedText "it 's a charming and often affecting journey . ">
|
||||
t2 / <AttackedText "it 's a loveable and ordinarily affecting journey . ">
|
||||
t1.num_words / 8 t2.num_words / 8
|
||||
[92mPositive (100%)[0m --> [91mNegative (69%)[0m
|
||||
|
||||
it 's a [92mcharming[0m and [92moften[0m affecting journey .
|
||||
@@ -36,6 +39,9 @@ it 's a [91mloveable[0m and [91mordinarily[0m affecting journey .
|
||||
|
||||
|
||||
--------------------------------------------- Result 2 ---------------------------------------------
|
||||
t1 / <AttackedText "unflinchingly bleak and desperate ">
|
||||
t2 / <AttackedText "unflinchingly bleak and desperation ">
|
||||
t1.num_words / 4 t2.num_words / 4
|
||||
[91mNegative (83%)[0m --> [92mPositive (90%)[0m
|
||||
|
||||
unflinchingly bleak and [91mdesperate[0m
|
||||
|
||||
@@ -3,7 +3,20 @@
|
||||
(wir_method): unk
|
||||
)
|
||||
(goal_function): UntargetedClassification
|
||||
(transformation): CompositeTransformation
|
||||
(transformation): CompositeTransformation(
|
||||
(0): WordSwapNeighboringCharacterSwap(
|
||||
(random_one): True
|
||||
)
|
||||
(1): WordSwapRandomCharacterSubstitution(
|
||||
(random_one): True
|
||||
)
|
||||
(2): WordSwapRandomCharacterDeletion(
|
||||
(random_one): True
|
||||
)
|
||||
(3): WordSwapRandomCharacterInsertion(
|
||||
(random_one): True
|
||||
)
|
||||
)
|
||||
(constraints):
|
||||
(0): LevenshteinEditDistance(
|
||||
(max_edit_distance): 30
|
||||
@@ -13,8 +26,11 @@
|
||||
(2): StopwordModification
|
||||
(is_black_box): True
|
||||
)
|
||||
/.*/
|
||||
|
||||
--------------------------------------------- Result 1 ---------------------------------------------
|
||||
t1 / <AttackedText "lovingly photographed in the manner of a golden book sprung to life , stuart little 2 manages sweetness largely without stickiness .">
|
||||
t2 / <AttackedText "lovingly photographed in the manner of a golden book sprung to ife , stuart little 2 manager seetness largely without stickiness .">
|
||||
t1.num_words / 20 t2.num_words / 20
|
||||
[92mPositive (91%)[0m --> [91mNegative (69%)[0m
|
||||
|
||||
lovingly photographed in the manner of a golden book sprung to [92mlife[0m , stuart little 2 [92mmanages[0m [92msweetness[0m largely without stickiness .
|
||||
@@ -23,6 +39,9 @@ lovingly photographed in the manner of a golden book sprung to [91mife[0m , st
|
||||
|
||||
|
||||
--------------------------------------------- Result 2 ---------------------------------------------
|
||||
t1 / <AttackedText "consistently clever and suspenseful .">
|
||||
t2 / <AttackedText "conisstently celver and Huspenseful .">
|
||||
t1.num_words / 4 t2.num_words / 4
|
||||
[92mPositive (99%)[0m --> [91mNegative (82%)[0m
|
||||
|
||||
[92mconsistently[0m [92mclever[0m and [92msuspenseful[0m .
|
||||
|
||||
@@ -30,8 +30,11 @@
|
||||
(4): StopwordModification
|
||||
(is_black_box): True
|
||||
)
|
||||
/.*/
|
||||
|
||||
--------------------------------------------- Result 1 ---------------------------------------------
|
||||
t1 / <AttackedText "this kind of hands-on storytelling is ultimately what makes shanghai ghetto move beyond a good , dry , reliable textbook and what allows it to rank with its worthy predecessors .">
|
||||
t2 / <AttackedText "this kind of hands-on storytelling is ultimately what makes shanghai ghetto move beyond a good , dry , reliable manuals and what allows it to rank with its creditable predecessors .">
|
||||
t1.num_words / 28 t2.num_words / 28
|
||||
[92mPositive (73%)[0m --> [91mNegative (68%)[0m
|
||||
|
||||
this kind of hands-on storytelling is ultimately what makes shanghai ghetto move beyond a good , dry , reliable [92mtextbook[0m and what allows it to rank with its [92mworthy[0m predecessors .
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
/.*/Attack(
|
||||
/.*/2020-07-09 09:51:14,589 loading file /u/jm8wx/.flair/models/en-pos-ontonotes-fast-v0.4.pt
|
||||
Attack(
|
||||
(search_method): GreedyWordSwapWIR(
|
||||
(wir_method): unk
|
||||
)
|
||||
@@ -24,8 +25,11 @@
|
||||
(3): StopwordModification
|
||||
(is_black_box): True
|
||||
)
|
||||
/.*/
|
||||
|
||||
--------------------------------------------- Result 1 ---------------------------------------------
|
||||
t1 / <AttackedText "exposing the ways we fool ourselves is one hour photo's real strength .">
|
||||
t2 / <AttackedText "exposing the ways we fool ourselves is one stopwatch photo's real kraft .">
|
||||
t1.num_words / 12 t2.num_words / 12
|
||||
[92mPositive (100%)[0m --> [91mNegative (98%)[0m
|
||||
|
||||
exposing the ways we fool ourselves is one [92mhour[0m photo's real [92mstrength[0m .
|
||||
@@ -34,6 +38,9 @@ exposing the ways we fool ourselves is one [91mstopwatch[0m photo's real [91m
|
||||
|
||||
|
||||
--------------------------------------------- Result 2 ---------------------------------------------
|
||||
t1 / <AttackedText "it's up to you to decide whether to admire these people's dedication to their cause or be repelled by their dogmatism , manipulativeness and narrow , fearful view of american life .">
|
||||
t2 / <AttackedText "it's up to you to decide whether to admire these people's dedication to their cause or be rescheduled by their dogmatism , manipulativeness and narrow , shitless view of american life .">
|
||||
t1.num_words / 29 t2.num_words / 29
|
||||
[92mPositive (96%)[0m --> [91mNegative (99%)[0m
|
||||
|
||||
it's up to you to decide whether to admire these people's dedication to their cause or be [92mrepelled[0m by their dogmatism , manipulativeness and narrow , [92mfearful[0m view of american life .
|
||||
@@ -42,6 +49,9 @@ it's up to you to decide whether to admire these people's dedication to their ca
|
||||
|
||||
|
||||
--------------------------------------------- Result 3 ---------------------------------------------
|
||||
t1 / <AttackedText "mostly , [goldbacher] just lets her complicated characters be unruly , confusing and , through it all , human .">
|
||||
t2 / <AttackedText "mostly , [goldbacher] just lets her complicated characters be haphazard , confusing and , through it all , humanistic .">
|
||||
t1.num_words / 15 t2.num_words / 15
|
||||
[92mPositive (100%)[0m --> [91mNegative (96%)[0m
|
||||
|
||||
mostly , [goldbacher] just lets her complicated characters be [92munruly[0m , confusing and , through it all , [92mhuman[0m .
|
||||
@@ -50,6 +60,9 @@ mostly , [goldbacher] just lets her complicated characters be [91mhaphazard[0m
|
||||
|
||||
|
||||
--------------------------------------------- Result 4 ---------------------------------------------
|
||||
t1 / <AttackedText ". . . quite good at providing some good old fashioned spooks .">
|
||||
t2 / <AttackedText ". . . rather good at provision some good old fashioned bugging .">
|
||||
t1.num_words / 9 t2.num_words / 9
|
||||
[92mPositive (99%)[0m --> [91mNegative (90%)[0m
|
||||
|
||||
. . . [92mquite[0m good at [92mproviding[0m some good old fashioned [92mspooks[0m .
|
||||
|
||||
@@ -3,12 +3,17 @@
|
||||
(wir_method): unk
|
||||
)
|
||||
(goal_function): UntargetedClassification
|
||||
(transformation): WordSwapRandomCharacterSubstitution
|
||||
(transformation): WordSwapRandomCharacterSubstitution(
|
||||
(random_one): True
|
||||
)
|
||||
(constraints): None
|
||||
(is_black_box): True
|
||||
)
|
||||
/.*/
|
||||
|
||||
--------------------------------------------- Result 1 ---------------------------------------------
|
||||
t1 / <AttackedText "I was surprised how much I enjoyed this. Sure it is a bit slow moving in parts, but what else would one expect from Rollin? Also there is plenty of nudity, nothing wrong with that, particularly as it includes lots of the gorgeous, Brigitte Lahaie. There are also some spectacularly eroticised female dead, bit more dodgey, perhaps, but most effective. There is also a sci-fi like storyline with a brief explanation at the end, but I wouldn't bother too much with that. No, here we have a most interesting exploration of memory and the effect of memory loss and to just what extent one is still 'alive' without memory. My DVD sleeve mentions David Cronenberg and whilst this is perhaps not quite as good as his best films, there is some similarity here, particularly with the great use of seemingly menacing architecture and the effective and creepy use of inside space. As I have tried to indicate this is by no means a rip roaring thriller, it is a captivating, nightmare like movie that makes the very most of its locations, including a stunning railway setting at the end.">
|
||||
t2 / <AttackedText "I was surprised how much I enjoyed this. Sure it is a bct slow moving in parts, but what else would one expect from Rollin? Also there is plenty of nudity, nothing wrong with that, particularly as it includes lots of the gorgeous, Brigitte Lahaie. There are also some spectacularly eroticised female dead, bit more dodgey, perhaps, but most effective. There is also a sci-fi like storyline with a brief explanation at the end, but I wouldn't bother too much with that. No, here we have a most interesting exploration of memory and the effect of memory loss and to just what extent one is still 'alive' without memory. My DVD sleeve mentions David Cronenberg and whilst this is perhaps not quite as good as his best films, there is some similarity here, particularly with the great use of seemingly menacing architecture and the effective and creepy use of inside space. As I have tried to indicate this is by no means a rip roaring thriller, it is a captivating, nightmare like movie that makes the very most of its locations, including a stunning railway setting at the end.">
|
||||
t1.num_words / 189 t2.num_words / 189
|
||||
[92mPositive (75%)[0m --> [91mNegative (71%)[0m
|
||||
|
||||
I was surprised how much I enjoyed this. Sure it is a [92mbit[0m slow moving in parts, but what else would one expect from Rollin? Also there is plenty of nudity, nothing wrong with that, particularly as it includes lots of the gorgeous, Brigitte Lahaie. There are also some spectacularly eroticised female dead, bit more dodgey, perhaps, but most effective. There is also a sci-fi like storyline with a brief explanation at the end, but I wouldn't bother too much with that. No, here we have a most interesting exploration of memory and the effect of memory loss and to just what extent one is still 'alive' without memory. My DVD sleeve mentions David Cronenberg and whilst this is perhaps not quite as good as his best films, there is some similarity here, particularly with the great use of seemingly menacing architecture and the effective and creepy use of inside space. As I have tried to indicate this is by no means a rip roaring thriller, it is a captivating, nightmare like movie that makes the very most of its locations, including a stunning railway setting at the end.
|
||||
@@ -17,6 +22,9 @@ I was surprised how much I enjoyed this. Sure it is a [91mbct[0m slow moving i
|
||||
|
||||
|
||||
--------------------------------------------- Result 2 ---------------------------------------------
|
||||
t1 / <AttackedText "I went into "Night of the Hunted" not knowing what to expect at all. I was really impressed.<br /><br />It is essentially a mystery/thriller where this girl who can't remember anything gets 'rescued' by a guy who happens to be driving past. The two become fast friends and lovers and together, they try to figure out what is going on with her. Through some vague flashbacks and grim memories, they eventually get to the bottom of it and the ending is pretty cool.<br /><br />I really liked the setting of this one: a desolate, post-modern Paris is the backdrop with lots of gray skies and tall buildings. Very metropolitan. Groovy soundtrack and lots of nudity.<br /><br />Surprising it was made in 1980; seems somewhat ahead of it's time.<br /><br />8 out of 10, kids.">
|
||||
t2 / <AttackedText "I went into "Night of the Hunted" not knowing what to expect at all. I was really impressed.<br /><br />It is essentially a mystery/thriller where this girl who can't remember anything gets 'rescued' by a guy who happens to be driving past. The two become fast friends and lovers and together, they try to figure out what is going on with her. Through some vague flashbacks and grEm memories, they eventually get to the bottom of it and the ending is pretty cool.<br /><br />I really liked the setting of this one: a desolate, post-modern Paris is the backdrop with lots of gray skies and tall buildings. Very metropolitan. Groovy soundtrack and lots of nudity.<br /><br />Surprising it was made in 1980; seems somewhat ahead of it's time.<br /><br />8 out of 10, kids.">
|
||||
t1.num_words / 139 t2.num_words / 139
|
||||
[92mPositive (69%)[0m --> [91mNegative (53%)[0m
|
||||
|
||||
I went into "Night of the Hunted" not knowing what to expect at all. I was really impressed.<br /><br />It is essentially a mystery/thriller where this girl who can't remember anything gets 'rescued' by a guy who happens to be driving past. The two become fast friends and lovers and together, they try to figure out what is going on with her. Through some vague flashbacks and [92mgrim[0m memories, they eventually get to the bottom of it and the ending is pretty cool.<br /><br />I really liked the setting of this one: a desolate, post-modern Paris is the backdrop with lots of gray skies and tall buildings. Very metropolitan. Groovy soundtrack and lots of nudity.<br /><br />Surprising it was made in 1980; seems somewhat ahead of it's time.<br /><br />8 out of 10, kids.
|
||||
|
||||
@@ -28,8 +28,11 @@
|
||||
(4): StopwordModification
|
||||
(is_black_box): False
|
||||
)
|
||||
/.*/
|
||||
|
||||
--------------------------------------------- Result 1 ---------------------------------------------
|
||||
t1 / <AttackedText "the story gives ample opportunity for large-scale action and suspense , which director shekhar kapur supplies with tremendous skill .">
|
||||
t2 / <AttackedText "the story gives ample opportunity for large-scale action and suspense , which director shekhar kapur stagnated with tremendous skill .">
|
||||
t1.num_words / 18 t2.num_words / 18
|
||||
[92mPositive (96%)[0m --> [91mNegative (77%)[0m
|
||||
|
||||
the story gives ample opportunity for large-scale action and suspense , which director shekhar kapur [92msupplies[0m with tremendous skill .
|
||||
|
||||
@@ -15,7 +15,12 @@
|
||||
(2): StopwordModification
|
||||
(is_black_box): True
|
||||
)
|
||||
/.*/
|
||||
|
||||
t1 / <AttackedText "The new rights are nice enough
|
||||
Everyone really likes the newest benefits ">
|
||||
t2 / <AttackedText "The new rights are nice enough
|
||||
Everyone really likes the newest benefits ">
|
||||
t1.num_words / 12 t2.num_words / 12
|
||||
--------------------------------------------- Result 1 ---------------------------------------------
|
||||
[92mEntailment (99%)[0m --> [37m[SKIPPED][0m
|
||||
|
||||
@@ -23,7 +28,17 @@
|
||||
[1m[4mHypothesis[0m[0m: Everyone really likes the newest benefits
|
||||
|
||||
|
||||
t1 / <AttackedText "This site includes a list of all award winners and a searchable database of Government Executive articles.
|
||||
The Government Executive articles housed on the website are not able to be searched.">
|
||||
t2 / <AttackedText "This site includes a list of all award winners and a searchable database of Government Executive articles.
|
||||
The Government Executive articles housed on the website are not able-bodied to be searched.">
|
||||
t1.num_words / 31 t2.num_words / 31
|
||||
--------------------------------------------- Result 2 ---------------------------------------------
|
||||
t1 / <AttackedText "This site includes a list of all award winners and a searchable database of Government Executive articles.
|
||||
The Government Executive articles housed on the website are not able to be searched.">
|
||||
t2 / <AttackedText "This site includes a list of all award winners and a searchable database of Government Executive articles.
|
||||
The Government Executive articles housed on the website are not able-bodied to be searched.">
|
||||
t1.num_words / 31 t2.num_words / 31
|
||||
[37mNeutral (100%)[0m --> [92mEntailment (56%)[0m
|
||||
|
||||
[1m[4mPremise[0m[0m: This site includes a list of all award winners and a searchable database of Government Executive articles.
|
||||
@@ -33,7 +48,17 @@
|
||||
[1m[4mHypothesis[0m[0m: The Government Executive articles housed on the website are not [92mable-bodied[0m to be searched.
|
||||
|
||||
|
||||
t1 / <AttackedText "uh i don't know i i have mixed emotions about him uh sometimes i like him but at the same times i love to see somebody beat him
|
||||
I like him for the most part, but would still enjoy seeing someone beat him.">
|
||||
t2 / <AttackedText "uh i don't know i i have mixed emotions about him uh sometimes i like him but at the same times i love to see somebody beat him
|
||||
I like him for the most office, but would still enjoy seeing someone beat him.">
|
||||
t1.num_words / 43 t2.num_words / 43
|
||||
--------------------------------------------- Result 3 ---------------------------------------------
|
||||
t1 / <AttackedText "uh i don't know i i have mixed emotions about him uh sometimes i like him but at the same times i love to see somebody beat him
|
||||
I like him for the most part, but would still enjoy seeing someone beat him.">
|
||||
t2 / <AttackedText "uh i don't know i i have mixed emotions about him uh sometimes i like him but at the same times i love to see somebody beat him
|
||||
I like him for the most office, but would still enjoy seeing someone beat him.">
|
||||
t1.num_words / 43 t2.num_words / 43
|
||||
[91mContradiction (99%)[0m --> [92mEntailment (100%)[0m
|
||||
|
||||
[1m[4mPremise[0m[0m: uh i don't know i i have mixed emotions about him uh sometimes i like him but at the same times i love to see somebody beat him
|
||||
|
||||
@@ -3,7 +3,20 @@
|
||||
(wir_method): unk
|
||||
)
|
||||
(goal_function): UntargetedClassification
|
||||
(transformation): CompositeTransformation
|
||||
(transformation): CompositeTransformation(
|
||||
(0): WordSwapNeighboringCharacterSwap(
|
||||
(random_one): True
|
||||
)
|
||||
(1): WordSwapRandomCharacterSubstitution(
|
||||
(random_one): True
|
||||
)
|
||||
(2): WordSwapRandomCharacterDeletion(
|
||||
(random_one): True
|
||||
)
|
||||
(3): WordSwapRandomCharacterInsertion(
|
||||
(random_one): True
|
||||
)
|
||||
)
|
||||
(constraints):
|
||||
(0): LevenshteinEditDistance(
|
||||
(max_edit_distance): 30
|
||||
@@ -13,8 +26,11 @@
|
||||
(2): StopwordModification
|
||||
(is_black_box): True
|
||||
)
|
||||
/.*/
|
||||
|
||||
--------------------------------------------- Result 1 ---------------------------------------------
|
||||
t1 / <AttackedText "hide new secretions from the parental units ">
|
||||
t2 / <AttackedText "Ehide enw secretions from the parental units ">
|
||||
t1.num_words / 7 t2.num_words / 7
|
||||
[91mNegative (100%)[0m --> [92mPositive (71%)[0m
|
||||
|
||||
[91mhide[0m [91mnew[0m secretions from the parental units
|
||||
@@ -29,6 +45,9 @@ contains no wit , only labored gags
|
||||
|
||||
|
||||
--------------------------------------------- Result 3 ---------------------------------------------
|
||||
t1 / <AttackedText "that loves its characters and communicates something rather beautiful about human nature ">
|
||||
t2 / <AttackedText "that lodes its characters and communicates somethNng rathrer beautifdul about human nature ">
|
||||
t1.num_words / 12 t2.num_words / 12
|
||||
[92mPositive (100%)[0m --> [91mNegative (96%)[0m
|
||||
|
||||
that [92mloves[0m its characters and communicates [92msomething[0m [92mrather[0m [92mbeautiful[0m about human nature
|
||||
|
||||
@@ -11,4 +11,4 @@ from .textbugger_li_2018 import TextBuggerLi2018
|
||||
from .textfooler_jin_2019 import TextFoolerJin2019
|
||||
from .pwws_ren_2019 import PWWSRen2019
|
||||
from .pruthi_2019 import Pruthi2019
|
||||
from .PSO_zang_2020 import PSOZang2020
|
||||
from .pso_zang_2020 import PSOZang2020
|
||||
|
||||
@@ -4,7 +4,7 @@ from textattack.constraints.pre_transformation import (
|
||||
StopwordModification,
|
||||
)
|
||||
from textattack.goal_functions import UntargetedClassification
|
||||
from textattack.search_methods import PSOAlgorithm
|
||||
from textattack.search_methods import ParticleSwarmOptimization
|
||||
from textattack.shared.attack import Attack
|
||||
from textattack.transformations import WordSwapEmbedding, WordSwapHowNet
|
||||
|
||||
@@ -51,6 +51,6 @@ def PSOZang2020(model):
|
||||
#
|
||||
# Perform word substitution with a Particle Swarm Optimization (PSO) algorithm.
|
||||
#
|
||||
search_method = PSOAlgorithm(pop_size=60, max_iters=20)
|
||||
search_method = ParticleSwarmOptimization(pop_size=60, max_iters=20)
|
||||
|
||||
return Attack(goal_function, constraints, transformation, search_method)
|
||||
@@ -1,8 +1,10 @@
|
||||
from abc import ABC
|
||||
|
||||
from textattack.goal_function_results import GoalFunctionResult
|
||||
from textattack.shared import utils
|
||||
|
||||
|
||||
class AttackResult:
|
||||
class AttackResult(ABC):
|
||||
"""
|
||||
Result of an Attack run on a single (output, text_input) pair.
|
||||
|
||||
@@ -88,7 +90,10 @@ class AttackResult:
|
||||
|
||||
i1 = 0
|
||||
i2 = 0
|
||||
|
||||
print("t1 /", t1)
|
||||
print("t2 /", t2)
|
||||
print("t1.num_words /", t1.num_words, "t2.num_words /", t2.num_words)
|
||||
sp = None
|
||||
while i1 < t1.num_words or i2 < t2.num_words:
|
||||
# show deletions
|
||||
while (
|
||||
|
||||
@@ -1,21 +1,16 @@
|
||||
from abc import ABC, abstractmethod
|
||||
import pickle
|
||||
import random
|
||||
|
||||
from textattack.shared import utils
|
||||
|
||||
|
||||
class TextAttackDataset:
|
||||
class TextAttackDataset(ABC):
|
||||
"""
|
||||
Any iterable of (label, text_input) pairs qualifies as
|
||||
a ``TextAttackDataset``.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Loads a full dataset from disk.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
@@ -8,7 +10,7 @@ class GoalFunctionResultStatus:
|
||||
SKIPPED = 3
|
||||
|
||||
|
||||
class GoalFunctionResult:
|
||||
class GoalFunctionResult(ABC):
|
||||
"""
|
||||
Represents the result of a goal function evaluating a AttackedText object.
|
||||
|
||||
@@ -45,18 +47,21 @@ class GoalFunctionResult:
|
||||
if isinstance(self.score, torch.Tensor):
|
||||
self.score = self.score.item()
|
||||
|
||||
@abstractmethod
|
||||
def get_text_color_input(self):
|
||||
""" A string representing the color this result's changed
|
||||
portion should be if it represents the original input.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
@abstractmethod
|
||||
def get_text_color_perturbed(self):
|
||||
""" A string representing the color this result's changed
|
||||
portion should be if it represents the perturbed input.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
@abstractmethod
|
||||
def get_colored_output(self, color_method=None):
|
||||
""" Returns a string representation of this result's output, colored
|
||||
according to `color_method`.
|
||||
|
||||
@@ -41,4 +41,7 @@ class InputReduction(ClassificationGoalFunction):
|
||||
return min(num_words_score + model_score / initial_num_words, 1)
|
||||
|
||||
def extra_repr_keys(self):
|
||||
return ["target_num_words"]
|
||||
if self.maximizable:
|
||||
return ["maximizable"]
|
||||
else:
|
||||
return ["maximizable", "target_num_words"]
|
||||
|
||||
@@ -25,4 +25,7 @@ class TargetedClassification(ClassificationGoalFunction):
|
||||
return model_output[self.target_class]
|
||||
|
||||
def extra_repr_keys(self):
|
||||
return ["target_class"]
|
||||
if self.maximizable:
|
||||
return ["maximizable", "target_class"]
|
||||
else:
|
||||
return ["target_class"]
|
||||
|
||||
@@ -202,8 +202,11 @@ class GoalFunction(ABC):
|
||||
return all_outputs
|
||||
|
||||
def extra_repr_keys(self):
|
||||
attrs = []
|
||||
if self.query_budget < float("inf"):
|
||||
return ["query_budget"]
|
||||
return []
|
||||
attrs.append("query_budget")
|
||||
if self.maximizable:
|
||||
attrs.append("maximizable")
|
||||
return attrs
|
||||
|
||||
__repr__ = __str__ = default_class_repr
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
class Logger:
|
||||
from abc import ABC
|
||||
|
||||
|
||||
class Logger(ABC):
|
||||
""" An abstract class for different methods of logging attack results.
|
||||
"""
|
||||
|
||||
|
||||
@@ -3,4 +3,4 @@ from .beam_search import BeamSearch
|
||||
from .greedy_search import GreedySearch
|
||||
from .greedy_word_swap_wir import GreedyWordSwapWIR
|
||||
from .genetic_algorithm import GeneticAlgorithm
|
||||
from .PSO_algorithm import PSOAlgorithm
|
||||
from .particle_swarm_optimization import ParticleSwarmOptimization
|
||||
|
||||
@@ -13,7 +13,7 @@ from textattack.goal_function_results import GoalFunctionResultStatus
|
||||
from textattack.search_methods import SearchMethod
|
||||
|
||||
|
||||
class PSOAlgorithm(SearchMethod):
|
||||
class ParticleSwarmOptimization(SearchMethod):
|
||||
"""
|
||||
Attacks a model with word substiutitions using a Particle Swarm Optimization (PSO) algorithm.
|
||||
Some key hyper-parameters are setup according to the original paper:
|
||||
@@ -1,5 +1,6 @@
|
||||
import numpy as np
|
||||
|
||||
from textattack.shared import utils
|
||||
from textattack.transformations.transformation import Transformation
|
||||
|
||||
|
||||
@@ -34,3 +35,14 @@ class CompositeTransformation(Transformation):
|
||||
for transformation in self.transformations:
|
||||
new_attacked_texts.update(transformation(*args, **kwargs))
|
||||
return list(new_attacked_texts)
|
||||
|
||||
def __repr__(self):
|
||||
main_str = "CompositeTransformation" + "("
|
||||
transformation_lines = []
|
||||
for i, transformation in enumerate(self.transformations):
|
||||
transformation_lines.append(utils.add_indent(f"({i}): {transformation}", 2))
|
||||
transformation_lines.append(")")
|
||||
main_str += utils.add_indent("\n" + "\n".join(transformation_lines), 2)
|
||||
return main_str
|
||||
|
||||
__str__ = __repr__
|
||||
|
||||
@@ -48,3 +48,6 @@ class WordSwapRandomCharacterInsertion(WordSwap):
|
||||
candidate_words.append(candidate_word)
|
||||
|
||||
return candidate_words
|
||||
|
||||
def extra_repr_keys(self):
|
||||
return super().extra_repr_keys() + ["random_one"]
|
||||
|
||||
@@ -36,3 +36,6 @@ class WordSwapRandomCharacterSubstitution(WordSwap):
|
||||
candidate_words.append(candidate_word)
|
||||
|
||||
return candidate_words
|
||||
|
||||
def extra_repr_keys(self):
|
||||
return super().extra_repr_keys() + ["random_one"]
|
||||
|
||||
Reference in New Issue
Block a user