keras-team/keras · error · ValueError

`idf_weights` should only be set if `output_mode` is `'tf_id

Error message

`idf_weights` should only be set if `output_mode` is `'tf_idf'`. Received: idf_weights={idf_weights} and output_mode={output_mode}

What it means

`idf_weights` are per-token inverse-document-frequency scaling factors, which only exist in TF-IDF output mode. Supplying them with any other output_mode is rejected at construction.

Source

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

            arg_name="output_mode",
        )

        if invert and output_mode != "int":
            raise ValueError(
                "`output_mode` must be `'int'` when `invert` is true. "
                f"Received: output_mode={output_mode}"
            )

        if sparse and output_mode == "int":
            raise ValueError(
                "`sparse` may only be true if `output_mode` is "
                "`'one_hot'`, `'multi_hot'`, `'count'` or `'tf_idf'`. "
                f"Received: sparse={sparse} and "
                f"output_mode={output_mode}"
            )

        if idf_weights is not None and output_mode != "tf_idf":
            raise ValueError(
                "`idf_weights` should only be set if `output_mode` is "
                f"`'tf_idf'`. Received: idf_weights={idf_weights} and "
                f"output_mode={output_mode}"
            )

        super().__init__(name=name)
        self._convert_input_args = False
        self._allow_non_tensor_positional_args = True
        self.supports_jit = False

        self.invert = invert
        self.max_tokens = max_tokens
        self.num_oov_indices = num_oov_indices
        self.mask_token = mask_token
        self.oov_token = oov_token
        self.output_mode = output_mode
        self.sparse = sparse
        self.pad_to_max_tokens = pad_to_max_tokens

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Set `output_mode='tf_idf'` if you want idf weighting.
  2. Or remove the `idf_weights` argument.

Example fix

# before
layer = IndexLookup(vocabulary=vocab, idf_weights=idf)

# after
layer = IndexLookup(vocabulary=vocab, idf_weights=idf, output_mode='tf_idf')
Defensive patterns

Strategy: validation

Validate before calling

if idf_weights is not None and output_mode != 'tf_idf':
    raise ValueError('idf_weights requires output_mode=tf_idf')

Prevention

When it happens

Trigger: `IndexLookup(vocabulary=vocab, idf_weights=w)` while output_mode is 'int' (default), 'multi_hot', 'count' or 'one_hot'.

Common situations: Reusing a TF-IDF layer's full argument set after switching output_mode, or loading idf weights from a fitted sklearn TfidfVectorizer into a count-mode layer.

Related errors


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