code: def compute_true_positives_at_k(
      predictions: torch.Tensor,
      labels: torch.Tensor,
      weights: torch.Tensor,
      n_classes: int,
  ) -> torch.Tensor:
      """
      Compute and return a list of weighted true positives (true predictions) at k. When k = 0,
      tp is counted when the 1st predicted class matches the label. When k = 1, tp is counted
      when either the 1st or 2nd predicted class matches the label.
  
      Args:
          predictions (Tensor): Tensor of label predictions with shape of (n_sample, n_class) or (n_task, n_sample, n_class).
          labels (Tensor): Tensor of ground truth labels with shape of (n_sample, ) or (n_task, n_sample).
          weights (Tensor): Tensor of weight on each sample, with shape of (n_sample, ) or (n_task, n_sample).
          n_classes (int): Number of classes.
  
      Output:
          true_positives_list (Tensor): Tensor of true positives with shape of (n_class, ) or (n_task, n_class).
  
      Examples:
  
          >>> predictions = torch.tensor([[0.9, 0.1, 0, 0, 0], [0.1, 0.2, 0.25, 0.15, 0.3], [0, 1.0, 0, 0, 0], [0, 0, 0.2, 0.7, 0.1]])
          >>> labels = torch.tensor([0, 3, 1, 2])
          >>> weights = torch.tensor([1, 0.25, 0.5, 0.25])
          >>> n_classes = 5
          >>> true_positives_list = compute_multiclass_k_sum(predictions, labels, n_classes)
          >>> true_positives_list
          tensor([1.5000, 1.7500, 1.7500, 2.0000, 2.0000])
  
      """
      ranks = torch.argsort(predictions, dim=-1, descending=True)
      true_positives = (
          torch.zeros(1, device=predictions.device)
          if predictions.ndim == 2
          else torch.zeros(
              predictions.shape[0], 1, device=predictions.device
          )
      )
      true_positives_list = torch.tensor([], device=predictions.device)
  
      for k in range(n_classes):
          mask = (
              torch.unsqueeze(labels, dim=-1) == ranks[..., k : k + 1]
          )
          mask = mask * torch.unsqueeze(weights, dim=-1)
          true_positives += mask.sum(dim=-2)
          true_positives_list = torch.cat(
              (true_positives_list, true_positives), dim=-1
          )
  
      return true_positives_list
  
file_path: torchrec/torchrec/metrics/multiclass_recall.py
imports: from typing import Any, cast, Dict, List, Optional, Type
  import torch
  from torchrec.metrics.metrics_namespace import MetricName, MetricNamespace, MetricPrefix
  from torchrec.metrics.rec_metric import MetricComputationReport, RecMetric, RecMetricComputation, RecMetricException
label_desc: Use image node_id 1 for calling a global function with example usage: compute_true_positives_at_k(predictions, labels, weights, n_classes) and returns: true_positives_list