class_docstr: The GraphMask-Explainer model from the `"Interpreting Graph Neural
  Networks for NLP With Differentiable Edge Masking"
  <https://arxiv.org/abs/2010.00577>`_ paper for identifying layer-wise
  compact subgraph structures and node features that play a crucial role in
  the predictions made by a GNN.
  
  .. note::
      For an example of using :class:`GraphMaskExplainer`,
      see `examples/explain/graphmask_explainer.py
      <https://github.com/pyg-team/pytorch_geometric/blob/master/examples/
      /explain/graphmask_explainer.py>`_.
  
      A working real-time example of :class:`GraphMaskExplainer` in the form
      of a deployed app can be accessed `here
      <https://graph-explainability.streamlit.app/>`_.
  
  Args:
      num_layers (int): The number of layers to use.
      epochs (int, optional): The number of epochs to train.
          (default: :obj:`100`)
      lr (float, optional): The learning rate to apply.
          (default: :obj:`0.01`)
      penalty_scaling (int, optional): Scaling value of penalty term. Value
          must lie between 0 and 10. (default: :obj:`5`)
      lambda_optimizer_lr (float, optional): The learning rate to optimize
          the Lagrange multiplier. (default: :obj:`1e-2`)
      init_lambda (float, optional): The Lagrange multiplier. Value must lie
          between :obj:`0` and `1`. (default: :obj:`0.55`)
      allowance (float, optional): A float value between :obj:`0` and
          :obj:`1` denotes tolerance level. (default: :obj:`0.03`)
      log (bool, optional): If set to :obj:`False`, will not log any
          learning progress. (default: :obj:`True`)
      **kwargs (optional): Additional hyper-parameters to override default
          settings in
          :attr:`~torch_geometric.nn.models.GraphMaskExplainer.coeffs`.
class_docstr_tok: ["The","GraphMask-Explainer","model","from","the","`","``","Interpreting","Graph","Neural","Networks","for","NLP","With","Differentiable","Edge","Masking","''","<","https",":","\/\/arxiv.org\/abs\/2010.00577",">","`","_","paper","for","identifying","layer-wise","compact","subgraph","structures","and","node","features","that","play","a","crucial","role","in","the","predictions","made","by","a","GNN",".","..","note",":",":","For","an","example","of","using",":","class",":","`","GraphMaskExplainer","`",",","see","`","examples\/explain\/graphmask_explainer.py","<","https",":","\/\/github.com\/pyg-team\/pytorch_geometric\/blob\/master\/examples\/","\/explain\/graphmask_explainer.py",">","`","_",".","A","working","real-time","example","of",":","class",":","`","GraphMaskExplainer","`","in","the","form","of","a","deployed","app","can","be","accessed","`","here","<","https",":","\/\/graph-explainability.streamlit.app\/",">","`","_",".","Args",":","num_layers","(","int",")",":","The","number","of","layers","to","use",".","epochs","(","int",",","optional",")",":","The","number","of","epochs","to","train",".","(","default",":",":","obj",":","`","100","`",")","lr","(","float",",","optional",")",":","The","learning","rate","to","apply",".","(","default",":",":","obj",":","`","0.01","`",")","penalty_scaling","(","int",",","optional",")",":","Scaling","value","of","penalty","term",".","Value","must","lie","between","0","and","10",".","(","default",":",":","obj",":","`","5","`",")","lambda_optimizer_lr","(","float",",","optional",")",":","The","learning","rate","to","optimize","the","Lagrange","multiplier",".","(","default",":",":","obj",":","`","1e-2","`",")","init_lambda","(","float",",","optional",")",":","The","Lagrange","multiplier",".","Value","must","lie","between",":","obj",":","`","0","`","and","`","1","`",".","(","default",":",":","obj",":","`","0.55","`",")","allowance","(","float",",","optional",")",":","A","float","value","between",":","obj",":","`","0","`","and",":","obj",":","`","1","`","denotes","tolerance","level",".","(","default",":",":","obj",":","`","0.03","`",")","log","(","bool",",","optional",")",":","If","set","to",":","obj",":","`","False","`",",","will","not","log","any","learning","progress",".","(","default",":",":","obj",":","`","True","`",")","*","*","kwargs","(","optional",")",":","Additional","hyper-parameters","to","override","default","settings","in",":","attr",":","`","~torch_geometric.nn.models.GraphMaskExplainer.coeffs","`","."]
docstr: Generates explanations for the original GNN model.
  
  Args:
      model (torch.nn.Module): The model to explain.
      index (int or torch.Tensor, optional): The index of the model
          output to explain. Needs to be a single index.
          (default: :obj:`None`).
docstr_tok: ["Generates","explanations","for","the","original","GNN","model",".","Args",":","model","(","torch.nn.Module",")",":","The","model","to","explain",".","index","(","int","or","torch.Tensor",",","optional",")",":","The","index","of","the","model","output","to","explain",".","Needs","to","be","a","single","index",".","(","default",":",":","obj",":","`","None","`",")","."]
code: def _explain(
      self,
      model: torch.nn.Module,
      *,
      index: Optional[Union[int, Tensor]] = None,
  ) -> Tensor:
      r"""Generates explanations for the original GNN model.
  
      Args:
          model (torch.nn.Module): The model to explain.
          index (int or torch.Tensor, optional): The index of the model
              output to explain. Needs to be a single index.
              (default: :obj:`None`).
      """
      if (
          not isinstance(index, Tensor)
          and not isinstance(index, int)
          and index is not None
      ):
          raise ValueError(
              "'index' parameter can only be a 'Tensor', "
              "'integer' or set to 'None' instead."
          )
  
      self._freeze_model(model)
      self._set_flags(model)
  
      with torch.no_grad():
          latest_source_embeddings, latest_messages = [], []
          latest_target_embeddings = []
          for module in model.modules():
              if isinstance(module, MessagePassing):
                  latest_source_embeddings.append(
                      module.latest_source_embeddings
                  )
                  latest_messages.append(module.latest_messages)
                  latest_target_embeddings.append(
                      module.latest_target_embeddings
                  )
          gate_input = [
              latest_source_embeddings,
              latest_messages,
              latest_target_embeddings,
          ]
          if self.log:
              pbar = tqdm(total=self.num_layers)
          for i in range(self.num_layers):
              if self.log:
                  pbar.set_description("Explain")
              output = self.full_biases[i]
              for j in range(len(gate_input)):
                  partial = self.gates[i * 4][j](gate_input[j][i])
                  result = self.gates[(i * 4) + 1][j](partial)
                  output = output + result
              relu_output = self.gates[(i * 4) + 2](
                  output / len(gate_input)
              )
              sampling_weights = self.gates[(i * 4) + 3](
                  relu_output
              ).squeeze(dim=-1)
              sampling_weights, _ = self._hard_concrete(
                  sampling_weights, training=False
              )
              if i == 0:
                  edge_weight = sampling_weights
              else:
                  edge_weight = torch.cat(
                      (edge_weight, sampling_weights), 0
                  )
              if self.log:
                  pbar.update(1)
      if self.log:
          pbar.close()
  
      edge_mask = edge_weight.view(
          -1, edge_weight.size(0) // self.num_layers
      )
      edge_mask = torch.mean(edge_mask, 0)
  
      return edge_mask
  
imports: import math
  from typing import List, Optional, Tuple, Union
  import numpy
  import torch
  import torch.nn.functional
  from torch import Tensor
  from torch.nn import LayerNorm, Linear, Parameter, ReLU
  from tqdm import tqdm
  from torch_geometric.explain import Explanation
  from torch_geometric.explain.algorithm import ExplainerAlgorithm
  from torch_geometric.explain.config import MaskType, ModelMode, ModelTaskLevel
  from torch_geometric.nn import MessagePassing
label_desc: Use image node_id 13 for calling the GraphMaskExplainer obj's underlying member method code with example usage: obj._explain(model) and returns: edge_mask