docstr: An implementation of the integrated gradients method for `tensorflow` models.
  
  For details of the method see the original paper: https://arxiv.org/abs/1703.01365 .
  
  Parameters
  ----------
  model
      `tensorflow` model.
  layer
      A layer or a function having as parameter the model and returning a layer with respect to which the
      gradients are calculated. If not provided, the gradients are calculated with respect to the input.
      To guarantee saving and loading of the explainer, the layer has to be specified as a callable which
      returns a layer given the model. E.g. ``lambda model: model.layers[0].embeddings``.
  target_fn
      A scalar function that is applied to the predictions of the model.
      This can be used to specify which scalar output the attributions should be calculated for.
      This can be particularly useful if the desired output is not known before calling the model
      (e.g. explaining the `argmax` output for a probabilistic classifier, in this case we could pass
      ``target_fn=partial(np.argmax, axis=1)``).
  method
      Method for the integral approximation. Methods available:
      ``"riemann_left"``, ``"riemann_right"``, ``"riemann_middle"``, ``"riemann_trapezoid"``, ``"gausslegendre"``.
  n_steps
      Number of step in the path integral approximation from the baseline to the input instance.
  internal_batch_size
      Batch size for the internal batching.
docstr_tok: ["An","implementation","of","the","integrated","gradients","method","for","`","tensorflow","`","models",".","For","details","of","the","method","see","the","original","paper",":","https",":","\/\/arxiv.org\/abs\/1703.01365",".","Parameters","--","--","--","--","--","model","`","tensorflow","`","model",".","layer","A","layer","or","a","function","having","as","parameter","the","model","and","returning","a","layer","with","respect","to","which","the","gradients","are","calculated",".","If","not","provided",",","the","gradients","are","calculated","with","respect","to","the","input",".","To","guarantee","saving","and","loading","of","the","explainer",",","the","layer","has","to","be","specified","as","a","callable","which","returns","a","layer","given","the","model",".","E.g",".","``","lambda","model",":","model.layers","[","0","]",".embeddings","``",".","target_fn","A","scalar","function","that","is","applied","to","the","predictions","of","the","model",".","This","can","be","used","to","specify","which","scalar","output","the","attributions","should","be","calculated","for",".","This","can","be","particularly","useful","if","the","desired","output","is","not","known","before","calling","the","model","(","e.g",".","explaining","the","`","argmax","`","output","for","a","probabilistic","classifier",",","in","this","case","we","could","pass","``","target_fn=partial","(","np.argmax",",","axis=1",")","``",")",".","method","Method","for","the","integral","approximation",".","Methods","available",":","``","``","riemann_left","''","``",",","``","``","riemann_right","''","``",",","``","``","riemann_middle","''","``",",","``","``","riemann_trapezoid","''","``",",","``","``","gausslegendre","''","``",".","n_steps","Number","of","step","in","the","path","integral","approximation","from","the","baseline","to","the","input","instance",".","internal_batch_size","Batch","size","for","the","internal","batching","."]
code: def __init__(
      self,
      model: tf.keras.Model,
      layer: Optional[
          Union[
              Callable[[tf.keras.Model], tf.keras.layers.Layer],
              tf.keras.layers.Layer,
          ]
      ] = None,
      target_fn: Optional[Callable] = None,
      method: str = "gausslegendre",
      n_steps: int = 50,
      internal_batch_size: int = 100,
  ) -> None:
      """
      An implementation of the integrated gradients method for `tensorflow` models.
  
      For details of the method see the original paper: https://arxiv.org/abs/1703.01365 .
  
      Parameters
      ----------
      model
          `tensorflow` model.
      layer
          A layer or a function having as parameter the model and returning a layer with respect to which the
          gradients are calculated. If not provided, the gradients are calculated with respect to the input.
          To guarantee saving and loading of the explainer, the layer has to be specified as a callable which
          returns a layer given the model. E.g. ``lambda model: model.layers[0].embeddings``.
      target_fn
          A scalar function that is applied to the predictions of the model.
          This can be used to specify which scalar output the attributions should be calculated for.
          This can be particularly useful if the desired output is not known before calling the model
          (e.g. explaining the `argmax` output for a probabilistic classifier, in this case we could pass
          ``target_fn=partial(np.argmax, axis=1)``).
      method
          Method for the integral approximation. Methods available:
          ``"riemann_left"``, ``"riemann_right"``, ``"riemann_middle"``, ``"riemann_trapezoid"``, ``"gausslegendre"``.
      n_steps
          Number of step in the path integral approximation from the baseline to the input instance.
      internal_batch_size
          Batch size for the internal batching.
      """
  
      super().__init__(meta=copy.deepcopy(DEFAULT_META_INTGRAD))
      params = locals()
      remove = ["self", "model", "__class__", "layer"]
      params = {k: v for k, v in params.items() if k not in remove}
      self.model = model
  
      if self.model.inputs is None:
          self._has_inputs = False
      else:
          self._has_inputs = True
  
      if layer is None:
          self.orig_call: Optional[Callable] = None
          self.layer = None
          layer_meta: Union[int, str] = LayerState.UNSPECIFIED.value
  
      elif isinstance(layer, tf.keras.layers.Layer):
          self.orig_call = layer.call
          self.layer = layer
  
          try:
              layer_meta = model.layers.index(layer)
          except ValueError:
              layer_meta = LayerState.NON_SERIALIZABLE.value
              logger.warning(
                  "Layer not in the list of `model.layers`. Passing the layer directly would not "
                  "permit the serialization of the explainer. This is due to nested layers. To permit "
                  "the serialization of the explainer, provide the layer as a callable which returns "
                  "the layer given the model."
              )
  
      elif callable(layer):
          self.layer = layer(self.model)
          self.orig_call = self.layer.call
          self.callable_layer = layer
          layer_meta = LayerState.CALLABLE.value
  
      else:
          raise TypeError(
              f"Unsupported layer type. Received {type(layer)}."
          )
  
      params["layer"] = layer_meta
      self.meta["params"].update(params)
      self.n_steps = n_steps
      self.method = method
      self.internal_batch_size = internal_batch_size
  
      self._is_list: Optional[bool] = None
      self._is_np: Optional[bool] = None
      self.orig_dummy_input: Optional[Union[list, np.ndarray]] = None
  
      self.target_fn = target_fn
  
imports: import copy
  import logging
  import string
  import warnings
  from enum import Enum
  from typing import Callable, List, Optional, Tuple, Union, cast
  import numpy
  import tensorflow
  from alibi.api.defaults import DEFAULT_DATA_INTGRAD, DEFAULT_META_INTGRAD
  from alibi.api.interfaces import Explainer, Explanation
  from alibi.utils.approximation_methods import approximation_parameters
label_desc: Use image node_id 1 to create a new IntegratedGradients object from inherited base classes: Explainer with example: obj = IntegratedGradients(model, layer, target_fn, method, n_steps, internal_batch_size)