class_docstr: Generates scores (log odds) for the top-k tokens for Causal/Masked LM.
      
class_docstr_tok: ["Generates","scores","(","log","odds",")","for","the","top-k","tokens","for","Causal\/Masked","LM","."]
docstr: Take Causal/Masked LM model and tokenizer and build a log odds output model for the top-k tokens.
  
  Parameters
  ----------
  model: object or function
      A object of any pretrained transformer model which is to be explained.
  
  tokenizer: object
      A tokenizer object(PreTrainedTokenizer/PreTrainedTokenizerFast).
  
  generation_function_for_topk_token_ids: function
      A function which is used to generate top-k token ids. Log odds will be generated for these custom token ids.
  
  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 top-k token ids using the model.
docstr_tok: ["Take","Causal\/Masked","LM","model","and","tokenizer","and","build","a","log","odds","output","model","for","the","top-k","tokens",".","Parameters","--","--","--","--","--","model",":","object","or","function","A","object","of","any","pretrained","transformer","model","which","is","to","be","explained",".","tokenizer",":","object","A","tokenizer","object","(","PreTrainedTokenizer\/PreTrainedTokenizerFast",")",".","generation_function_for_topk_token_ids",":","function","A","function","which","is","used","to","generate","top-k","token","ids",".","Log","odds","will","be","generated","for","these","custom","token","ids",".","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","top-k","token","ids","using","the","model","."]
code: def __init__(
      self,
      model,
      tokenizer,
      k=10,
      generate_topk_token_ids=None,
      batch_size=128,
      device=None,
  ):
      """Take Causal/Masked LM model and tokenizer and build a log odds output model for the top-k tokens.
  
      Parameters
      ----------
      model: object or function
          A object of any pretrained transformer model which is to be explained.
  
      tokenizer: object
          A tokenizer object(PreTrainedTokenizer/PreTrainedTokenizerFast).
  
      generation_function_for_topk_token_ids: function
          A function which is used to generate top-k token ids. Log odds will be generated for these custom token ids.
  
      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 top-k token ids using the model.
      """
      super().__init__(model)
  
      self.tokenizer = tokenizer
      # set pad token if not defined
      if getattr_silent(self.tokenizer, "pad_token") is None:
          self.tokenizer.pad_token = self.tokenizer.eos_token
      self.k = k
      self._custom_generate_topk_token_ids = generate_topk_token_ids
      self.batch_size = batch_size
      self.device = device
  
      self.X = None
      self.topk_token_ids = None
      self.output_names = None
  
      self.model_type = None
      if safe_isinstance(
          self.inner_model, "transformers.PreTrainedModel"
      ):
          self.model_type = "pt"
          import torch
  
          self.device = (
              torch.device(
                  "cuda" if torch.cuda.is_available() else "cpu"
              )
              if self.device is None
              else self.device
          )
          self.inner_model = self.inner_model.to(self.device)
      elif safe_isinstance(
          self.inner_model, "transformers.TFPreTrainedModel"
      ):
          self.model_type = "tf"
  
file_path: shap/shap/models/_topk_lm.py
imports: import numpy
  import scipy.special
  from .._serializable import Deserializer, Serializer
  from ..utils import safe_isinstance
  from ..utils.transformers import MODELS_FOR_CAUSAL_LM, getattr_silent
  from ._model import Model
label_desc: Use image node_id 1 to create a new TopKLM object from inherited base classes: Model with example: obj = TopKLM(model, tokenizer, k, generate_topk_token_ids, batch_size, device)