{"record":{"id":"15e54f1c2606e445","repo":"keras-team/keras","slug":"fbetascore-expects-2d-inputs-with-shape-batch-siz-15e54f","errorCode":null,"errorMessage":"FBetaScore expects 2D inputs with shape (batch_size, output_dim), with output_dim fully defined (not None). Received input shapes: y_pred.shape={y_pred_shape} and y_true.shape={y_true_shape}.","messagePattern":"FBetaScore expects 2D inputs with shape \\(batch_size, output_dim\\), with output_dim fully defined \\(not None\\)\\. Received input shapes: y_pred\\.shape=(.+?) and y_true\\.shape=(.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/metrics/f_score_metrics.py","lineNumber":131,"sourceCode":"        self.average = average\n        self.beta = beta\n        self.threshold = threshold\n        self.axis = None\n        self._built = False\n\n        if self.average != \"micro\":\n            self.axis = 0\n\n    def _build(self, y_true_shape, y_pred_shape):\n        if len(y_pred_shape) != 2 or len(y_true_shape) != 2:\n            raise ValueError(\n                \"FBetaScore expects 2D inputs with shape \"\n                \"(batch_size, output_dim). Received input \"\n                f\"shapes: y_pred.shape={y_pred_shape} and \"\n                f\"y_true.shape={y_true_shape}.\"\n            )\n        if y_pred_shape[-1] is None or y_true_shape[-1] is None:\n            raise ValueError(\n                \"FBetaScore expects 2D inputs with shape \"\n                \"(batch_size, output_dim), with output_dim fully \"\n                \"defined (not None). Received input \"\n                f\"shapes: y_pred.shape={y_pred_shape} and \"\n                f\"y_true.shape={y_true_shape}.\"\n            )\n        num_classes = y_pred_shape[-1]\n        if self.average != \"micro\":\n            init_shape = (num_classes,)\n        else:\n            init_shape = ()\n\n        def _add_zeros_variable(name):\n            return self.add_variable(\n                name=name,\n                shape=init_shape,\n                initializer=initializers.Zeros(),\n                dtype=self.dtype,","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/metrics/f_score_metrics.py#L113-L149","documentation":"Raised from FBetaScore._build during update_state when inputs are rank 2 but the last dimension (output_dim / num_classes) is None, i.e. not statically known. The metric must allocate per-class state variables, so it needs a concrete class count.","triggerScenarios":"Calling update_state inside tf.function/JAX jit traced with dynamic output shapes; a Functional model whose output shape is (batch, None); symbolic tensors with unspecified last dim.","commonSituations":"Custom train steps traced with jit; XLA/jit dynamic axes; building metrics from symbolic tensors instead of model.compile.","solutions":["Pass tensors with statically-known last dim (e.g. TensorSpec([None, num_classes])).","Build the metric with a fixed shape or update it outside jit-traced code on concrete tensors.","Prefer model.compile(metrics=[...]) so Keras builds metrics from the model's output shape."],"exampleFix":"# before\n@tf.function\ndef step(x, y):\n    m.update_state(y, model(x))  # output dim unknown under trace\n\n# after\nm = keras.metrics.FBetaScore(beta=1.0, average='macro')\nm.build((None, 10), (None, 10))  # fix the class dim, update on concrete tensors","handlingStrategy":"validation","validationCode":"assert y_pred.shape[-1] is not None and y_true.shape[-1] is not None, 'output_dim must be static'","typeGuard":"def has_static_last_dim(t) -> bool:\n    return t.shape[-1] is not None","tryCatchPattern":null,"preventionTips":["Give TensorSpecs explicit last dims when tracing.","Let model.compile build metrics rather than calling update_state inside jit."],"tags":["keras","metrics","fbeta","shape-validation","static-shape"],"backgroundTag":"undefined-tensor-dimension","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}