{"record":{"id":"c5b11b328e178fc5","repo":"keras-team/keras","slug":"invalid-value-for-argument-num-regressors-expec","errorCode":null,"errorMessage":"Invalid value for argument `num_regressors`. Expected a value >= 0. Received: num_regressors={num_regressors}","messagePattern":"Invalid value for argument `num_regressors`\\. Expected a value >= 0\\. Received: num_regressors=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/metrics/regression_metrics.py","lineNumber":427,"sourceCode":"        dtype=None,\n    ):\n        super().__init__(name=name, dtype=dtype)\n        # Metric should be maximized during optimization.\n        self._direction = \"up\"\n\n        valid_class_aggregation_values = (\n            None,\n            \"uniform_average\",\n            \"variance_weighted_average\",\n        )\n        if class_aggregation not in valid_class_aggregation_values:\n            raise ValueError(\n                \"Invalid value for argument `class_aggregation`. Expected \"\n                f\"one of {valid_class_aggregation_values}. \"\n                f\"Received: class_aggregation={class_aggregation}\"\n            )\n        if num_regressors < 0:\n            raise ValueError(\n                \"Invalid value for argument `num_regressors`. \"\n                \"Expected a value >= 0. \"\n                f\"Received: num_regressors={num_regressors}\"\n            )\n        self.class_aggregation = class_aggregation\n        self.num_regressors = num_regressors\n        self.num_samples = self.add_variable(\n            shape=(),\n            initializer=initializers.Zeros(),\n            name=\"num_samples\",\n        )\n        self._built = False\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                \"R2Score expects 2D inputs with shape \"\n                \"(batch_size, output_dim). Received input \"","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/metrics/regression_metrics.py#L409-L445","documentation":"R2Score requires num_regressors (the predictor count used for adjusted R-squared) to be a non-negative integer or None. Passing a negative value raises this ValueError in __init__. Pass None to get plain, non-adjusted R-squared.","triggerScenarios":"keras.metrics.R2Score(num_regressors=-1) or any negative number, often from a config default of -1 meaning 'unset'.","commonSituations":"Config systems that use -1 as a sentinel for 'not configured'; arithmetic computing num_regressors from dataset dimensions that underflows.","solutions":["Pass num_regressors=None if you do not need adjusted R-squared.","Otherwise pass the actual input feature count, e.g. num_regressors=X.shape[1].","Map config sentinels like -1 to None before constructing the metric."],"exampleFix":"# before\nmetric = keras.metrics.R2Score(num_regressors=-1)\n\n# after\nmetric = keras.metrics.R2Score(num_regressors=X_train.shape[1])\n# or keras.metrics.R2Score() for plain R2","handlingStrategy":"validation","validationCode":"if num_regressors is not None:\n    assert isinstance(num_regressors, int) and num_regressors >= 0, 'num_regressors must be >= 0 or None'","typeGuard":"def is_valid_num_regressors(v) -> bool:\n    return v is None or (isinstance(v, int) and v >= 0)","tryCatchPattern":null,"preventionTips":["Map config sentinels like -1 to None before constructing R2Score."],"tags":["keras","metrics","r2-score","argument-out-of-range","regression"],"backgroundTag":"argument-out-of-range","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}