Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
jstzwj committed Jul 7, 2024
1 parent 26f628f commit be325c8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
6 changes: 3 additions & 3 deletions textattack/attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion textattack/attack_recipes/leap_2023.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from .attack_recipe import AttackRecipe


class LEAP2023(AttackRecipe):
@staticmethod
def build(model_wrapper):
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion textattack/search_methods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
from .particle_swarm_optimization_leap import ParticleSwarmOptimizationLEAP
35 changes: 26 additions & 9 deletions textattack/search_methods/particle_swarm_optimization_leap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -90,6 +97,7 @@ def get_one_levy(min, max):
continue
return temp


def softmax(x, axis=1):
row_max = x.max(axis=axis)

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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])
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit be325c8

Please sign in to comment.