class_docstr: GNNExplainer model from `GNNExplainer: Generating Explanations for
  Graph Neural Networks <https://arxiv.org/abs/1903.03894>`__
  
  It identifies compact subgraph structures and small subsets of node features that play a
  critical role in GNN-based node classification and graph classification.
  
  To generate an explanation, it learns an edge mask :math:`M` and a feature mask :math:`F`
  by optimizing the following objective function.
  
  .. math::
    l(y, \hat{y}) + \alpha_1 \|M\|_1 + \alpha_2 H(M) + \beta_1 \|F\|_1 + \beta_2 H(F)
  
  where :math:`l` is the loss function, :math:`y` is the original model prediction,
  :math:`\hat{y}` is the model prediction with the edge and feature mask applied, :math:`H` is
  the entropy function.
  
  Parameters
  ----------
  model : nn.Module
      The GNN model to explain.
  
      * The required arguments of its forward function are graph and feat.
        The latter one is for input node features.
      * It should also optionally take an eweight argument for edge weights
        and multiply the messages by it in message passing.
      * The output of its forward function is the logits for the predicted
        node/graph classes.
  
      See also the example in :func:`explain_node` and :func:`explain_graph`.
  num_hops : int
      The number of hops for GNN information aggregation.
  lr : float, optional
      The learning rate to use, default to 0.01.
  num_epochs : int, optional
      The number of epochs to train.
  alpha1 : float, optional
      A higher value will make the explanation edge masks more sparse by decreasing
      the sum of the edge mask.
  alpha2 : float, optional
      A higher value will make the explanation edge masks more sparse by decreasing
      the entropy of the edge mask.
  beta1 : float, optional
      A higher value will make the explanation node feature masks more sparse by
      decreasing the mean of the node feature mask.
  beta2 : float, optional
      A higher value will make the explanation node feature masks more sparse by
      decreasing the entropy of the node feature mask.
  log : bool, optional
      If True, it will log the computation process, default to True.
class_docstr_tok: ["GNNExplainer","model","from","`","GNNExplainer",":","Generating","Explanations","for","Graph","Neural","Networks","<","https",":","\/\/arxiv.org\/abs\/1903.03894",">","`","__","It","identifies","compact","subgraph","structures","and","small","subsets","of","node","features","that","play","a","critical","role","in","GNN-based","node","classification","and","graph","classification",".","To","generate","an","explanation",",","it","learns","an","edge","mask",":","math",":","`","M","`","and","a","feature","mask",":","math",":","`","F","`","by","optimizing","the","following","objective","function",".","..","math",":",":","l","(","y",",","\\hat","{","y","}",")","+","\\alpha_1","\\|M\\|_1","+","\\alpha_2","H","(","M",")","+","\\beta_1","\\|F\\|_1","+","\\beta_2","H","(","F",")","where",":","math",":","`","l","`","is","the","loss","function",",",":","math",":","`","y","`","is","the","original","model","prediction",",",":","math",":","`","\\hat","{","y","}","`","is","the","model","prediction","with","the","edge","and","feature","mask","applied",",",":","math",":","`","H","`","is","the","entropy","function",".","Parameters","--","--","--","--","--","model",":","nn.Module","The","GNN","model","to","explain",".","*","The","required","arguments","of","its","forward","function","are","graph","and","feat",".","The","latter","one","is","for","input","node","features",".","*","It","should","also","optionally","take","an","eweight","argument","for","edge","weights","and","multiply","the","messages","by","it","in","message","passing",".","*","The","output","of","its","forward","function","is","the","logits","for","the","predicted","node\/graph","classes",".","See","also","the","example","in",":","func",":","`","explain_node","`","and",":","func",":","`","explain_graph","`",".","num_hops",":","int","The","number","of","hops","for","GNN","information","aggregation",".","lr",":","float",",","optional","The","learning","rate","to","use",",","default","to","0.01.","num_epochs",":","int",",","optional","The","number","of","epochs","to","train",".","alpha1",":","float",",","optional","A","higher","value","will","make","the","explanation","edge","masks","more","sparse","by","decreasing","the","sum","of","the","edge","mask",".","alpha2",":","float",",","optional","A","higher","value","will","make","the","explanation","edge","masks","more","sparse","by","decreasing","the","entropy","of","the","edge","mask",".","beta1",":","float",",","optional","A","higher","value","will","make","the","explanation","node","feature","masks","more","sparse","by","decreasing","the","mean","of","the","node","feature","mask",".","beta2",":","float",",","optional","A","higher","value","will","make","the","explanation","node","feature","masks","more","sparse","by","decreasing","the","entropy","of","the","node","feature","mask",".","log",":","bool",",","optional","If","True",",","it","will","log","the","computation","process",",","default","to","True","."]
docstr: Add regularization terms to the loss.
  
  Parameters
  ----------
  loss : Tensor
      Loss value.
  feat_mask : Tensor
      Feature mask of shape :math:`(1, D)`, where :math:`D`
      is the feature size.
  edge_mask : Tensor
      Edge mask of shape :math:`(E)`, where :math:`E`
      is the number of edges.
  
  Returns
  -------
  Tensor
      Loss value with regularization terms added.
docstr_tok: ["Add","regularization","terms","to","the","loss",".","Parameters","--","--","--","--","--","loss",":","Tensor","Loss","value",".","feat_mask",":","Tensor","Feature","mask","of","shape",":","math",":","`","(","1",",","D",")","`",",","where",":","math",":","`","D","`","is","the","feature","size",".","edge_mask",":","Tensor","Edge","mask","of","shape",":","math",":","`","(","E",")","`",",","where",":","math",":","`","E","`","is","the","number","of","edges",".","Returns","--","--","--","-","Tensor","Loss","value","with","regularization","terms","added","."]
code: def _loss_regularize(self, loss, feat_mask, edge_mask):
      r"""Add regularization terms to the loss.
  
      Parameters
      ----------
      loss : Tensor
          Loss value.
      feat_mask : Tensor
          Feature mask of shape :math:`(1, D)`, where :math:`D`
          is the feature size.
      edge_mask : Tensor
          Edge mask of shape :math:`(E)`, where :math:`E`
          is the number of edges.
  
      Returns
      -------
      Tensor
          Loss value with regularization terms added.
      """
      # epsilon for numerical stability
      eps = 1e-15
  
      edge_mask = edge_mask.sigmoid()
      # Edge mask sparsity regularization
      loss = loss + self.alpha1 * torch.sum(edge_mask)
      # Edge mask entropy regularization
      ent = -edge_mask * torch.log(edge_mask + eps) - (
          1 - edge_mask
      ) * torch.log(1 - edge_mask + eps)
      loss = loss + self.alpha2 * ent.mean()
  
      feat_mask = feat_mask.sigmoid()
      # Feature mask sparsity regularization
      loss = loss + self.beta1 * torch.mean(feat_mask)
      # Feature mask entropy regularization
      ent = -feat_mask * torch.log(feat_mask + eps) - (
          1 - feat_mask
      ) * torch.log(1 - feat_mask + eps)
      loss = loss + self.beta2 * ent.mean()
  
      return loss
  
file_path: dgl/python/dgl/nn/pytorch/explain/gnnexplainer.py
imports: from math import sqrt
  import torch
  from torch import nn
  from tqdm import tqdm
  from ....base import EID, NID
  from ....subgraph import khop_in_subgraph
label_desc: Use image node_id 3 for calling the GNNExplainer obj's underlying member method code with example usage: obj._loss_regularize(loss, feat_mask, edge_mask) and returns: loss