{"record":{"id":"67c981c82a427bbe","repo":"tensorflow/models","slug":"weighted-sparse-categorical-crossentropy-expects","errorCode":null,"errorMessage":"Weighted sparse categorical crossentropy expects `labels` to have a rank of one less than `predictions`. labels.shape was %s, and predictions.shape was %s.","messagePattern":"Weighted sparse categorical crossentropy expects `labels` to have a rank of one less than `predictions`\\. labels\\.shape was (.+?), and predictions\\.shape was (.+?)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"official/nlp/modeling/losses/weighted_sparse_categorical_crossentropy.py","lineNumber":35,"sourceCode":"import tensorflow as tf, tf_keras\n\n\ndef _adjust_labels(labels, predictions):\n  \"\"\"Adjust the 'labels' tensor by squeezing it if needed.\"\"\"\n  labels = tf.cast(labels, tf.int32)\n  if len(predictions.shape) == len(labels.shape):\n    labels = tf.squeeze(labels, [-1])\n  return labels, predictions\n\n\ndef _validate_rank(labels, predictions, weights):\n  if weights is not None and len(weights.shape) != len(labels.shape):\n    raise RuntimeError(\n        (\"Weight and label tensors were not of the same rank. weights.shape \"\n         \"was %s, and labels.shape was %s.\") %\n        (predictions.shape, labels.shape))\n  if (len(predictions.shape) - 1) != len(labels.shape):\n    raise RuntimeError(\n        (\"Weighted sparse categorical crossentropy expects `labels` to have a \"\n         \"rank of one less than `predictions`. labels.shape was %s, and \"\n         \"predictions.shape was %s.\") % (labels.shape, predictions.shape))\n\n\ndef loss(labels, predictions, weights=None, from_logits=False):\n  \"\"\"Calculate a per-batch sparse categorical crossentropy loss.\n\n  This loss function assumes that the predictions are post-softmax.\n  Args:\n    labels: The labels to evaluate against. Should be a set of integer indices\n      ranging from 0 to (vocab_size-1).\n    predictions: The network predictions. Should have softmax already applied.\n    weights: An optional weight array of the same shape as the 'labels' array.\n      If None, all examples will be used.\n    from_logits: Whether the input predictions are logits.\n\n  Returns:","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/nlp/modeling/losses/weighted_sparse_categorical_crossentropy.py#L17-L53","documentation":"Error \"Weighted sparse categorical crossentropy expects `labels` to have a rank of one less than `predictions`. labels.shape was %s, and predictions.shape was %s.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/nlp/modeling/losses/weighted_sparse_categorical_crossentropy.py:35 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"}