1
0
mirror of https://github.com/QData/TextAttack.git synced 2021-10-13 00:05:06 +03:00

update scripts and textattack recipe

This commit is contained in:
uvafan
2020-05-17 16:28:58 -04:00
parent 31a045d3c4
commit 5c0584acd7
8 changed files with 58 additions and 40 deletions

View File

@@ -9,9 +9,10 @@
ArXiv, abs/1801.00554.
"""
from textattack.shared.attack import Attack
from textattack.constraints.overlap import WordsPerturbed
from textattack.constraints.grammaticality.language_models import Google1BillionWordsLanguageModel
from textattack.constraints.semantics import WordEmbeddingDistance
from textattack.constraints.semantics import WordEmbeddingDistance, RepeatModification, StopwordModification
from textattack.goal_functions import UntargetedClassification
from textattack.search_methods import GeneticAlgorithm
from textattack.transformations import WordSwapEmbedding

View File

@@ -8,9 +8,10 @@
"""
from textattack.shared.attack import Attack
from textattack.goal_functions import UntargetedClassification
from textattack.constraints.grammaticality import PartOfSpeech
from textattack.constraints.semantics import WordEmbeddingDistance
from textattack.constraints.semantics import WordEmbeddingDistance, RepeatModification, StopwordModification
from textattack.constraints.semantics.sentence_encoders import UniversalSentenceEncoder
from textattack.search_methods import GreedyWordSwapWIR
from textattack.transformations import WordSwapEmbedding
@@ -25,19 +26,26 @@ def TextFoolerJin2019(model):
# (The paper claims 0.7, but analysis of the code and some empirical
# results show that it's definitely 0.5.)
#
transformation = WordSwapEmbedding(max_candidates=50, textfooler_stopwords=True)
transformation = WordSwapEmbedding(max_candidates=50)
constraints = []
#
# Don't modify the same word twice or stopwords
#
constraints.append(
RepeatModfication(),
StopwordModification(textfooler_stopwords=True),
)
#
# Minimum word embedding cosine similarity of 0.5.
#
constraints = []
constraints.append(
WordEmbeddingDistance(min_cos_sim=0.5)
WordEmbeddingDistance(min_cos_sim=0.5)
)
#
# Only replace words with the same part of speech (or nouns with verbs)
#
constraints.append(
PartOfSpeech(allow_verb_noun_swap=True)
PartOfSpeech(allow_verb_noun_swap=True)
)
#
# Universal Sentence Encoder with a minimum angular similarity of ε = 0.7.
@@ -57,7 +65,6 @@ def TextFoolerJin2019(model):
#
# Greedily swap words with "Word Importance Ranking".
#
attack = GreedyWordSwapWIR(goal_function, transformation=transformation,
constraints=constraints, max_depth=None)
return attack
search_method = GreedyWordSwapWIR()
return Attack(goal_function, constraints, transformation, search_method)

View File

@@ -9,10 +9,14 @@ class StopwordModification(ModificationConstraint):
"""
A constraint disallowing the modification of stopwords
"""
def __init__(self, textfooler_stopwords=False):
self.textfooler_stopwords = textfooler_stopwords
def _get_modifiable_indices(self, tokenized_text):
""" Returns the word indices in x which are able to be deleted """
try:
tokenized_text.identify_stopwords(self.textfooler_stopwords)
return set(range(len(tokenized_text.words))) - tokenized_text.attack_attrs['stopword_indices']
except KeyError:
raise KeyError('`stopword_indices` in attack_attrs required for StopwordDeletion constraint.')
@@ -25,3 +29,6 @@ class StopwordModification(ModificationConstraint):
transformation: The transformation to check compatibility with.
"""
return isinstance(transformation, WordSwap)
def extra_repr_keys(self):
return ['textfooler_stopwords']

View File

@@ -2,3 +2,4 @@ from .beam_search import BeamSearch
from .greedy_word_swap import GreedyWordSwap
from .greedy_word_swap_wir import GreedyWordSwapWIR
from .genetic_algorithm import GeneticAlgorithm
from .search_method import SearchMethod

View File

@@ -2,6 +2,6 @@ from . import scripts
from . import utils
from . import validators
from .attack import Attack
from .tokenized_text import TokenizedText
from .word_embedding import WordEmbedding

View File

@@ -18,15 +18,15 @@ class Attack:
constraints that successful examples must meet.
Args:
search_method: A strategy for exploring the search space of possible perturbations
goal_function: A function for determining how well a perturbation is doing at achieving the attack's goal.
constraints: A list of constraints to add to the attack, defining which perturbations are valid.
transformation: The transformation applied at each step of the attack.
constraints: A list of constraints to add to the attack
search_method: A strategy for exploring the search space of possible perturbations
is_black_box: Whether or not the attack is black box.
"""
def __init__(self, search_method, goal_function, transformation, constraints=[], is_black_box=True):
def __init__(self, goal_function, constraints, transformation, search_method):
""" Initialize an attack object. Attacks can be run multiple times.
"""
self.search_method = search_method
@@ -40,10 +40,10 @@ class Attack:
raise NameError('Cannot instantiate attack without tokenizer')
self.transformation = transformation
self.is_black_box = True
for transformation in transformations:
if not transformation.is_black_box:
self.is_black_box = False
break
for transformation in transformations:
if not transformation.is_black_box:
self.is_black_box = False
break
if not self.search_method.check_transformation_compatibility(self.transformation):
raise ValueError('SearchMethod {self.search_method} incompatible with transformation {self.transformation}')

View File

@@ -106,6 +106,8 @@ CONSTRAINT_CLASS_NAMES = {
'infer-sent': 'textattack.constraints.semantics.sentence_encoders.InferSent',
'thought-vector': 'textattack.constraints.semantics.sentence_encoders.ThoughtVector',
'use': 'textattack.constraints.semantics.sentence_encoders.UniversalSentenceEncoder',
'repeat': 'textattack.constraints.semantics.RepeatModification',
'stopword': 'textattack.constraints.semantics.StopwordModification',.
#
# Grammaticality constraints
#
@@ -116,16 +118,16 @@ CONSTRAINT_CLASS_NAMES = {
#
# Overlap constraints
#
'bleu': 'textattack.constraints.overlap.BLEU',
'chrf': 'textattack.constraints.overlap.chrF',
'edit-distance': 'textattack.constraints.overlap.LevenshteinEditDistance',
'meteor': 'textattack.constraints.overlap.METEOR',
'words-perturbed': 'textattack.constraints.overlap.WordsPerturbed',
'bleu': 'textattack.constraints.overlap.BLEU',
'chrf': 'textattack.constraints.overlap.chrF',
'edit-distance': 'textattack.constraints.overlap.LevenshteinEditDistance',
'meteor': 'textattack.constraints.overlap.METEOR',
'max-words-perturbed': 'textattack.constraints.overlap.MaxWordsPerturbed',
}
SEARCH_CLASS_NAMES = {
'beam-search': 'textattack.search_methods.BeamSearch',
'greedy-word': 'textattack.search_methods.GreedyWordSwap',
'greedy': 'textattack.search_methods.GreedySearch',
'ga-word': 'textattack.search_methods.GeneticAlgorithm',
'greedy-word-wir': 'textattack.search_methods.GreedyWordSwapWIR',
}
@@ -200,11 +202,11 @@ def get_args():
attack_group = parser.add_mutually_exclusive_group(required=False)
search_choices = ', '.join(SEARCH_CLASS_NAMES.keys())
attack_group.add_argument('--attack', '--attack_method', type=str,
attack_group.add_argument('--search', '-s', '--search_method', type=str,
required=False, default='greedy-word-wir',
help=f'The type of attack to run. choices: {search_choices}')
help=f'The search_method to use. choices: {search_choices}')
attack_group.add_argument('--recipe', type=str, required=False, default=None,
attack_group.add_argument('--recipe', '-r', type=str, required=False, default=None,
help='full attack recipe (overrides provided goal function, transformation & constraints)',
choices=RECIPE_NAMES.keys())
@@ -287,20 +289,21 @@ def parse_goal_function_and_attack_from_args(args):
if args.recipe:
attack = parse_recipe_from_args(model, args)
goal_function = attack.goal_function
return goal_function, attack
else:
goal_function = parse_goal_function_from_args(args, model)
transformation = parse_transformation_from_args(args)
constraints = parse_constraints_from_args(args)
if ':' in args.attack:
attack_name, params = args.attack.split(':')
if attack_name not in SEARCH_CLASS_NAMES:
raise ValueError(f'Error: unsupported attack {attack_name}')
attack = eval(f'{SEARCH_CLASS_NAMES[attack_name]}(goal_function, transformation, constraints=constraints, {params})')
elif args.attack in SEARCH_CLASS_NAMES:
attack = eval(f'{SEARCH_CLASS_NAMES[args.attack]}(goal_function, transformation, constraints=constraints)')
if ':' in args.search:
search_name, params = args.search.split(':')
if search_name not in SEARCH_CLASS_NAMES:
raise ValueError(f'Error: unsupported search {search_name}')
search_method = eval(f'{SEARCH_CLASS_NAMES[search_name]}({params})')
elif args.search in SEARCH_CLASS_NAMES:
search_method = eval(f'{SEARCH_CLASS_NAMES[args.search]}()')
else:
raise ValueError(f'Error: unsupported attack {args.attack}')
return goal_function, attack
raise ValueError(f'Error: unsupported attack {args.search}')
return goal_function, textattack.shared.Attack(goal_function, constraints, transformation, search_method)
def parse_logger_from_args(args):# Create logger
attack_log_manager = textattack.loggers.AttackLogManager()

View File

@@ -32,7 +32,6 @@ class TokenizedText:
self.words = words_from_text(text, words_to_ignore=[TokenizedText.SPLIT_TOKEN])
self.text = text
self.attack_attrs = attack_attrs
self._identify_stopwords()
if 'modified_indices' not in attack_attrs:
attack_attrs['modified_indices'] = set()
@@ -42,9 +41,9 @@ class TokenizedText:
def __hash__(self):
return hash(self.text)
def _identify_stopwords(self):
def identify_stopwords(self, textfooler_stopwords=False):
self.stopwords = set(stopwords.words('english'))
if 'textfooler_stopwords' in attack_attrs:
if textfooler_stopwords:
self.stopwords = set(['a', 'about', 'above', 'across', 'after', 'afterwards', 'again', 'against', 'ain', 'all', 'almost', 'alone', 'along', 'already', 'also', 'although', 'am', 'among', 'amongst', 'an', 'and', 'another', 'any', 'anyhow', 'anyone', 'anything', 'anyway', 'anywhere', 'are', 'aren', "aren't", 'around', 'as', 'at', 'back', 'been', 'before', 'beforehand', 'behind', 'being', 'below', 'beside', 'besides', 'between', 'beyond', 'both', 'but', 'by', 'can', 'cannot', 'could', 'couldn', "couldn't", 'd', 'didn', "didn't", 'doesn', "doesn't", 'don', "don't", 'down', 'due', 'during', 'either', 'else', 'elsewhere', 'empty', 'enough', 'even', 'ever', 'everyone', 'everything', 'everywhere', 'except', 'first', 'for', 'former', 'formerly', 'from', 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'he', 'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', 'hereupon', 'hers', 'herself', 'him', 'himself', 'his', 'how', 'however', 'hundred', 'i', 'if', 'in', 'indeed', 'into', 'is', 'isn', "isn't", 'it', "it's", 'its', 'itself', 'just', 'latter', 'latterly', 'least', 'll', 'may', 'me', 'meanwhile', 'mightn', "mightn't", 'mine', 'more', 'moreover', 'most', 'mostly', 'must', 'mustn', "mustn't", 'my', 'myself', 'namely', 'needn', "needn't", 'neither', 'never', 'nevertheless', 'next', 'no', 'nobody', 'none', 'noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'o', 'of', 'off', 'on', 'once', 'one', 'only', 'onto', 'or', 'other', 'others', 'otherwise', 'our', 'ours', 'ourselves', 'out', 'over', 'per', 'please','s', 'same', 'shan', "shan't", 'she', "she's", "should've", 'shouldn', "shouldn't", 'somehow', 'something', 'sometime', 'somewhere', 'such', 't', 'than', 'that', "that'll", 'the', 'their', 'theirs', 'them', 'themselves', 'then', 'thence', 'there', 'thereafter', 'thereby', 'therefore', 'therein', 'thereupon', 'these', 'they','this', 'those', 'through', 'throughout', 'thru', 'thus', 'to', 'too','toward', 'towards', 'under', 'unless', 'until', 'up', 'upon', 'used', 've', 'was', 'wasn', "wasn't", 'we', 'were', 'weren', "weren't", 'what', 'whatever', 'when', 'whence', 'whenever', 'where', 'whereafter', 'whereas', 'whereby', 'wherein', 'whereupon', 'wherever', 'whether', 'which', 'while', 'whither', 'who', 'whoever', 'whole', 'whom', 'whose', 'why', 'with', 'within', 'without', 'won', "won't", 'would', 'wouldn', "wouldn't", 'y', 'yet', 'you', "you'd", "you'll", "you're", "you've", 'your', 'yours', 'yourself', 'yourselves'])
attack_attrs['stopword_indices'] = set()