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: This function performs model inference for tensorflow and pytorch models.
  
  Parameters
  ----------
  inputs: dict
      Dictionary of padded source sentence ids and attention mask as tensors.
  
  output_ids: numpy.ndarray
      An array of decoder output ids.
  
  Returns
  -------
  numpy.ndarray
      Returns output logits from the model.
docstr_tok: ["This","function","performs","model","inference","for","tensorflow","and","pytorch","models",".","Parameters","--","--","--","--","--","inputs",":","dict","Dictionary","of","padded","source","sentence","ids","and","attention","mask","as","tensors",".","output_ids",":","numpy.ndarray","An","array","of","decoder","output","ids",".","Returns","--","--","--","-","numpy.ndarray","Returns","output","logits","from","the","model","."]
code: def model_inference(self, inputs, output_ids):
      """This function performs model inference for tensorflow and pytorch models.
  
      Parameters
      ----------
      inputs: dict
          Dictionary of padded source sentence ids and attention mask as tensors.
  
      output_ids: numpy.ndarray
          An array of decoder output ids.
  
      Returns
      -------
      numpy.ndarray
          Returns output logits from the model.
      """
      if self.similarity_model_type == "pt":
          import torch
  
          # create torch tensors and move to device
          if self.device is not None:
              inputs = inputs.to(self.device)
          output_ids = torch.tensor(
              output_ids, dtype=torch.int64, device=self.device
          )
          self.similarity_model.eval()
          with torch.no_grad():
              if self.similarity_model.config.is_encoder_decoder:
                  # model inference
                  outputs = self.similarity_model(
                      **inputs,
                      decoder_input_ids=output_ids,
                      labels=output_ids,
                      return_dict=True,
                  )
              else:
                  # combine source and target sentence ids to pass into decoder eg: in case of distillgpt2
                  inputs["input_ids"] = torch.cat(
                      (inputs["input_ids"], output_ids), dim=-1
                  )
                  attention_mask_for_output_ids = torch.ones(
                      output_ids.shape,
                      dtype=output_ids.dtype,
                      device=self.device,
                  )
                  inputs["attention_mask"] = torch.cat(
                      (
                          inputs["attention_mask"],
                          attention_mask_for_output_ids,
                      ),
                      dim=-1,
                  )
                  # create position ids due to left padding for decoder models
                  inputs["position_ids"] = (
                      inputs["attention_mask"].long().cumsum(-1) - 1
                  )
                  inputs["position_ids"].masked_fill_(
                      inputs["attention_mask"] == 0, 0
                  )
                  # model inference
                  outputs = self.similarity_model(
                      **inputs, return_dict=True
                  )
              logits = (
                  outputs.logits.detach()
                  .cpu()
                  .numpy()
                  .astype("float64")
              )
      elif self.similarity_model_type == "tf":
          import tensorflow as tf
  
          output_ids = tf.convert_to_tensor(output_ids, dtype=tf.int32)
          if self.similarity_model.config.is_encoder_decoder:
              if self.device is None:
                  outputs = self.similarity_model(
                      inputs,
                      decoder_input_ids=output_ids,
                      labels=output_ids,
                      return_dict=True,
                  )
              else:
                  try:
                      with tf.device(self.device):
                          outputs = self.similarity_model(
                              inputs,
                              decoder_input_ids=output_ids,
                              labels=output_ids,
                              return_dict=True,
                          )
                  except RuntimeError as err:
                      print(err)
          else:
              # combine source and target sentence ids to pass into decoder eg: in case of distillgpt2
              inputs["input_ids"] = tf.concat(
                  (inputs["input_ids"], output_ids), axis=-1
              )
              attention_mask_for_output_ids = tf.ones(
                  output_ids.shape, dtype=output_ids.dtype
              )
              inputs["attention_mask"] = tf.concat(
                  (
                      inputs["attention_mask"],
                      attention_mask_for_output_ids,
                  ),
                  axis=-1,
              )
              inputs["position_ids"] = (
                  tf.math.cumsum(inputs["attention_mask"], axis=-1) - 1
              )
              inputs["position_ids"] = tf.where(
                  inputs["attention_mask"] == 0,
                  0,
                  inputs["position_ids"],
              )
              if self.device is None:
                  outputs = self.similarity_model(
                      inputs, return_dict=True
                  )
              else:
                  try:
                      with tf.device(self.device):
                          outputs = self.similarity_model(
                              inputs, return_dict=True
                          )
                  except RuntimeError as err:
                      print(err)
          logits = outputs.logits.numpy().astype("float64")
      return logits
  
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 8 for calling the TeacherForcing obj's underlying member method code with example usage: obj.model_inference(inputs, output_ids) and returns: logits