{"record":{"id":"8afadb09e03b9fd1","repo":"keras-team/keras","slug":"fbetascore-expects-2d-inputs-with-shape-batch-siz","errorCode":null,"errorMessage":"FBetaScore expects 2D inputs with shape (batch_size, output_dim). 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\\)\\. 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":124,"sourceCode":"            if threshold > 1.0 or threshold <= 0.0:\n                raise ValueError(\n                    \"Invalid `threshold` argument value. \"\n                    \"It should verify 0 < threshold <= 1. \"\n                    f\"Received: threshold={threshold}\"\n                )\n\n        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 = ()","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/metrics/f_score_metrics.py#L106-L142","documentation":"Raised from FBetaScore._build during update_state when y_pred or y_true is not rank 2. FBetaScore operates on one-hot/probability matrices of shape (batch_size, output_dim); rank-1 binary vectors or rank-3 tensors are rejected.","triggerScenarios":"Passing rank-1 y_true=[1,0,1,1] and y_pred=[0.8,0.2,0.9,0.7] to update_state; a model with rank-1 output; 3D sequence outputs without reshaping.","commonSituations":"Binary classification with single-column outputs; forgetting one-hot/to_categorical on integer labels; time-series outputs not flattened per timestep.","solutions":["Expand dims for binary cases: keras.ops.expand_dims(y, -1).","One-hot encode integer labels: keras.ops.one_hot(y_true, num_classes).","End the model with Dense(num_classes, activation='softmax'/'sigmoid') so predictions are (batch, num_classes)."],"exampleFix":"# before\nm.update_state(y_true, y_pred)  # both rank 1\n\n# after\nimport keras.ops as ops\nm.update_state(ops.expand_dims(y_true, -1), ops.expand_dims(y_pred, -1))\n# or for multiclass:\nm.update_state(ops.one_hot(y_true, num_classes), y_pred)","handlingStrategy":"validation","validationCode":"import keras.ops as ops\nif len(y_pred.shape) != 2:\n    y_pred = ops.expand_dims(y_pred, -1)\nif len(y_true.shape) != 2:\n    y_true = ops.expand_dims(y_true, -1)","typeGuard":"def are_rank2(*ts) -> bool:\n    return all(len(t.shape) == 2 for t in ts)","tryCatchPattern":null,"preventionTips":["One-hot encode integer labels before update_state.","Smoke-test metrics with one batch right after model.compile."],"tags":["keras","metrics","fbeta","shape-validation"],"backgroundTag":"tensor-rank-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}