class_docstr: An attack generates adversarial examples on text.
  
  An attack is comprised of a goal function, constraints, transformation, and a search method. Use :meth:`attack` method to attack one sample at a time.
  
  Args:
      goal_function (:class:`~textattack.goal_functions.GoalFunction`):
          A function for determining how well a perturbation is doing at achieving the attack's goal.
      constraints (list of :class:`~textattack.constraints.Constraint` or :class:`~textattack.constraints.PreTransformationConstraint`):
          A list of constraints to add to the attack, defining which perturbations are valid.
      transformation (:class:`~textattack.transformations.Transformation`):
          The transformation applied at each step of the attack.
      search_method (:class:`~textattack.search_methods.SearchMethod`):
          The method for exploring the search space of possible perturbations
      transformation_cache_size (:obj:`int`, `optional`, defaults to :obj:`2**15`):
          The number of items to keep in the transformations cache
      constraint_cache_size (:obj:`int`, `optional`, defaults to :obj:`2**15`):
          The number of items to keep in the constraints cache
  
  Example::
  
      >>> import textattack
      >>> import transformers
  
      >>> # Load model, tokenizer, and model_wrapper
      >>> model = transformers.AutoModelForSequenceClassification.from_pretrained("textattack/bert-base-uncased-imdb")
      >>> tokenizer = transformers.AutoTokenizer.from_pretrained("textattack/bert-base-uncased-imdb")
      >>> model_wrapper = textattack.models.wrappers.HuggingFaceModelWrapper(model, tokenizer)
  
      >>> # Construct our four components for `Attack`
      >>> from textattack.constraints.pre_transformation import RepeatModification, StopwordModification
      >>> from textattack.constraints.semantics import WordEmbeddingDistance
      >>> from textattack.transformations import WordSwapEmbedding
      >>> from textattack.search_methods import GreedyWordSwapWIR
  
      >>> goal_function = textattack.goal_functions.UntargetedClassification(model_wrapper)
      >>> constraints = [
      ...     RepeatModification(),
      ...     StopwordModification(),
      ...     WordEmbeddingDistance(min_cos_sim=0.9)
      ... ]
      >>> transformation = WordSwapEmbedding(max_candidates=50)
      >>> search_method = GreedyWordSwapWIR(wir_method="delete")
  
      >>> # Construct the actual attack
      >>> attack = textattack.Attack(goal_function, constraints, transformation, search_method)
  
      >>> input_text = "I really enjoyed the new movie that came out last month."
      >>> label = 1 #Positive
      >>> attack_result = attack.attack(input_text, label)
class_docstr_tok: ["An","attack","generates","adversarial","examples","on","text",".","An","attack","is","comprised","of","a","goal","function",",","constraints",",","transformation",",","and","a","search","method",".","Use",":","meth",":","`","attack","`","method","to","attack","one","sample","at","a","time",".","Args",":","goal_function","(",":","class",":","`","~textattack.goal_functions.GoalFunction","`",")",":","A","function","for","determining","how","well","a","perturbation","is","doing","at","achieving","the","attack","'s","goal",".","constraints","(","list","of",":","class",":","`","~textattack.constraints.Constraint","`","or",":","class",":","`","~textattack.constraints.PreTransformationConstraint","`",")",":","A","list","of","constraints","to","add","to","the","attack",",","defining","which","perturbations","are","valid",".","transformation","(",":","class",":","`","~textattack.transformations.Transformation","`",")",":","The","transformation","applied","at","each","step","of","the","attack",".","search_method","(",":","class",":","`","~textattack.search_methods.SearchMethod","`",")",":","The","method","for","exploring","the","search","space","of","possible","perturbations","transformation_cache_size","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","2","*","*","15","`",")",":","The","number","of","items","to","keep","in","the","transformations","cache","constraint_cache_size","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","2","*","*","15","`",")",":","The","number","of","items","to","keep","in","the","constraints","cache","Example",":",":",">",">",">","import","textattack",">",">",">","import","transformers",">",">",">","#","Load","model",",","tokenizer",",","and","model_wrapper",">",">",">","model","=","transformers.AutoModelForSequenceClassification.from_pretrained","(","``","textattack\/bert-base-uncased-imdb","''",")",">",">",">","tokenizer","=","transformers.AutoTokenizer.from_pretrained","(","``","textattack\/bert-base-uncased-imdb","''",")",">",">",">","model_wrapper","=","textattack.models.wrappers.HuggingFaceModelWrapper","(","model",",","tokenizer",")",">",">",">","#","Construct","our","four","components","for","`","Attack","`",">",">",">","from","textattack.constraints.pre_transformation","import","RepeatModification",",","StopwordModification",">",">",">","from","textattack.constraints.semantics","import","WordEmbeddingDistance",">",">",">","from","textattack.transformations","import","WordSwapEmbedding",">",">",">","from","textattack.search_methods","import","GreedyWordSwapWIR",">",">",">","goal_function","=","textattack.goal_functions.UntargetedClassification","(","model_wrapper",")",">",">",">","constraints","=","[","...","RepeatModification","(",")",",","...","StopwordModification","(",")",",","...","WordEmbeddingDistance","(","min_cos_sim=0.9",")","...","]",">",">",">","transformation","=","WordSwapEmbedding","(","max_candidates=50",")",">",">",">","search_method","=","GreedyWordSwapWIR","(","wir_method=","''","delete","''",")",">",">",">","#","Construct","the","actual","attack",">",">",">","attack","=","textattack.Attack","(","goal_function",",","constraints",",","transformation",",","search_method",")",">",">",">","input_text","=","``","I","really","enjoyed","the","new","movie","that","came","out","last","month",".","''",">",">",">","label","=","1","#","Positive",">",">",">","attack_result","=","attack.attack","(","input_text",",","label",")"]
docstr: Initialize an attack object.
  
  Attacks can be run multiple times.
docstr_tok: ["Initialize","an","attack","object",".","Attacks","can","be","run","multiple","times","."]
code: def __init__(
      self,
      goal_function: GoalFunction,
      constraints: List[Union[Constraint, PreTransformationConstraint]],
      transformation: Transformation,
      search_method: SearchMethod,
      transformation_cache_size=2**15,
      constraint_cache_size=2**15,
  ):
      """Initialize an attack object.
  
      Attacks can be run multiple times.
      """
      assert isinstance(
          goal_function, GoalFunction
      ), f"`goal_function` must be of type `textattack.goal_functions.GoalFunction`, but got type `{type(goal_function)}`."
      assert isinstance(
          constraints, list
      ), "`constraints` must be a list of `textattack.constraints.Constraint` or `textattack.constraints.PreTransformationConstraint`."
      for c in constraints:
          assert isinstance(
              c, (Constraint, PreTransformationConstraint)
          ), "`constraints` must be a list of `textattack.constraints.Constraint` or `textattack.constraints.PreTransformationConstraint`."
      assert isinstance(
          transformation, Transformation
      ), f"`transformation` must be of type `textattack.transformations.Transformation`, but got type `{type(transformation)}`."
      assert isinstance(
          search_method, SearchMethod
      ), f"`search_method` must be of type `textattack.search_methods.SearchMethod`, but got type `{type(search_method)}`."
  
      self.goal_function = goal_function
      self.search_method = search_method
      self.transformation = transformation
      self.is_black_box = (
          getattr(transformation, "is_black_box", True)
          and search_method.is_black_box
      )
  
      if not self.search_method.check_transformation_compatibility(
          self.transformation
      ):
          raise ValueError(
              f"SearchMethod {self.search_method} incompatible with transformation {self.transformation}"
          )
  
      self.constraints = []
      self.pre_transformation_constraints = []
      for constraint in constraints:
          if isinstance(
              constraint,
              textattack.constraints.PreTransformationConstraint,
          ):
              self.pre_transformation_constraints.append(constraint)
          else:
              self.constraints.append(constraint)
  
      # Check if we can use transformation cache for our transformation.
      if not self.transformation.deterministic:
          self.use_transformation_cache = False
      elif isinstance(self.transformation, CompositeTransformation):
          self.use_transformation_cache = True
          for t in self.transformation.transformations:
              if not t.deterministic:
                  self.use_transformation_cache = False
                  break
      else:
          self.use_transformation_cache = True
      self.transformation_cache_size = transformation_cache_size
      self.transformation_cache = lru.LRU(transformation_cache_size)
  
      self.constraint_cache_size = constraint_cache_size
      self.constraints_cache = lru.LRU(constraint_cache_size)
  
      # Give search method access to functions for getting transformations and evaluating them
      self.search_method.get_transformations = self.get_transformations
      # Give search method access to self.goal_function for model query count, etc.
      self.search_method.goal_function = self.goal_function
      # The search method only needs access to the first argument. The second is only used
      # by the attack class when checking whether to skip the sample
      self.search_method.get_goal_results = (
          self.goal_function.get_results
      )
  
      # Give search method access to get indices which need to be ordered / searched
      self.search_method.get_indices_to_order = (
          self.get_indices_to_order
      )
  
      self.search_method.filter_transformations = (
          self.filter_transformations
      )
  
imports: from collections import OrderedDict
  from typing import List, Union
  import lru
  import torch
  import textattack
  from textattack.attack_results import FailedAttackResult, MaximizedAttackResult, SkippedAttackResult, SuccessfulAttackResult
  from textattack.constraints import Constraint, PreTransformationConstraint
  from textattack.goal_function_results import GoalFunctionResultStatus
  from textattack.goal_functions import GoalFunction
  from textattack.models.wrappers import ModelWrapper
  from textattack.search_methods import SearchMethod
  from textattack.shared import AttackedText, utils
  from textattack.transformations import CompositeTransformation, Transformation
label_desc: Use image node_id 1 to create a new Attack object  with example: obj = Attack(goal_function, constraints, transformation, search_method, transformation_cache_size, constraint_cache_size)