def DeepWordBugGao2018(model, use_all_transformations=True): """ Gao, Lanchantin, Soffa, Qi. Black-box Generation of Adversarial Text Sequences to Evade Deep Learning Classifiers. https://arxiv.org/abs/1801.04354 """ # # Swap characters out from words. Choose the best of four potential transformations. # if use_all_transformations: # We propose four similar methods: transformation = CompositeTransformation([ # (1) Swap: Swap two adjacent letters in the word. WordSwapNeighboringCharacterSwap(), # (2) Substitution: Substitute a letter in the word with a random letter. WordSwapRandomCharacterSubstitution(), # (3) Deletion: Delete a random letter from the word. WordSwapRandomCharacterDeletion(), # (4) Insertion: Insert a random letter in the word. WordSwapRandomCharacterInsertion(), ]) else: # We use the Combined Score and the Substitution Transformer to generate # adversarial samples, with the maximum edit distance difference of 30 # (ϵ = 30). transformation = WordSwapRandomCharacterSubstitution() # # Don't modify the same word twice or stopwords # constraints = [RepeatModification(), StopwordModification()] # # In these experiments, we hold the maximum difference # on edit distance (ϵ) to a constant 30 for each sample. # constraints.append(LevenshteinEditDistance(30)) # # Goal is untargeted classification # goal_function = UntargetedClassification(model) # # Greedily swap words with "Word Importance Ranking". # search_method = GreedyWordSwapWIR() return Attack(goal_function, constraints, transformation, search_method)
def build(model, use_all_transformations=True, ensemble: bool=False): # # Swap characters out from words. Choose the best of four potential transformations. # if use_all_transformations: # We propose four similar methods: transformation = CompositeTransformation( [ # (1) Swap: Swap two adjacent letters in the word. WordSwapNeighboringCharacterSwap(), # (2) Substitution: Substitute a letter in the word with a random letter. WordSwapRandomCharacterSubstitution(), # (3) Deletion: Delete a random letter from the word. WordSwapRandomCharacterDeletion(), # (4) Insertion: Insert a random letter in the word. WordSwapRandomCharacterInsertion(), ] ) else: # We use the Combined Score and the Substitution Transformer to generate # adversarial samples, with the maximum edit distance difference of 30 # (ϵ = 30). transformation = WordSwapRandomCharacterSubstitution() # # Don't modify the same word twice or stopwords # constraints = [RepeatModification(), StopwordModification()] # # In these experiments, we hold the maximum difference # on edit distance (ϵ) to a constant 30 for each sample. # constraints.append(LevenshteinEditDistance(30)) # # Goal is untargeted classification # goal_function = UntargetedClassification(model) # # Greedily swap words with "Word Importance Ranking". # search_method = GreedyWordSwapWIR(ensemble=ensemble) return Attack(goal_function, constraints, transformation, search_method)
def DeepWordBugGao2018(model, use_all_transformations=True): # # Swap characters out from words. Choose the best of four potential transformations. # if use_all_transformations: # We propose four similar methods: transformation = CompositeTransformation([ # (1) Swap: Swap two adjacent letters in the word. WordSwapNeighboringCharacterSwap(), # (2) Substitution: Substitute a letter in the word with a random letter. WordSwapRandomCharacterSubstitution(), # (3) Deletion: Delete a random letter from the word. WordSwapRandomCharacterDeletion(), # (4) Insertion: Insert a random letter in the word. WordSwapRandomCharacterInsertion() ]) else: # We use the Combined Score and the Substitution Transformer to generate # adversarial samples, with the maximum edit distance difference of 30 # (ϵ = 30). transformation = WordSwapRandomCharacterSubstitution() # # In these experiments, we hold the maximum difference # on edit distance (ϵ) to a constant 30 for each sample. # constraints = [LevenshteinEditDistance(30)] # # Goal is untargeted classification # goal_function = UntargetedClassification(model) # # Greedily swap words with "Word Importance Ranking". # attack = GreedyWordSwapWIR(goal_function, transformation=transformation, constraints=constraints, max_depth=None) return attack
def __init__(self, **kwargs): from textattack.transformations import CompositeTransformation from textattack.transformations import \ WordSwapNeighboringCharacterSwap, \ WordSwapRandomCharacterDeletion, WordSwapRandomCharacterInsertion, \ WordSwapRandomCharacterSubstitution, WordSwapNeighboringCharacterSwap transformation = CompositeTransformation([ # (1) Swap: Swap two adjacent letters in the word. WordSwapNeighboringCharacterSwap(), # (2) Substitution: Substitute a letter in the word with a random letter. WordSwapRandomCharacterSubstitution(), # (3) Deletion: Delete a random letter from the word. WordSwapRandomCharacterDeletion(), # (4) Insertion: Insert a random letter in the word. WordSwapRandomCharacterInsertion() ]) super().__init__(transformation, constraints=DEFAULT_CONSTRAINTS, **kwargs)