class_docstr: TextTemplateInput is an implementation of InterpretableInput for text inputs, whose
  interpretable features are certain segments (e.g., words, phrases) of the text.
  It takes a template string (or function) to define the feature segmentats
  of the input text. Its input format to the model will be the completed text,
  while its interpretable representation will be a binary tensor of the number of
  the segment features whose values indicates if the feature is
  “presence” or “absence”.
  
  Args:
  
      template (str or Callable): template string or function that takes
              the text segments and format them into the text input for the model
      values (List[str] or Dict[str, str]): the values of the segments. it is
              the input to the template.
      baselines (List[str] or Dict[str, str] or Callable or None, optional): the
              baseline values for the segment features. If it is None, emptry string
              will be used as the baseline.
              Default: None
      mask (List[int] or Dict[str, int] or None, optional): the mask to group the
              segment features. It must be in the same format as the values
              and assign each segment a mask index. Segments with the same
              index will be seen as a single interpretable feature, which means
              they must be perturbed together and end with same attributions.
              Default: None
  
  Examples::
  
      >>> text_inp = TextTemplateInput(
      >>>     template="{} feels {} right now",
      >>>     values=["He", "depressed"],
      >>>     baselines=["It", "neutral"],
      >>> )
      >>>
      >>> text_inp.to_tensor()
      >>> # torch.tensor([[1, 1]])
      >>>
      >>> text_inp.to_model_input(torch.tensor([[0, 1]]))
      >>> # "It feels depressed right now"
class_docstr_tok: ["TextTemplateInput","is","an","implementation","of","InterpretableInput","for","text","inputs",",","whose","interpretable","features","are","certain","segments","(","e.g.",",","words",",","phrases",")","of","the","text",".","It","takes","a","template","string","(","or","function",")","to","define","the","feature","segmentats","of","the","input","text",".","Its","input","format","to","the","model","will","be","the","completed","text",",","while","its","interpretable","representation","will","be","a","binary","tensor","of","the","number","of","the","segment","features","whose","values","indicates","if","the","feature","is","\u201c","presence","\u201d","or","\u201c","absence","\u201d",".","Args",":","template","(","str","or","Callable",")",":","template","string","or","function","that","takes","the","text","segments","and","format","them","into","the","text","input","for","the","model","values","(","List","[","str","]","or","Dict","[","str",",","str","]",")",":","the","values","of","the","segments",".","it","is","the","input","to","the","template",".","baselines","(","List","[","str","]","or","Dict","[","str",",","str","]","or","Callable","or","None",",","optional",")",":","the","baseline","values","for","the","segment","features",".","If","it","is","None",",","emptry","string","will","be","used","as","the","baseline",".","Default",":","None","mask","(","List","[","int","]","or","Dict","[","str",",","int","]","or","None",",","optional",")",":","the","mask","to","group","the","segment","features",".","It","must","be","in","the","same","format","as","the","values","and","assign","each","segment","a","mask","index",".","Segments","with","the","same","index","will","be","seen","as","a","single","interpretable","feature",",","which","means","they","must","be","perturbed","together","and","end","with","same","attributions",".","Default",":","None","Examples",":",":",">",">",">","text_inp","=","TextTemplateInput","(",">",">",">","template=","''","{","}","feels","{","}","right","now","''",",",">",">",">","values=","[","``","He","''",",","``","depressed","''","]",",",">",">",">","baselines=","[","``","It","''",",","``","neutral","''","]",",",">",">",">",")",">",">",">",">",">",">","text_inp.to_tensor","(",")",">",">",">","#","torch.tensor","(","[","[","1",",","1","]","]",")",">",">",">",">",">",">","text_inp.to_model_input","(","torch.tensor","(","[","[","0",",","1","]","]",")",")",">",">",">","#","``","It","feels","depressed","right","now","''"]
code: def __init__(
      self,
      template: Union[str, Callable],
      values: Union[List[str], Dict[str, str]],
      baselines: Union[
          List[str], Dict[str, str], Callable, None
      ] = None,
      mask: Union[List[int], Dict[str, int], None] = None,
  ):
      # convert values dict to list
      if isinstance(values, dict):
          dict_keys = list(values.keys())
          values = [values[k] for k in dict_keys]
      else:
          assert isinstance(
              values, list
          ), f"the values must be either a list or a dict, received: {type(values)}"
          dict_keys = []
  
      self.values = values
      self.dict_keys = dict_keys
  
      n_features = len(values)
  
      if baselines is None:
          # default baseline is to remove the element
          baselines = [""] * len(values)
      elif not callable(baselines):
          if dict_keys:
              assert isinstance(baselines, dict), (
                  "if values is a dict, the baselines must also be a dict "
                  "or a callable which return a dict, "
                  f"received: {type(baselines)}"
              )
  
              # convert dict to list
              baselines = [baselines[k] for k in dict_keys]
          else:
              assert isinstance(baselines, list), (
                  "if values is a list, the baselines must also be a list "
                  "or a callable which return a list, "
                  f"received: {type(baselines)}"
              )
  
      self.baselines = baselines
  
      if mask is None:
          n_itp_features = n_features
      else:
          if self.dict_keys:
              assert isinstance(mask, dict), (
                  "if values is dict, the mask must also be a dict, "
                  f"received: {type(mask)}"
              )
  
              # convert dict to list
              mask = [mask[k] for k in self.dict_keys]
  
          mask_ids = set(mask)
          mask_id_to_idx = {mid: i for i, mid in enumerate(mask_ids)}
  
          # internal compressed mask of continuous interpretable indices from 0
          # cannot replace original mask of ids for grouping across values externally
          self.formatted_mask = [mask_id_to_idx[mid] for mid in mask]
  
          n_itp_features = len(mask_ids)
  
      # number of raw features and intepretable features
      self.n_features = n_features
      self.n_itp_features = n_itp_features
  
      if isinstance(template, str):
          template = template.format
      else:
          assert callable(template), (
              "the template must be either a string or a callable, "
              f"received: {type(template)}"
          )
          template = template
      self.format_fn = template
  
      self.mask = mask
  
file_path: captum/captum/attr/_utils/interpretable_input.py
imports: from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import torch
  from torch import Tensor
label_desc: Use image node_id 1 to create a new TextTemplateInput object from inherited base classes: InterpretableInput with example: obj = TextTemplateInput(template, values, baselines, mask)