class_docstr: Generates scores (log odds) for output text explanation algorithms using Teacher Forcing technique.
  
  This class supports generation of log odds for transformer models as well as functions. In model agnostic
  cases (model is function) it expects a similarity_model and similarity_tokenizer to approximate log odd scores
  for target sentence generated by the model.
class_docstr_tok: ["Generates","scores","(","log","odds",")","for","output","text","explanation","algorithms","using","Teacher","Forcing","technique",".","This","class","supports","generation","of","log","odds","for","transformer","models","as","well","as","functions",".","In","model","agnostic","cases","(","model","is","function",")","it","expects","a","similarity_model","and","similarity_tokenizer","to","approximate","log","odd","scores","for","target","sentence","generated","by","the","model","."]
docstr: Build a teacher forcing model from the given text generation model.
  
  Parameters
  ----------
  model: object or function
      A object of any pretrained transformer model or function which is to be explained.
  
  tokenizer: object
      A tokenizer object(PreTrainedTokenizer/PreTrainedTokenizerFast) which is used to tokenize source and target sentence.
  
  similarity_model: object
      A pretrained transformer model object which is used in model agnostic scenario to approximate log odds.
  
  similarity_tokenizer: object
      A tokenizer object(PreTrainedTokenizer/PreTrainedTokenizerFast) which is used to tokenize sentence in model agnostic scenario.
  
  batch_size: int
      Batch size for model inferencing and computing logodds (default=128).
  
  device: str
      By default, it infers if system has a gpu and accordingly sets device. Should be 'cpu' or 'cuda' or pytorch models.
  
  Returns
  -------
  numpy.ndarray
      The scores (log odds) of generating target sentence ids using the model.
docstr_tok: ["Build","a","teacher","forcing","model","from","the","given","text","generation","model",".","Parameters","--","--","--","--","--","model",":","object","or","function","A","object","of","any","pretrained","transformer","model","or","function","which","is","to","be","explained",".","tokenizer",":","object","A","tokenizer","object","(","PreTrainedTokenizer\/PreTrainedTokenizerFast",")","which","is","used","to","tokenize","source","and","target","sentence",".","similarity_model",":","object","A","pretrained","transformer","model","object","which","is","used","in","model","agnostic","scenario","to","approximate","log","odds",".","similarity_tokenizer",":","object","A","tokenizer","object","(","PreTrainedTokenizer\/PreTrainedTokenizerFast",")","which","is","used","to","tokenize","sentence","in","model","agnostic","scenario",".","batch_size",":","int","Batch","size","for","model","inferencing","and","computing","logodds","(","default=128",")",".","device",":","str","By","default",",","it","infers","if","system","has","a","gpu","and","accordingly","sets","device",".","Should","be","'cpu","'","or","'cuda","'","or","pytorch","models",".","Returns","--","--","--","-","numpy.ndarray","The","scores","(","log","odds",")","of","generating","target","sentence","ids","using","the","model","."]
code: def __init__(
      self,
      model,
      tokenizer=None,
      similarity_model=None,
      similarity_tokenizer=None,
      batch_size=128,
      device=None,
  ):
      """Build a teacher forcing model from the given text generation model.
  
      Parameters
      ----------
      model: object or function
          A object of any pretrained transformer model or function which is to be explained.
  
      tokenizer: object
          A tokenizer object(PreTrainedTokenizer/PreTrainedTokenizerFast) which is used to tokenize source and target sentence.
  
      similarity_model: object
          A pretrained transformer model object which is used in model agnostic scenario to approximate log odds.
  
      similarity_tokenizer: object
          A tokenizer object(PreTrainedTokenizer/PreTrainedTokenizerFast) which is used to tokenize sentence in model agnostic scenario.
  
      batch_size: int
          Batch size for model inferencing and computing logodds (default=128).
  
      device: str
          By default, it infers if system has a gpu and accordingly sets device. Should be 'cpu' or 'cuda' or pytorch models.
  
      Returns
      -------
      numpy.ndarray
          The scores (log odds) of generating target sentence ids using the model.
      """
      super().__init__(model)
  
      self.tokenizer = tokenizer
      # set pad token if not defined
      if (
          self.tokenizer is not None
          and getattr_silent(self.tokenizer, "pad_token") is None
      ):
          self.tokenizer.pad_token = self.tokenizer.eos_token
  
      # set our working device
      self.device = device
      if self.device is None:
          if getattr(model, "device", None) is not None:
              self.device = model.device
          elif getattr(similarity_model, "device", None) is not None:
              self.device = similarity_model.device
  
      self.batch_size = batch_size
      # assign text generation function
      if safe_isinstance(
          model, "transformers.PreTrainedModel"
      ) or safe_isinstance(model, "transformers.TFPreTrainedModel"):
          self.text_generate = models.TextGeneration(
              self.inner_model,
              tokenizer=self.tokenizer,
              device=self.device,
          )
          self.similarity_model = model
          self.similarity_tokenizer = tokenizer
          self.model_agnostic = False
      else:
          self.text_generate = models.TextGeneration(
              self.inner_model, device=self.device
          )
          self.similarity_model = similarity_model
          self.similarity_tokenizer = similarity_tokenizer
          # set pad token for a similarity tokenizer(in a model agnostic scenario) if not defined
          if (
              self.similarity_tokenizer is not None
              and self.similarity_tokenizer.pad_token is None
          ):
              self.similarity_tokenizer.pad_token = (
                  self.similarity_tokenizer.eos_token
              )
          self.model_agnostic = True
      # initializing target which is the target sentence/ids for every new row of explanation
      self.output = None
      self.output_names = None
  
      self.similarity_model_type = None
      if safe_isinstance(
          self.similarity_model, "transformers.PreTrainedModel"
      ):
          self.similarity_model_type = "pt"
          if (
              self.device is not None
          ):  # = torch.device('cuda' if torch.cuda.is_available() else 'cpu') if self.device is None else self.device
              d = self.similarity_model.device
              assert (
                  d == self.device or str(d) == self.device
              ), "The passed similarity_model must be on the same device!"
              # self.similarity_model = self.similarity_model.to(self.device)
      elif safe_isinstance(
          self.similarity_model, "transformers.TFPreTrainedModel"
      ):
          self.similarity_model_type = "tf"
  
file_path: shap/shap/models/_teacher_forcing.py
imports: import numpy
  import scipy.special
  from ..None import models
  from .._serializable import Deserializer, Serializer
  from ..utils import safe_isinstance
  from ..utils.transformers import getattr_silent, parse_prefix_suffix_for_tokenizer
  from ._model import Model
label_desc: Use image node_id 1 to create a new TeacherForcing object from inherited base classes: Model with example: obj = TeacherForcing(model, tokenizer, similarity_model, similarity_tokenizer, batch_size, device)