class_docstr: A constraint based on the GPT-2 language model.
  
  from "Better Language Models and Their Implications"
  (openai.com/blog/better-language-models/)
  
  Args:
      model_name: id of GPT2 model
docstr: Gets the probability of the word at index `word_index` according to
  GPT-2.
  
  Assumes that all items in `text_list` have the same prefix up
  until `word_index`.
docstr_tok: ["Gets","the","probability","of","the","word","at","index","`","word_index","`","according","to","GPT-2",".","Assumes","that","all","items","in","`","text_list","`","have","the","same","prefix","up","until","`","word_index","`","."]
code: def get_log_probs_at_index(self, text_list, word_index):
      """Gets the probability of the word at index `word_index` according to
      GPT-2.
  
      Assumes that all items in `text_list` have the same prefix up
      until `word_index`.
      """
      prefix = text_list[0].text_until_word_index(word_index)
  
      if not utils.has_letter(prefix):
          # This language model perplexity is not defined with respect to
          # a word without a prefix. If the prefix is null, just return the
          # log-probability 0.0.
          return torch.zeros(len(text_list), dtype=torch.float)
  
      token_ids = self.tokenizer.encode(prefix)
      tokens_tensor = torch.tensor([token_ids])
      tokens_tensor = tokens_tensor.to(utils.device)
  
      with torch.no_grad():
          outputs = self.model(tokens_tensor)
      predictions = outputs[0]
  
      probs = []
      for attacked_text in text_list:
          next_word_ids = self.tokenizer.encode(
              attacked_text.words[word_index]
          )
          next_word_prob = predictions[0, -1, next_word_ids[0]]
          probs.append(next_word_prob)
  
      return probs
  
file_path: TextAttack/textattack/constraints/grammaticality/language_models/gpt2.py
imports: import os
  import torch
  from textattack.shared import utils
  from .language_model_constraint import LanguageModelConstraint
label_desc: Use image node_id 2 for calling the GPT2 obj's underlying member method code with example usage: obj.get_log_probs_at_index(text_list, word_index) and returns: probs, torch