class_docstr: An abstract class for computing link prediction retrieval metrics.
  
  Args:
      k (int): The number of top-:math:`k` predictions to evaluate against.
class_docstr_tok: ["An","abstract","class","for","computing","link","prediction","retrieval","metrics",".","Args",":","k","(","int",")",":","The","number","of","top-",":","math",":","`","k","`","predictions","to","evaluate","against","."]
docstr: Updates the state variables based on the current mini-batch
  prediction.
  
  :meth:`update` can be repeated multiple times to accumulate the results
  of successive predictions, *e.g.*, inside a mini-batch training or
  evaluation loop.
  
  Args:
      pred_index_mat (torch.Tensor): The top-:math:`k` predictions of
          every example in the mini-batch with shape
          :obj:`[batch_size, k]`.
      edge_label_index (torch.Tensor): The ground-truth indices for every
          example in the mini-batch, given in COO format of shape
          :obj:`[2, num_ground_truth_indices]`.
docstr_tok: ["Updates","the","state","variables","based","on","the","current","mini-batch","prediction",".",":","meth",":","`","update","`","can","be","repeated","multiple","times","to","accumulate","the","results","of","successive","predictions",",","*","e.g",".","*",",","inside","a","mini-batch","training","or","evaluation","loop",".","Args",":","pred_index_mat","(","torch.Tensor",")",":","The","top-",":","math",":","`","k","`","predictions","of","every","example","in","the","mini-batch","with","shape",":","obj",":","`","[","batch_size",",","k","]","`",".","edge_label_index","(","torch.Tensor",")",":","The","ground-truth","indices","for","every","example","in","the","mini-batch",",","given","in","COO","format","of","shape",":","obj",":","`","[","2",",","num_ground_truth_indices","]","`","."]
code: def update(
      self,
      pred_index_mat: Tensor,
      edge_label_index: Union[Tensor, Tuple[Tensor, Tensor]],
  ):
      r"""Updates the state variables based on the current mini-batch
      prediction.
  
      :meth:`update` can be repeated multiple times to accumulate the results
      of successive predictions, *e.g.*, inside a mini-batch training or
      evaluation loop.
  
      Args:
          pred_index_mat (torch.Tensor): The top-:math:`k` predictions of
              every example in the mini-batch with shape
              :obj:`[batch_size, k]`.
          edge_label_index (torch.Tensor): The ground-truth indices for every
              example in the mini-batch, given in COO format of shape
              :obj:`[2, num_ground_truth_indices]`.
      """
      if pred_index_mat.size(1) != self.k:
          raise ValueError(
              f"Expected 'pred_index_mat' to hold {self.k} "
              f"many indices for every entry "
              f"(got {pred_index_mat.size(1)})"
          )
  
      # Compute a boolean matrix indicating if the k-th prediction is part of
      # the ground-truth. We do this by flattening both prediction and
      # target indices, and then determining overlaps via `torch.isin`.
      max_index = (
          max(
              pred_index_mat.max() if pred_index_mat.numel() > 0 else 0,
              edge_label_index[1].max()
              if edge_label_index[1].numel() > 0
              else 0,
          )
          + 1
      )
      arange = torch.arange(
          start=0,
          end=max_index * pred_index_mat.size(0),
          step=max_index,
          device=pred_index_mat.device,
      ).view(-1, 1)
      flat_pred_index = (pred_index_mat + arange).view(-1)
      flat_y_index = (
          max_index * edge_label_index[0] + edge_label_index[1]
      )
  
      pred_isin_mat = torch.isin(flat_pred_index, flat_y_index)
      pred_isin_mat = pred_isin_mat.view(pred_index_mat.size())
  
      # Compute the number of targets per example:
      y_count = scatter(
          torch.ones_like(edge_label_index[0]),
          edge_label_index[0],
          dim=0,
          dim_size=pred_index_mat.size(0),
          reduce="sum",
      )
  
      metric = self._compute(pred_isin_mat, y_count)
  
      self.accum += metric.sum()
      self.total += (y_count > 0).sum()
  
imports: from abc import ABC, abstractmethod
  from typing import Optional, Tuple, Union
  import torch
  from torch import Tensor
  from torch_geometric.utils import cumsum, scatter
label_desc: Use image node_id 2 for calling the LinkPredMetric obj's underlying member method code with example usage: obj.update(pred_index_mat, edge_label_index) without return types