class_docstr: An explainer class for instance-level explanations of Graph Neural
  Networks.
  
  Args:
      model (torch.nn.Module): The model to explain.
      algorithm (ExplainerAlgorithm): The explanation algorithm.
      explanation_type (ExplanationType or str): The type of explanation to
          compute. The possible values are:
  
              - :obj:`"model"`: Explains the model prediction.
  
              - :obj:`"phenomenon"`: Explains the phenomenon that the model
                is trying to predict.
  
          In practice, this means that the explanation algorithm will either
          compute their losses with respect to the model output
          (:obj:`"model"`) or the target output (:obj:`"phenomenon"`).
      model_config (ModelConfig): The model configuration.
          See :class:`~torch_geometric.explain.config.ModelConfig` for
          available options. (default: :obj:`None`)
      node_mask_type (MaskType or str, optional): The type of mask to apply
          on nodes. The possible values are (default: :obj:`None`):
  
              - :obj:`None`: Will not apply any mask on nodes.
  
              - :obj:`"object"`: Will mask each node.
  
              - :obj:`"common_attributes"`: Will mask each feature.
  
              - :obj:`"attributes"`: Will mask each feature across all nodes.
  
      edge_mask_type (MaskType or str, optional): The type of mask to apply
          on edges. Has the sample possible values as :obj:`node_mask_type`.
          (default: :obj:`None`)
      threshold_config (ThresholdConfig, optional): The threshold
          configuration.
          See :class:`~torch_geometric.explain.config.ThresholdConfig` for
          available options. (default: :obj:`None`)
class_docstr_tok: ["An","explainer","class","for","instance-level","explanations","of","Graph","Neural","Networks",".","Args",":","model","(","torch.nn.Module",")",":","The","model","to","explain",".","algorithm","(","ExplainerAlgorithm",")",":","The","explanation","algorithm",".","explanation_type","(","ExplanationType","or","str",")",":","The","type","of","explanation","to","compute",".","The","possible","values","are",":","-",":","obj",":","`","``","model","''","`",":","Explains","the","model","prediction",".","-",":","obj",":","`","``","phenomenon","''","`",":","Explains","the","phenomenon","that","the","model","is","trying","to","predict",".","In","practice",",","this","means","that","the","explanation","algorithm","will","either","compute","their","losses","with","respect","to","the","model","output","(",":","obj",":","`","``","model","''","`",")","or","the","target","output","(",":","obj",":","`","``","phenomenon","''","`",")",".","model_config","(","ModelConfig",")",":","The","model","configuration",".","See",":","class",":","`","~torch_geometric.explain.config.ModelConfig","`","for","available","options",".","(","default",":",":","obj",":","`","None","`",")","node_mask_type","(","MaskType","or","str",",","optional",")",":","The","type","of","mask","to","apply","on","nodes",".","The","possible","values","are","(","default",":",":","obj",":","`","None","`",")",":","-",":","obj",":","`","None","`",":","Will","not","apply","any","mask","on","nodes",".","-",":","obj",":","`","``","object","''","`",":","Will","mask","each","node",".","-",":","obj",":","`","``","common_attributes","''","`",":","Will","mask","each","feature",".","-",":","obj",":","`","``","attributes","''","`",":","Will","mask","each","feature","across","all","nodes",".","edge_mask_type","(","MaskType","or","str",",","optional",")",":","The","type","of","mask","to","apply","on","edges",".","Has","the","sample","possible","values","as",":","obj",":","`","node_mask_type","`",".","(","default",":",":","obj",":","`","None","`",")","threshold_config","(","ThresholdConfig",",","optional",")",":","The","threshold","configuration",".","See",":","class",":","`","~torch_geometric.explain.config.ThresholdConfig","`","for","available","options",".","(","default",":",":","obj",":","`","None","`",")"]
docstr: Computes the explanation of the GNN for the given inputs and
  target.
  
  .. note::
  
      If you get an error message like "Trying to backward through the
      graph a second time", make sure that the target you provided
      was computed with :meth:`torch.no_grad`.
  
  Args:
      x (Union[torch.Tensor, Dict[NodeType, torch.Tensor]]): The input
          node features of a homogeneous or heterogeneous graph.
      edge_index (Union[torch.Tensor, Dict[NodeType, torch.Tensor]]): The
          input edge indices of a homogeneous or heterogeneous graph.
      target (torch.Tensor): The target of the model.
          If the explanation type is :obj:`"phenomenon"`, the target has
          to be provided.
          If the explanation type is :obj:`"model"`, the target should be
          set to :obj:`None` and will get automatically inferred. For
          classification tasks, the target needs to contain the class
          labels. (default: :obj:`None`)
      index (Union[int, Tensor], optional): The indices in the
          first-dimension of the model output to explain.
          Can be a single index or a tensor of indices.
          If set to :obj:`None`, all model outputs will be explained.
          (default: :obj:`None`)
      **kwargs: additional arguments to pass to the GNN.
docstr_tok: ["Computes","the","explanation","of","the","GNN","for","the","given","inputs","and","target",".","..","note",":",":","If","you","get","an","error","message","like","``","Trying","to","backward","through","the","graph","a","second","time","''",",","make","sure","that","the","target","you","provided","was","computed","with",":","meth",":","`","torch.no_grad","`",".","Args",":","x","(","Union","[","torch.Tensor",",","Dict","[","NodeType",",","torch.Tensor","]","]",")",":","The","input","node","features","of","a","homogeneous","or","heterogeneous","graph",".","edge_index","(","Union","[","torch.Tensor",",","Dict","[","NodeType",",","torch.Tensor","]","]",")",":","The","input","edge","indices","of","a","homogeneous","or","heterogeneous","graph",".","target","(","torch.Tensor",")",":","The","target","of","the","model",".","If","the","explanation","type","is",":","obj",":","`","``","phenomenon","''","`",",","the","target","has","to","be","provided",".","If","the","explanation","type","is",":","obj",":","`","``","model","''","`",",","the","target","should","be","set","to",":","obj",":","`","None","`","and","will","get","automatically","inferred",".","For","classification","tasks",",","the","target","needs","to","contain","the","class","labels",".","(","default",":",":","obj",":","`","None","`",")","index","(","Union","[","int",",","Tensor","]",",","optional",")",":","The","indices","in","the","first-dimension","of","the","model","output","to","explain",".","Can","be","a","single","index","or","a","tensor","of","indices",".","If","set","to",":","obj",":","`","None","`",",","all","model","outputs","will","be","explained",".","(","default",":",":","obj",":","`","None","`",")","*","*","kwargs",":","additional","arguments","to","pass","to","the","GNN","."]
code: def __call__(
      self,
      x: Union[Tensor, Dict[NodeType, Tensor]],
      edge_index: Union[Tensor, Dict[EdgeType, Tensor]],
      *,
      target: Optional[Tensor] = None,
      index: Optional[Union[int, Tensor]] = None,
      **kwargs,
  ) -> Union[Explanation, HeteroExplanation]:
      r"""Computes the explanation of the GNN for the given inputs and
      target.
  
      .. note::
  
          If you get an error message like "Trying to backward through the
          graph a second time", make sure that the target you provided
          was computed with :meth:`torch.no_grad`.
  
      Args:
          x (Union[torch.Tensor, Dict[NodeType, torch.Tensor]]): The input
              node features of a homogeneous or heterogeneous graph.
          edge_index (Union[torch.Tensor, Dict[NodeType, torch.Tensor]]): The
              input edge indices of a homogeneous or heterogeneous graph.
          target (torch.Tensor): The target of the model.
              If the explanation type is :obj:`"phenomenon"`, the target has
              to be provided.
              If the explanation type is :obj:`"model"`, the target should be
              set to :obj:`None` and will get automatically inferred. For
              classification tasks, the target needs to contain the class
              labels. (default: :obj:`None`)
          index (Union[int, Tensor], optional): The indices in the
              first-dimension of the model output to explain.
              Can be a single index or a tensor of indices.
              If set to :obj:`None`, all model outputs will be explained.
              (default: :obj:`None`)
          **kwargs: additional arguments to pass to the GNN.
      """
      # Choose the `target` depending on the explanation type:
      prediction: Optional[Tensor] = None
      if self.explanation_type == ExplanationType.phenomenon:
          if target is None:
              raise ValueError(
                  f"The 'target' has to be provided for the explanation "
                  f"type '{self.explanation_type.value}'"
              )
      elif self.explanation_type == ExplanationType.model:
          if target is not None:
              warnings.warn(
                  f"The 'target' should not be provided for the explanation "
                  f"type '{self.explanation_type.value}'"
              )
          prediction = self.get_prediction(x, edge_index, **kwargs)
          target = self.get_target(prediction)
  
      if isinstance(index, int):
          index = torch.tensor([index])
  
      training = self.model.training
      self.model.eval()
  
      explanation = self.algorithm(
          self.model,
          x,
          edge_index,
          target=target,
          index=index,
          **kwargs,
      )
  
      self.model.train(training)
  
      # Add explainer objectives to the `Explanation` object:
      explanation._model_config = self.model_config
      explanation.prediction = prediction
      explanation.target = target
      explanation.index = index
  
      # Add model inputs to the `Explanation` object:
      if isinstance(explanation, Explanation):
          explanation._model_args = list(kwargs.keys())
          explanation.x = x
          explanation.edge_index = edge_index
  
          for key, arg in kwargs.items():  # Add remaining `kwargs`:
              explanation[key] = arg
  
      elif isinstance(explanation, HeteroExplanation):
          # TODO Add `explanation._model_args`
  
          assert isinstance(x, dict)
          explanation.set_value_dict("x", x)
  
          assert isinstance(edge_index, dict)
          explanation.set_value_dict("edge_index", edge_index)
  
          for key, arg in kwargs.items():  # Add remaining `kwargs`:
              if isinstance(arg, dict):
                  # Keyword arguments are likely named `{attr_name}_dict`
                  # while we only want to assign the `{attr_name}` to the
                  # `HeteroExplanation` object:
                  key = key[:-5] if key.endswith("_dict") else key
                  explanation.set_value_dict(key, arg)
              else:
                  explanation[key] = arg
  
      explanation.validate_masks()
      return explanation.threshold(self.threshold_config)
  
file_path: pytorch_geometric/torch_geometric/explain/explainer.py
imports: import warnings
  from typing import Any, Dict, Optional, Union
  import torch
  from torch import Tensor
  from torch_geometric.explain import ExplainerAlgorithm, Explanation, HeteroExplanation
  from torch_geometric.explain.algorithm.utils import clear_masks, set_hetero_masks, set_masks
  from torch_geometric.explain.config import ExplainerConfig, ExplanationType, MaskType, ModelConfig, ModelMode, ModelReturnType, ThresholdConfig
  from torch_geometric.typing import EdgeType, NodeType
label_desc: Use image node_id 4 for calling the Explainer obj's underlying member method code with example usage: obj.__call__(x, edge_index) and returns: explanation