{"record":{"id":"7da9f6f637bfc5c3","repo":"keras-team/keras","slug":"when-output-mode-is-tf-idf-idf-weights-mu","errorCode":null,"errorMessage":"When `output_mode` is `'tf_idf'`, `idf_weights` must be provided.","messagePattern":"When `output_mode` is `'tf_idf'`, `idf_weights` must be provided\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/index_lookup.py","lineNumber":850,"sourceCode":"            )\n            idf_weights = (\n                self.idf_weights_const if self.output_mode == \"tf_idf\" else None\n            )\n            output = numerical_utils.encode_categorical_inputs(\n                lookups,\n                output_mode=(\n                    \"count\"\n                    if self.output_mode == \"tf_idf\"\n                    else self.output_mode\n                ),\n                depth=depth,\n                dtype=self._value_dtype,\n                sparse=self.sparse,\n                backend_module=tf_backend,\n            )\n            if self.output_mode == \"tf_idf\":\n                if idf_weights is None:\n                    raise ValueError(\n                        \"When `output_mode` is `'tf_idf'`, \"\n                        \"`idf_weights` must be provided.\"\n                    )\n                output = tf_backend.numpy.multiply(\n                    tf_backend.core.cast(output, idf_weights.dtype), idf_weights\n                )\n            return output\n\n    def _lookup_dense(self, inputs):\n        \"\"\"Lookup table values for a dense Tensor, handling masking and OOV.\"\"\"\n        # When executing eagerly and tracing keras.Input objects,\n        # do not call lookup.\n        # This is critical for restoring SavedModel, which will first trace\n        # layer.call and then attempt to restore the table. We need the table to\n        # be uninitialized for the restore to work, but calling the table\n        # uninitialized would error.\n        if tf.executing_eagerly() and backend.is_keras_tensor(inputs):\n            lookups = tf.zeros_like(inputs, dtype=self._value_dtype)","sourceCodeStart":832,"sourceCodeEnd":868,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/index_lookup.py#L832-L868","documentation":"When an IndexLookup layer has output_mode='tf_idf', the output must be scaled by IDF weights at call() time. The weights are not inferred from data on this path, so if idf_weights was never supplied to the layer, call() raises immediately.","triggerScenarios":"Calling layer(data) on a layer with output_mode='tf_idf' without having set idf_weights via set_vocabulary(vocab, idf_weights=...) or the constructor's idf_weights argument.","commonSituations":"Building a TF-IDF feature pipeline, adapting the vocabulary only, and forgetting the weights; loading a model where idf weights failed to serialize; converting a count-mode layer to tf_idf mode.","solutions":["Call set_vocabulary(vocabulary, idf_weights=weights) before invoking the layer","Pass idf_weights together with vocabulary at construction time","If you only need counts/indices, switch output_mode to 'count' or 'int'"],"exampleFix":"// before\nlayer = IndexLookup(output_mode='tf_idf', vocabulary=vocab)\nout = layer(x)\n// after\nlayer = IndexLookup(output_mode='tf_idf', vocabulary=vocab, idf_weights=idf)\nout = layer(x)","handlingStrategy":"validation","validationCode":"if layer.output_mode == 'tf_idf' and layer.idf_weights is None:\n    raise ValueError('set idf_weights before calling the layer')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pair vocabulary and idf_weights in set_vocabulary","Add a smoke-test layer(sample) call in pipeline setup code"],"tags":["keras","tf-idf","missing-argument","preprocessing"],"backgroundTag":"missing-required-parameter","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}