keras-team/keras · error · ValueError

When `output_mode` is `'tf_idf'`, `idf_weights` must be prov

Error message

When `output_mode` is `'tf_idf'`, `idf_weights` must be provided.

What it means

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.

Source

Thrown at keras/src/layers/preprocessing/index_lookup.py:850

            )
            idf_weights = (
                self.idf_weights_const if self.output_mode == "tf_idf" else None
            )
            output = numerical_utils.encode_categorical_inputs(
                lookups,
                output_mode=(
                    "count"
                    if self.output_mode == "tf_idf"
                    else self.output_mode
                ),
                depth=depth,
                dtype=self._value_dtype,
                sparse=self.sparse,
                backend_module=tf_backend,
            )
            if self.output_mode == "tf_idf":
                if idf_weights is None:
                    raise ValueError(
                        "When `output_mode` is `'tf_idf'`, "
                        "`idf_weights` must be provided."
                    )
                output = tf_backend.numpy.multiply(
                    tf_backend.core.cast(output, idf_weights.dtype), idf_weights
                )
            return output

    def _lookup_dense(self, inputs):
        """Lookup table values for a dense Tensor, handling masking and OOV."""
        # When executing eagerly and tracing keras.Input objects,
        # do not call lookup.
        # This is critical for restoring SavedModel, which will first trace
        # layer.call and then attempt to restore the table. We need the table to
        # be uninitialized for the restore to work, but calling the table
        # uninitialized would error.
        if tf.executing_eagerly() and backend.is_keras_tensor(inputs):
            lookups = tf.zeros_like(inputs, dtype=self._value_dtype)

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Call set_vocabulary(vocabulary, idf_weights=weights) before invoking the layer
  2. Pass idf_weights together with vocabulary at construction time
  3. If you only need counts/indices, switch output_mode to 'count' or 'int'

Example fix

// before
layer = IndexLookup(output_mode='tf_idf', vocabulary=vocab)
out = layer(x)
// after
layer = IndexLookup(output_mode='tf_idf', vocabulary=vocab, idf_weights=idf)
out = layer(x)
Defensive patterns

Strategy: validation

Validate before calling

if layer.output_mode == 'tf_idf' and layer.idf_weights is None:
    raise ValueError('set idf_weights before calling the layer')

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/7da9f6f637bfc5c3. Report an issue: GitHub.