From be325c8f2b54c4c52d8b42d934d2495fa40efdfc Mon Sep 17 00:00:00 2001 From: jstzwj <1103870790@qq.com> Date: Mon, 8 Jul 2024 03:32:39 +0800 Subject: [PATCH] format --- textattack/attack.py | 6 ++-- textattack/attack_recipes/leap_2023.py | 5 ++- .../classification/targeted_classification.py | 2 +- textattack/search_methods/__init__.py | 2 +- .../particle_swarm_optimization_leap.py | 35 ++++++++++++++----- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/textattack/attack.py b/textattack/attack.py index 7e05f93e..47537d1b 100644 --- a/textattack/attack.py +++ b/textattack/attack.py @@ -372,9 +372,9 @@ def filter_transformations( uncached_texts.append(transformed_text) else: # promote transformed_text to the top of the LRU cache - self.constraints_cache[(current_text, transformed_text)] = ( - self.constraints_cache[(current_text, transformed_text)] - ) + self.constraints_cache[ + (current_text, transformed_text) + ] = self.constraints_cache[(current_text, transformed_text)] if self.constraints_cache[(current_text, transformed_text)]: filtered_texts.append(transformed_text) filtered_texts += self._filter_transformations_uncached( diff --git a/textattack/attack_recipes/leap_2023.py b/textattack/attack_recipes/leap_2023.py index 9149b81b..cc507628 100644 --- a/textattack/attack_recipes/leap_2023.py +++ b/textattack/attack_recipes/leap_2023.py @@ -17,6 +17,7 @@ from .attack_recipe import AttackRecipe + class LEAP2023(AttackRecipe): @staticmethod def build(model_wrapper): @@ -36,6 +37,8 @@ def build(model_wrapper): # # Perform word substitution with LEAP algorithm. # - search_method = ParticleSwarmOptimizationLEAP(pop_size=60, max_iters=20, post_turn_check=True, max_turn_retries=20) + search_method = ParticleSwarmOptimizationLEAP( + pop_size=60, max_iters=20, post_turn_check=True, max_turn_retries=20 + ) return Attack(goal_function, constraints, transformation, search_method) diff --git a/textattack/goal_functions/classification/targeted_classification.py b/textattack/goal_functions/classification/targeted_classification.py index 3b1ad3ac..6041b625 100644 --- a/textattack/goal_functions/classification/targeted_classification.py +++ b/textattack/goal_functions/classification/targeted_classification.py @@ -11,7 +11,7 @@ class TargetedClassification(ClassificationGoalFunction): """A targeted attack on classification models which attempts to maximize the score of the target label. - Complete when the arget label is the predicted label. + Complete when the target label is the predicted label. """ def __init__(self, *args, target_class=0, **kwargs): diff --git a/textattack/search_methods/__init__.py b/textattack/search_methods/__init__.py index 4338cd0f..8645c474 100644 --- a/textattack/search_methods/__init__.py +++ b/textattack/search_methods/__init__.py @@ -15,4 +15,4 @@ from .alzantot_genetic_algorithm import AlzantotGeneticAlgorithm from .improved_genetic_algorithm import ImprovedGeneticAlgorithm from .particle_swarm_optimization import ParticleSwarmOptimization -from .particle_swarm_optimization_leap import ParticleSwarmOptimizationLEAP \ No newline at end of file +from .particle_swarm_optimization_leap import ParticleSwarmOptimizationLEAP diff --git a/textattack/search_methods/particle_swarm_optimization_leap.py b/textattack/search_methods/particle_swarm_optimization_leap.py index 74b676c2..bec99114 100644 --- a/textattack/search_methods/particle_swarm_optimization_leap.py +++ b/textattack/search_methods/particle_swarm_optimization_leap.py @@ -15,14 +15,17 @@ import numpy as np from scipy.special import gamma as gamma + from textattack.goal_function_results import GoalFunctionResultStatus from textattack.search_methods import ParticleSwarmOptimization + def sigmax(alpha): numerator = gamma(alpha + 1.0) * np.sin(np.pi * alpha / 2.0) denominator = gamma((alpha + 1) / 2.0) * alpha * np.power(2.0, (alpha - 1.0) / 2.0) return np.power(numerator / denominator, 1.0 / alpha) + def vf(alpha): x = np.random.normal(0, 1) y = np.random.normal(0, 1) @@ -31,6 +34,7 @@ def vf(alpha): return x / np.power(np.abs(y), 1.0 / alpha) + def K(alpha): k = alpha * gamma((alpha + 1.0) / (2.0 * alpha)) / gamma(1.0 / alpha) k *= np.power( @@ -42,6 +46,7 @@ def K(alpha): return k + def C(alpha): x = np.array( (0.75, 0.8, 0.9, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.95, 1.99) @@ -67,6 +72,7 @@ def C(alpha): return np.interp(alpha, x, y) + def levy(alpha, gamma=1, n=1): w = 0 for i in range(0, n): @@ -81,6 +87,7 @@ def levy(alpha, gamma=1, n=1): return z + def get_one_levy(min, max): while True: temp = levy(1.5, 1) @@ -90,6 +97,7 @@ def get_one_levy(min, max): continue return temp + def softmax(x, axis=1): row_max = x.max(axis=axis) @@ -103,10 +111,11 @@ def softmax(x, axis=1): s = x_exp / x_sum return s + class ParticleSwarmOptimizationLEAP(ParticleSwarmOptimization): - """Attacks a model with word substiutitions using a variant of Particle Swarm - Optimization (PSO) algorithm called LEAP. - """ + """Attacks a model with word substiutitions using a variant of Particle + Swarm Optimization (PSO) algorithm called LEAP.""" + def _greedy_perturb(self, pop_member, original_result): best_neighbors, prob_list = self._get_best_neighbors( pop_member.result, original_result @@ -115,11 +124,11 @@ def _greedy_perturb(self, pop_member, original_result): pop_member.attacked_text = random_result.attacked_text pop_member.result = random_result return True - + def perform_search(self, initial_result): self._search_over = False population = self._initialize_population(initial_result, self.pop_size) - + # Initialize velocities v_init = [] v_init_rand = np.random.uniform(-self.v_max, self.v_max, self.pop_size) @@ -133,7 +142,10 @@ def perform_search(self, initial_result): if len(v_init_levy) == self.pop_size: break for i in range(self.pop_size): - if np.random.uniform(-self.v_max, self.v_max, ) < levy(1.5, 1): + if np.random.uniform( + -self.v_max, + self.v_max, + ) < levy(1.5, 1): v_init.append(v_init_rand[i]) else: v_init.append(v_init_levy[i]) @@ -167,9 +179,14 @@ def perform_search(self, initial_result): for i in range(self.max_iters): for k in range(len(population)): if population[k].score < fit_ave: - omega.append(self.omega_2 + ((population[k].score - fit_min) * - (self.omega_1 - self.omega_2)) / - (fit_ave - fit_min)) + omega.append( + self.omega_2 + + ( + (population[k].score - fit_min) + * (self.omega_1 - self.omega_2) + ) + / (fit_ave - fit_min) + ) else: omega.append(get_one_levy(0.5, 0.8)) C1 = self.c1_origin - i / self.max_iters * (self.c1_origin - self.c2_origin)