{"record":{"id":"e9b738643634eb15","repo":"tensorflow/models","slug":"length-of-class-weights-should-be-e9b738","errorCode":null,"errorMessage":"Length of class_weights should be {}","messagePattern":"Length of class_weights should be (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/vision/losses/maskrcnn_losses.py","lineNumber":197,"sourceCode":"\n    Args:\n      class_outputs: a float tensor representing the class prediction for each\n        box with a shape of [batch_size, num_boxes, num_classes].\n      class_targets: a float tensor representing the class label for each box\n        with a shape of [batch_size, num_boxes].\n      class_weights: A float list containing the weight of each class.\n\n    Returns:\n      a scalar tensor representing total class loss.\n    \"\"\"\n    with tf.name_scope('fast_rcnn_loss'):\n      output_dtype = class_outputs.dtype\n      num_classes = class_outputs.get_shape().as_list()[-1]\n      class_weights = (\n          class_weights if class_weights is not None else [1.0] * num_classes\n      )\n      if num_classes != len(class_weights):\n        raise ValueError(\n            'Length of class_weights should be {}'.format(num_classes)\n        )\n\n      class_weights = tf.constant(class_weights, dtype=output_dtype)\n\n      class_targets_one_hot = tf.one_hot(\n          tf.cast(class_targets, dtype=tf.int32),\n          num_classes,\n          dtype=class_outputs.dtype)\n      if self._use_binary_cross_entropy:\n        # (batch_size, num_boxes, num_classes)\n        cross_entropy_loss = tf.nn.sigmoid_cross_entropy_with_logits(\n            labels=class_targets_one_hot, logits=class_outputs)\n        cross_entropy_loss *= class_weights\n      else:\n        # (batch_size, num_boxes)\n        cross_entropy_loss = tf.nn.softmax_cross_entropy_with_logits(\n            labels=class_targets_one_hot, logits=class_outputs)","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/vision/losses/maskrcnn_losses.py#L179-L215","documentation":"Error \"Length of class_weights should be {}\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/vision/losses/maskrcnn_losses.py:197 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"}