textattack.goal_functions package

Goal functions determine if an attack has been successful.

class textattack.goal_functions.goal_function.GoalFunction(model_wrapper, maximizable=False, use_cache=True, query_budget=inf, model_batch_size=32, model_cache_size=1048576)[source]

Bases: abc.ABC

Evaluates how well a perturbed attacked_text object is achieving a specified goal.

  • model_wrapper (ModelWrapper) – The victim model to attack.

  • maximizable (bool, optional, defaults to False) – Whether the goal function is maximizable, as opposed to a boolean result of success or failure.

  • query_budget (float, optional, defaults to float("in")) – The maximum number of model queries allowed.

  • model_cache_size (int, optional, defaults to 2**20) – The maximum number of items to keep in the model results cache at once.


Returns output for display based on the result of calling the model.

get_result(attacked_text, **kwargs)[source]

A helper method that queries self.get_results with a single AttackedText object.

get_results(attacked_text_list, check_skip=False)[source]

For each attacked_text object in attacked_text_list, returns a result consisting of whether or not the goal has been achieved, the output for display purposes, and a score.

Additionally returns whether the search is over due to the query budget.

init_attack_example(attacked_text, ground_truth_output)[source]

Called before attacking attacked_text to ‘reset’ the goal function and set properties for this example.