{"record":{"id":"f27c294c78ef6423","repo":"tensorflow/models","slug":"k-must-be-a-positive-integer","errorCode":null,"errorMessage":"k must be a positive integer.","messagePattern":"k must be a positive integer\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/projects/yt8m/eval_utils/eval_util.py","lineNumber":123,"sourceCode":"      are 'batch' x 'num_classes'.\n    labels: A numpy matrix containing the ground truth labels. Dimensions are\n      'batch' x 'num_classes'.\n    k: the top k non-zero entries to preserve in each prediction.\n\n  Returns:\n    A tuple (predictions,labels, true_positives). 'predictions' and 'labels'\n    are lists of lists of floats. 'true_positives' is a list of scalars. The\n    length of the lists are equal to the number of classes. The entries in the\n    predictions variable are probability predictions, and\n    the corresponding entries in the labels variable are the ground truth for\n    those predictions. The entries in 'true_positives' are the number of true\n    positives for each class in the ground truth.\n\n  Raises:\n    ValueError: An error occurred when the k is not a positive integer.\n  \"\"\"\n  if k <= 0:\n    raise ValueError(\"k must be a positive integer.\")\n  k = min(k, predictions.shape[1])\n  num_classes = predictions.shape[1]\n  prediction_triplets = []\n  for video_index in range(predictions.shape[0]):\n    prediction_triplets.extend(\n        top_k_triplets(predictions[video_index], labels[video_index], k))\n  out_predictions = [[] for _ in range(num_classes)]\n  out_labels = [[] for _ in range(num_classes)]\n  for triplet in prediction_triplets:\n    out_predictions[triplet[0]].append(triplet[1])\n    out_labels[triplet[0]].append(triplet[2])\n  out_true_positives = [np.sum(labels[:, i]) for i in range(num_classes)]\n\n  return out_predictions, out_labels, out_true_positives\n\n\ndef top_k_triplets(predictions, labels, k=20):\n  \"\"\"Get the top_k for a 1-d numpy array.","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/projects/yt8m/eval_utils/eval_util.py#L105-L141","documentation":"Error \"k must be a positive integer.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/projects/yt8m/eval_utils/eval_util.py:123 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}