{"record":{"id":"8415cc6e576feaba","repo":"tensorflow/models","slug":"length-of-class-weights-should-be","errorCode":null,"errorMessage":"Length of class_weights should be {}","messagePattern":"Length of class_weights should be (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/projects/panoptic/losses/panoptic_deeplab_losses.py","lineNumber":62,"sourceCode":"    # Assign pixel with ignore label to class 0 (background). The loss on the\n    # pixel will later be masked out.\n    labels = tf.where(valid_mask, labels, tf.zeros_like(labels))\n\n    labels = tf.squeeze(tf.cast(labels, tf.int32), axis=3)\n    valid_mask = tf.squeeze(tf.cast(valid_mask, tf.float32), axis=3)\n    onehot_labels = tf.one_hot(labels, num_classes)\n    onehot_labels = onehot_labels * (\n        1 - self._label_smoothing) + self._label_smoothing / num_classes\n    cross_entropy_loss = tf.nn.softmax_cross_entropy_with_logits(\n        labels=onehot_labels, logits=logits)\n\n    if not self._class_weights:\n      class_weights = [1] * num_classes\n    else:\n      class_weights = self._class_weights\n\n    if num_classes != len(class_weights):\n      raise ValueError(\n          'Length of class_weights should be {}'.format(num_classes))\n\n    weight_mask = tf.einsum('...y,y->...',\n                            tf.one_hot(labels, num_classes, dtype=tf.float32),\n                            tf.constant(class_weights, tf.float32))\n    valid_mask *= weight_mask\n\n    if sample_weight is not None:\n      valid_mask *= sample_weight\n\n    cross_entropy_loss *= tf.cast(valid_mask, tf.float32)\n\n    if self._top_k_percent_pixels >= 1.0:\n      loss = tf.reduce_sum(cross_entropy_loss) / normalizer\n    else:\n      loss = self._compute_top_k_loss(cross_entropy_loss)\n    return loss\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/projects/panoptic/losses/panoptic_deeplab_losses.py#L44-L80","documentation":"Error \"Length of class_weights should be {}\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/projects/panoptic/losses/panoptic_deeplab_losses.py:62 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"}