docstr: Calculates average Perplexity on all successfull attacks using a
  pre-trained small GPT-2 model.
  
  Args:
      results (``AttackResult`` objects):
          Attack results for each instance in dataset
  
  Example::
  
  
      >> import textattack
      >> import transformers
      >> model = transformers.AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
      >> tokenizer = transformers.AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
      >> model_wrapper = textattack.models.wrappers.HuggingFaceModelWrapper(model, tokenizer)
      >> attack = textattack.attack_recipes.DeepWordBugGao2018.build(model_wrapper)
      >> dataset = textattack.datasets.HuggingFaceDataset("glue", "sst2", split="train")
      >> attack_args = textattack.AttackArgs(
          num_examples=1,
          log_to_csv="log.csv",
          checkpoint_interval=5,
          checkpoint_dir="checkpoints",
          disable_stdout=True
      )
      >> attacker = textattack.Attacker(attack, dataset, attack_args)
      >> results = attacker.attack_dataset()
      >> ppl = textattack.metrics.quality_metrics.Perplexity().calculate(results)
code: def calculate(self, results):
      """Calculates average Perplexity on all successfull attacks using a
      pre-trained small GPT-2 model.
  
      Args:
          results (``AttackResult`` objects):
              Attack results for each instance in dataset
  
      Example::
  
  
          >> import textattack
          >> import transformers
          >> model = transformers.AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
          >> tokenizer = transformers.AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
          >> model_wrapper = textattack.models.wrappers.HuggingFaceModelWrapper(model, tokenizer)
          >> attack = textattack.attack_recipes.DeepWordBugGao2018.build(model_wrapper)
          >> dataset = textattack.datasets.HuggingFaceDataset("glue", "sst2", split="train")
          >> attack_args = textattack.AttackArgs(
              num_examples=1,
              log_to_csv="log.csv",
              checkpoint_interval=5,
              checkpoint_dir="checkpoints",
              disable_stdout=True
          )
          >> attacker = textattack.Attacker(attack, dataset, attack_args)
          >> results = attacker.attack_dataset()
          >> ppl = textattack.metrics.quality_metrics.Perplexity().calculate(results)
      """
      self.results = results
      self.original_candidates_ppl = []
      self.successful_candidates_ppl = []
  
      for i, result in enumerate(self.results):
          if isinstance(result, FailedAttackResult):
              continue
          elif isinstance(result, SkippedAttackResult):
              continue
          else:
              self.original_candidates.append(
                  result.original_result.attacked_text.text.lower()
              )
              self.successful_candidates.append(
                  result.perturbed_result.attacked_text.text.lower()
              )
  
      ppl_orig = self.calc_ppl(self.original_candidates)
      ppl_attack = self.calc_ppl(self.successful_candidates)
  
      self.all_metrics["avg_original_perplexity"] = round(ppl_orig, 2)
  
      self.all_metrics["avg_attack_perplexity"] = round(ppl_attack, 2)
  
      return self.all_metrics
  
file_path: TextAttack/textattack/metrics/quality_metrics/perplexity.py
imports: import torch
  from textattack.attack_results import FailedAttackResult, SkippedAttackResult
  from textattack.metrics import Metric
  import textattack.shared.utils
label_desc: Use image node_id 2 for calling the Perplexity obj's underlying member method code with example usage: obj.calculate(results) and returns: self