class_docstr: Lime is an interpretability method that trains an interpretable surrogate model
  by sampling points around a specified input example and using model evaluations
  at these points to train a simpler interpretable 'surrogate' model, such as a
  linear model.
  
  Lime provides a more specific implementation than LimeBase in order to expose
  a consistent API with other perturbation-based algorithms. For more general
  use of the LIME framework, consider using the LimeBase class directly and
  defining custom sampling and transformation to / from interpretable
  representation functions.
  
  Lime assumes that the interpretable representation is a binary vector,
  corresponding to some elements in the input being set to their baseline value
  if the corresponding binary interpretable feature value is 0 or being set
  to the original input value if the corresponding binary interpretable
  feature value is 1. Input values can be grouped to correspond to the same
  binary interpretable feature using a feature mask provided when calling
  attribute, similar to other perturbation-based attribution methods.
  
  One example of this setting is when applying Lime to an image classifier.
  Pixels in an image can be grouped into super-pixels or segments, which
  correspond to interpretable features, provided as a feature_mask when
  calling attribute. Sampled binary vectors convey whether a super-pixel
  is on (retains the original input values) or off (set to the corresponding
  baseline value, e.g. black image). An interpretable linear model is trained
  with input being the binary vectors and outputs as the corresponding scores
  of the image classifier with the appropriate super-pixels masked based on the
  binary vector. Coefficients of the trained surrogate
  linear model convey the importance of each super-pixel.
  
  More details regarding LIME can be found in the original paper:
  https://arxiv.org/abs/1602.04938
docstr: Args:
  
  
      forward_func (Callable): The forward function of the model or any
              modification of it
      interpretable_model (Model, optional): Model object to train
              interpretable model.
  
              This argument is optional and defaults to SkLearnLasso(alpha=0.01),
              which is a wrapper around the Lasso linear model in SkLearn.
              This requires having sklearn version >= 0.23 available.
  
              Other predefined interpretable linear models are provided in
              captum._utils.models.linear_model.
  
              Alternatively, a custom model object must provide a `fit` method to
              train the model, given a dataloader, with batches containing
              three tensors:
  
              - interpretable_inputs: Tensor
                [2D num_samples x num_interp_features],
              - expected_outputs: Tensor [1D num_samples],
              - weights: Tensor [1D num_samples]
  
              The model object must also provide a `representation` method to
              access the appropriate coefficients or representation of the
              interpretable model after fitting.
  
              Note that calling fit multiple times should retrain the
              interpretable model, each attribution call reuses
              the same given interpretable model object.
      similarity_func (Callable, optional): Function which takes a single sample
              along with its corresponding interpretable representation
              and returns the weight of the interpretable sample for
              training the interpretable model.
              This is often referred to as a similarity kernel.
  
              This argument is optional and defaults to a function which
              applies an exponential kernel to the cosine distance between
              the original input and perturbed input, with a kernel width
              of 1.0.
  
              A similarity function applying an exponential
              kernel to cosine / euclidean distances can be constructed
              using the provided get_exp_kernel_similarity_function in
              captum.attr._core.lime.
  
              Alternately, a custom callable can also be provided.
              The expected signature of this callable is:
  
              >>> def similarity_func(
              >>>    original_input: Tensor or tuple[Tensor, ...],
              >>>    perturbed_input: Tensor or tuple[Tensor, ...],
              >>>    perturbed_interpretable_input:
              >>>        Tensor [2D 1 x num_interp_features],
              >>>    **kwargs: Any
              >>> ) -> float or Tensor containing float scalar
  
              perturbed_input and original_input will be the same type and
              contain tensors of the same shape, with original_input
              being the same as the input provided when calling attribute.
  
              kwargs includes baselines, feature_mask, num_interp_features
              (integer, determined from feature mask).
      perturb_func (Callable, optional): Function which returns a single
              sampled input, which is a binary vector of length
              num_interp_features, or a generator of such tensors.
  
              This function is optional, the default function returns
              a binary vector where each element is selected
              independently and uniformly at random. Custom
              logic for selecting sampled binary vectors can
              be implemented by providing a function with the
              following expected signature:
  
              >>> perturb_func(
              >>>    original_input: Tensor or tuple[Tensor, ...],
              >>>    **kwargs: Any
              >>> ) -> Tensor [Binary 2D Tensor 1 x num_interp_features]
              >>>  or generator yielding such tensors
  
              kwargs includes baselines, feature_mask, num_interp_features
              (integer, determined from feature mask).
docstr_tok: ["Args",":","forward_func","(","Callable",")",":","The","forward","function","of","the","model","or","any","modification","of","it","interpretable_model","(","Model",",","optional",")",":","Model","object","to","train","interpretable","model",".","This","argument","is","optional","and","defaults","to","SkLearnLasso","(","alpha=0.01",")",",","which","is","a","wrapper","around","the","Lasso","linear","model","in","SkLearn",".","This","requires","having","sklearn","version",">","=","0.23","available",".","Other","predefined","interpretable","linear","models","are","provided","in","captum._utils.models.linear_model",".","Alternatively",",","a","custom","model","object","must","provide","a","`","fit","`","method","to","train","the","model",",","given","a","dataloader",",","with","batches","containing","three","tensors",":","-","interpretable_inputs",":","Tensor","[","2D","num_samples","x","num_interp_features","]",",","-","expected_outputs",":","Tensor","[","1D","num_samples","]",",","-","weights",":","Tensor","[","1D","num_samples","]","The","model","object","must","also","provide","a","`","representation","`","method","to","access","the","appropriate","coefficients","or","representation","of","the","interpretable","model","after","fitting",".","Note","that","calling","fit","multiple","times","should","retrain","the","interpretable","model",",","each","attribution","call","reuses","the","same","given","interpretable","model","object",".","similarity_func","(","Callable",",","optional",")",":","Function","which","takes","a","single","sample","along","with","its","corresponding","interpretable","representation","and","returns","the","weight","of","the","interpretable","sample","for","training","the","interpretable","model",".","This","is","often","referred","to","as","a","similarity","kernel",".","This","argument","is","optional","and","defaults","to","a","function","which","applies","an","exponential","kernel","to","the","cosine","distance","between","the","original","input","and","perturbed","input",",","with","a","kernel","width","of","1.0",".","A","similarity","function","applying","an","exponential","kernel","to","cosine","\/","euclidean","distances","can","be","constructed","using","the","provided","get_exp_kernel_similarity_function","in","captum.attr._core.lime",".","Alternately",",","a","custom","callable","can","also","be","provided",".","The","expected","signature","of","this","callable","is",":",">",">",">","def","similarity_func","(",">",">",">","original_input",":","Tensor","or","tuple","[","Tensor",",","...","]",",",">",">",">","perturbed_input",":","Tensor","or","tuple","[","Tensor",",","...","]",",",">",">",">","perturbed_interpretable_input",":",">",">",">","Tensor","[","2D","1","x","num_interp_features","]",",",">",">",">","*","*","kwargs",":","Any",">",">",">",")","-",">","float","or","Tensor","containing","float","scalar","perturbed_input","and","original_input","will","be","the","same","type","and","contain","tensors","of","the","same","shape",",","with","original_input","being","the","same","as","the","input","provided","when","calling","attribute",".","kwargs","includes","baselines",",","feature_mask",",","num_interp_features","(","integer",",","determined","from","feature","mask",")",".","perturb_func","(","Callable",",","optional",")",":","Function","which","returns","a","single","sampled","input",",","which","is","a","binary","vector","of","length","num_interp_features",",","or","a","generator","of","such","tensors",".","This","function","is","optional",",","the","default","function","returns","a","binary","vector","where","each","element","is","selected","independently","and","uniformly","at","random",".","Custom","logic","for","selecting","sampled","binary","vectors","can","be","implemented","by","providing","a","function","with","the","following","expected","signature",":",">",">",">","perturb_func","(",">",">",">","original_input",":","Tensor","or","tuple","[","Tensor",",","...","]",",",">",">",">","*","*","kwargs",":","Any",">",">",">",")","-",">","Tensor","[","Binary","2D","Tensor","1","x","num_interp_features","]",">",">",">","or","generator","yielding","such","tensors","kwargs","includes","baselines",",","feature_mask",",","num_interp_features","(","integer",",","determined","from","feature","mask",")","."]
code: def __init__(
      self,
      forward_func: Callable,
      interpretable_model: Optional[Model] = None,
      similarity_func: Optional[Callable] = None,
      perturb_func: Optional[Callable] = None,
  ) -> None:
      r"""
  
      Args:
  
  
          forward_func (Callable): The forward function of the model or any
                  modification of it
          interpretable_model (Model, optional): Model object to train
                  interpretable model.
  
                  This argument is optional and defaults to SkLearnLasso(alpha=0.01),
                  which is a wrapper around the Lasso linear model in SkLearn.
                  This requires having sklearn version >= 0.23 available.
  
                  Other predefined interpretable linear models are provided in
                  captum._utils.models.linear_model.
  
                  Alternatively, a custom model object must provide a `fit` method to
                  train the model, given a dataloader, with batches containing
                  three tensors:
  
                  - interpretable_inputs: Tensor
                    [2D num_samples x num_interp_features],
                  - expected_outputs: Tensor [1D num_samples],
                  - weights: Tensor [1D num_samples]
  
                  The model object must also provide a `representation` method to
                  access the appropriate coefficients or representation of the
                  interpretable model after fitting.
  
                  Note that calling fit multiple times should retrain the
                  interpretable model, each attribution call reuses
                  the same given interpretable model object.
          similarity_func (Callable, optional): Function which takes a single sample
                  along with its corresponding interpretable representation
                  and returns the weight of the interpretable sample for
                  training the interpretable model.
                  This is often referred to as a similarity kernel.
  
                  This argument is optional and defaults to a function which
                  applies an exponential kernel to the cosine distance between
                  the original input and perturbed input, with a kernel width
                  of 1.0.
  
                  A similarity function applying an exponential
                  kernel to cosine / euclidean distances can be constructed
                  using the provided get_exp_kernel_similarity_function in
                  captum.attr._core.lime.
  
                  Alternately, a custom callable can also be provided.
                  The expected signature of this callable is:
  
                  >>> def similarity_func(
                  >>>    original_input: Tensor or tuple[Tensor, ...],
                  >>>    perturbed_input: Tensor or tuple[Tensor, ...],
                  >>>    perturbed_interpretable_input:
                  >>>        Tensor [2D 1 x num_interp_features],
                  >>>    **kwargs: Any
                  >>> ) -> float or Tensor containing float scalar
  
                  perturbed_input and original_input will be the same type and
                  contain tensors of the same shape, with original_input
                  being the same as the input provided when calling attribute.
  
                  kwargs includes baselines, feature_mask, num_interp_features
                  (integer, determined from feature mask).
          perturb_func (Callable, optional): Function which returns a single
                  sampled input, which is a binary vector of length
                  num_interp_features, or a generator of such tensors.
  
                  This function is optional, the default function returns
                  a binary vector where each element is selected
                  independently and uniformly at random. Custom
                  logic for selecting sampled binary vectors can
                  be implemented by providing a function with the
                  following expected signature:
  
                  >>> perturb_func(
                  >>>    original_input: Tensor or tuple[Tensor, ...],
                  >>>    **kwargs: Any
                  >>> ) -> Tensor [Binary 2D Tensor 1 x num_interp_features]
                  >>>  or generator yielding such tensors
  
                  kwargs includes baselines, feature_mask, num_interp_features
                  (integer, determined from feature mask).
  
      """
      if interpretable_model is None:
          interpretable_model = SkLearnLasso(alpha=0.01)
  
      if similarity_func is None:
          similarity_func = get_exp_kernel_similarity_function()
  
      if perturb_func is None:
          perturb_func = default_perturb_func
  
      LimeBase.__init__(
          self,
          forward_func,
          interpretable_model,
          similarity_func,
          perturb_func,
          True,
          default_from_interp_rep_transform,
          None,
      )
  
imports: import inspect
  import math
  import typing
  import warnings
  from typing import Any, Callable, cast, List, Optional, Tuple, Union
  import torch
  from captum._utils.common import _expand_additional_forward_args, _expand_target, _flatten_tensor_or_tuple, _format_output, _format_tensor_into_tuples, _get_max_feature_index, _is_tuple, _reduce_list, _run_forward
  from captum._utils.models.linear_model import SkLearnLasso
  from captum._utils.models.model import Model
  from captum._utils.progress import progress
  from captum._utils.typing import BaselineType, Literal, TargetType, TensorOrTupleOfTensorsGeneric
  from captum.attr._utils.attribution import PerturbationAttribution
  from captum.attr._utils.batching import _batch_example_iterator
  from captum.attr._utils.common import _construct_default_feature_mask, _format_input_baseline
  from captum.log import log_usage
  from torch import Tensor
  from torch.nn import CosineSimilarity
  from torch.utils.data import DataLoader, TensorDataset
label_desc: Use image node_id 1 to create a new Lime object from inherited base classes: LimeBase with example: obj = Lime(forward_func, interpretable_model, similarity_func, perturb_func)