code: def top_k_by_class(predictions, labels, k=20):
      """Extracts the top k predictions for each video, sorted by class.
  
      Args:
        predictions: A numpy matrix containing the outputs of the model.
          Dimensions are 'batch' x 'num_classes'.
        k: the top k non-zero entries to preserve in each prediction.
  
      Returns:
        A tuple (predictions,labels, true_positives). 'predictions' and 'labels'
        are lists of lists of floats. 'true_positives' is a list of scalars. The
        length of the lists are equal to the number of classes. The entries in the
        predictions variable are probability predictions, and
        the corresponding entries in the labels variable are the ground truth for
        those predictions. The entries in 'true_positives' are the number of true
        positives for each class in the ground truth.
  
      Raises:
        ValueError: An error occurred when the k is not a positive integer.
      """
      if k <= 0:
          raise ValueError("k must be a positive integer.")
      k = min(k, predictions.shape[1])
      num_classes = predictions.shape[1]
      prediction_triplets = []
      for video_index in range(predictions.shape[0]):
          prediction_triplets.extend(
              top_k_triplets(
                  predictions[video_index], labels[video_index], k
              )
          )
      out_predictions = [[] for v in range(num_classes)]
      out_labels = [[] for v in range(num_classes)]
      for triplet in prediction_triplets:
          out_predictions[triplet[0]].append(triplet[1])
          out_labels[triplet[0]].append(triplet[2])
      out_true_positives = [
          numpy.sum(labels[:, i]) for i in range(num_classes)
      ]
  
      return out_predictions, out_labels, out_true_positives
  
file_path: PaddleHub/modules/video/classification/videotag_tsn_lstm/resource/metrics/youtube8m/eval_util.py
imports: import datetime
  import numpy
  from .None import mean_average_precision_calculator
  from .None import average_precision_calculator
label_desc: Use image node_id 5 for calling a global function with example usage: top_k_by_class(predictions, labels, k) and returns: out_predictions,  out_labels,  out_true_positives