{"record":{"id":"9dcdc4d74d6520a7","repo":"keras-team/keras","slug":"the-num-bins-for-hashing-cannot-be-none-or-n","errorCode":null,"errorMessage":"The `num_bins` for `Hashing` cannot be `None` or non-positive values. Received: num_bins={num_bins}.","messagePattern":"The `num_bins` for `Hashing` cannot be `None` or non-positive values\\. Received: num_bins=(.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/hashing.py","lineNumber":161,"sourceCode":"        sparse=False,\n        **kwargs,\n    ):\n        if not tf.available:\n            raise ImportError(\n                \"Layer Hashing requires TensorFlow. \"\n                \"Install it via `pip install tensorflow`.\"\n            )\n\n        # By default, output int32 when output_mode='int' and floats otherwise.\n        if \"dtype\" not in kwargs or kwargs[\"dtype\"] is None:\n            kwargs[\"dtype\"] = (\n                \"int64\" if output_mode == \"int\" else backend.floatx()\n            )\n\n        super().__init__(**kwargs)\n\n        if num_bins is None or num_bins <= 0:\n            raise ValueError(\n                \"The `num_bins` for `Hashing` cannot be `None` or \"\n                f\"non-positive values. Received: num_bins={num_bins}.\"\n            )\n\n        if output_mode == \"int\" and (\n            self.dtype_policy.name not in (\"int32\", \"int64\")\n        ):\n            raise ValueError(\n                'When `output_mode=\"int\"`, `dtype` should be an integer '\n                f\"type, 'int32' or 'in64'. Received: dtype={kwargs['dtype']}\"\n            )\n\n        # 'output_mode' must be one of (INT, ONE_HOT, MULTI_HOT, COUNT)\n        accepted_output_modes = (\"int\", \"one_hot\", \"multi_hot\", \"count\")\n        if output_mode not in accepted_output_modes:\n            raise ValueError(\n                \"Invalid value for argument `output_mode`. \"\n                f\"Expected one of {accepted_output_modes}. \"","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/hashing.py#L143-L179","documentation":"Hashing maps values into num_bins buckets, so num_bins must be a positive integer. None, zero, or negative values make the hash space empty and are rejected at construction.","triggerScenarios":"Hashing(num_bins=0), Hashing(num_bins=-1), or Hashing(num_bins=None) — the constructor validates num_bins is a positive number.","commonSituations":"Passing num_bins=0 by mistake; deriving num_bins from data as len(set(x)) when the set is empty; config typo like num_bins=-1 or forgetting the argument (None).","solutions":["Set num_bins to a positive integer larger than expected cardinality (common practice: a power of two like 2**18)","Compute num_bins defensively: num_bins = max(1, estimated_cardinality)","Do not leave num_bins unset in custom wrappers that forward None"],"exampleFix":"// before\nlayer = Hashing(num_bins=len(vocab) - len(vocab))  # evaluates to 0\n// after\nlayer = Hashing(num_bins=max(2**18, len(vocab)))","handlingStrategy":"validation","validationCode":"assert isinstance(num_bins, int) and num_bins > 0, \"num_bins must be a positive int\"","typeGuard":"def valid_num_bins(n):\n    return isinstance(n, int) and n > 0","tryCatchPattern":"catch ValueError from Hashing.__init__ and correct num_bins before constructing again","preventionTips":["Set num_bins to a positive integer (e.g. 2**18 for high-cardinality hashing)","Size num_bins above expected cardinality to limit collisions"],"tags":["keras","hashing","num-bins","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}