{"record":{"id":"1f43b73737ff8370","repo":"tensorflow/models","slug":"weight-and-label-tensors-were-not-of-the-same-rank","errorCode":null,"errorMessage":"Weight and label tensors were not of the same rank. weights.shape was %s, and labels.shape was %s.","messagePattern":"Weight and label tensors were not of the same rank\\. weights\\.shape was (.+?), and labels\\.shape was (.+?)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"official/nlp/modeling/losses/weighted_sparse_categorical_crossentropy.py","lineNumber":30,"sourceCode":"# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"Weighted sparse categorical cross-entropy losses.\"\"\"\n\nimport 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.","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/nlp/modeling/losses/weighted_sparse_categorical_crossentropy.py#L12-L48","documentation":"Error \"Weight and label tensors were not of the same rank. weights.shape was %s, and labels.shape was %s.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/nlp/modeling/losses/weighted_sparse_categorical_crossentropy.py:30 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"}