{"record":{"id":"00159bbed7bd94f4","repo":"keras-team/keras","slug":"num-tokens-must-be-set-to-use-this-layer-if-the-n","errorCode":null,"errorMessage":"num_tokens must be set to use this layer. If the number of tokens is not known beforehand, use the IntegerLookup layer instead.","messagePattern":"num_tokens must be set to use this layer\\. If the number of tokens is not known beforehand, use the IntegerLookup layer instead\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/category_encoding.py","lineNumber":102,"sourceCode":"            weight for each sample value when summing up in `count` mode.\n            Not used in `\"multi_hot\"` or `\"one_hot\"` modes.\n    \"\"\"\n\n    def __init__(\n        self, num_tokens=None, output_mode=\"multi_hot\", sparse=False, **kwargs\n    ):\n        super().__init__(**kwargs)\n\n        # Support deprecated names for output_modes.\n        if output_mode == \"binary\":\n            output_mode = \"multi_hot\"\n\n        # 'output_mode' must be one of (\"count\", \"one_hot\", \"multi_hot\")\n        if output_mode not in (\"count\", \"one_hot\", \"multi_hot\"):\n            raise ValueError(f\"Unknown arg for output_mode: {output_mode}\")\n\n        if num_tokens is None:\n            raise ValueError(\n                \"num_tokens must be set to use this layer. If the \"\n                \"number of tokens is not known beforehand, use the \"\n                \"IntegerLookup layer instead.\"\n            )\n        if num_tokens < 1:\n            raise ValueError(\n                f\"`num_tokens` must be >= 1. Received: num_tokens={num_tokens}.\"\n            )\n        self.num_tokens = num_tokens\n        self.output_mode = output_mode\n        self.sparse = sparse\n        self._allow_non_tensor_positional_args = True\n        self._convert_input_args = False\n\n    def _encode(self, inputs, count_weights=None):\n        inputs = self.backend.core.convert_to_tensor(inputs)\n        return numerical_utils.encode_categorical_inputs(\n            inputs,","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/category_encoding.py#L84-L120","documentation":"CategoryEncoding requires num_tokens because it must know the output width for one-hot/multi-hot/count encoding, but it received None. Unlike lookup-based layers it cannot infer vocabulary size from data, so the layer refuses to construct without an explicit token count.","triggerScenarios":"Thrown at keras/src/layers/preprocessing/category_encoding.py:102 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass num_tokens explicitly, e.g. CategoryEncoding(num_tokens=1000)","If the vocabulary is unknown up front, use keras.layers.IntegerLookup instead, which adapts to data"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}