class_docstr: A SaliencyMask class that implements the integrated gradients method.
class_docstr_tok: ["A","SaliencyMask","class","that","implements","the","integrated","gradients","method","."]
docstr: Args:
      node_idx: the index of the node to calculate gradients for.
      class_of_interest: the index for the class probability that the gradients will be calculated for.
      non_exist_edge (bool): Setting to True allows the function to get the importance for non-exist edges.
          This is useful when we want to understand adding which edges could change the current predictions.
          But the results for existing edges are not reliable. Simiarly, setting to False ((A_baseline = all zero matrix))
          could only accurately measure the importance of existing edges.
      adj_baseline: For integrated gradients, adj_baseline is the reference adjacency matrix to start with. Generally
          we should set A_baseline to an all-zero matrix or all-one matrix with the size of the original
          A_baseline matrix.
      steps (int): The number of values we need to interpolate. Generally steps = 20 should give good enough results.
  
  Returns
      (Numpy array): Integrated gradients for the links.
docstr_tok: ["Args",":","node_idx",":","the","index","of","the","node","to","calculate","gradients","for",".","class_of_interest",":","the","index","for","the","class","probability","that","the","gradients","will","be","calculated","for",".","non_exist_edge","(","bool",")",":","Setting","to","True","allows","the","function","to","get","the","importance","for","non-exist","edges",".","This","is","useful","when","we","want","to","understand","adding","which","edges","could","change","the","current","predictions",".","But","the","results","for","existing","edges","are","not","reliable",".","Simiarly",",","setting","to","False","(","(","A_baseline","=","all","zero","matrix",")",")","could","only","accurately","measure","the","importance","of","existing","edges",".","adj_baseline",":","For","integrated","gradients",",","adj_baseline","is","the","reference","adjacency","matrix","to","start","with",".","Generally","we","should","set","A_baseline","to","an","all-zero","matrix","or","all-one","matrix","with","the","size","of","the","original","A_baseline","matrix",".","steps","(","int",")",":","The","number","of","values","we","need","to","interpolate",".","Generally","steps","=","20","should","give","good","enough","results",".","Returns","(","Numpy","array",")",":","Integrated","gradients","for","the","links","."]
code: def get_integrated_link_masks(
      self,
      node_idx,
      class_of_interest,
      non_exist_edge=False,
      adj_baseline=None,
      steps=20,
  ):
      """
      Args:
          node_idx: the index of the node to calculate gradients for.
          class_of_interest: the index for the class probability that the gradients will be calculated for.
          non_exist_edge (bool): Setting to True allows the function to get the importance for non-exist edges.
              This is useful when we want to understand adding which edges could change the current predictions.
              But the results for existing edges are not reliable. Simiarly, setting to False ((A_baseline = all zero matrix))
              could only accurately measure the importance of existing edges.
          adj_baseline: For integrated gradients, adj_baseline is the reference adjacency matrix to start with. Generally
              we should set A_baseline to an all-zero matrix or all-one matrix with the size of the original
              A_baseline matrix.
          steps (int): The number of values we need to interpolate. Generally steps = 20 should give good enough results.
  
      Returns
          (Numpy array): Integrated gradients for the links.
      """
      if adj_baseline is None:
          if non_exist_edge:
              adj_baseline = np.ones(self._adj.shape)
          else:
              adj_baseline = np.zeros(self._adj.shape)
  
      adj_diff = self._adj - adj_baseline
  
      total_gradients = np.zeros_like(self._adj)
  
      for alpha in np.linspace(1.0 / steps, 1.0, steps):
          adj_step = adj_baseline + alpha * adj_diff
  
          if self._is_sparse:
              model_input = [
                  self._features,
                  np.array([[node_idx]]),
                  self._adj_inds,
                  adj_step,
              ]
          else:
              model_input = [
                  self._features,
                  np.array([[node_idx]]),
                  adj_step,
              ]
  
          model_input = [tf.convert_to_tensor(x) for x in model_input]
          grads = self._compute_gradients(
              model_input, class_of_interest, wrt=model_input[-1]
          )
  
          total_gradients += grads.numpy()
  
      if self._is_sparse:
          total_gradients = csr_matrix(
              (
                  total_gradients[0],
                  (self._adj_inds[0, :, 0], self._adj_inds[0, :, 1]),
              )
          )
          adj_diff = csr_matrix(
              (
                  adj_diff[0],
                  (self._adj_inds[0, :, 0], self._adj_inds[0, :, 1]),
              )
          )
          total_gradients = total_gradients.multiply(adj_diff) / steps
      else:
          total_gradients = np.squeeze(
              np.multiply(total_gradients, adj_diff) / steps, 0
          )
  
      return total_gradients
  
file_path: stellargraph/stellargraph/interpretability/saliency_maps/integrated_gradients.py
imports: import numpy
  from tensorflow.keras import backend
  from scipy.sparse import csr_matrix
  import tensorflow
  from stellargraph.mapper import SparseFullBatchSequence, FullBatchSequence
label_desc: Use image node_id 3 for calling the IntegratedGradients obj's underlying member method code with example usage: obj.get_integrated_link_masks(node_idx, class_of_interest, non_exist_edge, adj_baseline, steps) and returns: total_gradients