keras-team/keras · error · ValueError
`sparse` may only be true if `output_mode` is `"one_hot"`, `
Error message
`sparse` may only be true if `output_mode` is `"one_hot"`, `"multi_hot"`, or `"count"`. Received: sparse={sparse} and output_mode={output_mode} What it means
Hashing can only return a sparse tensor for modes that produce vector outputs per sample ('one_hot', 'multi_hot', 'count'). With output_mode='int' each input maps to a scalar bucket index, so a sparse representation is meaningless and the layer rejects sparse=True in that combination.
Source
Thrown at keras/src/layers/preprocessing/hashing.py:184
if output_mode == "int" and (
self.dtype_policy.name not in ("int32", "int64")
):
raise ValueError(
'When `output_mode="int"`, `dtype` should be an integer '
f"type, 'int32' or 'in64'. Received: dtype={kwargs['dtype']}"
)
# 'output_mode' must be one of (INT, ONE_HOT, MULTI_HOT, COUNT)
accepted_output_modes = ("int", "one_hot", "multi_hot", "count")
if output_mode not in accepted_output_modes:
raise ValueError(
"Invalid value for argument `output_mode`. "
f"Expected one of {accepted_output_modes}. "
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"`, or `"count"`. '
f"Received: sparse={sparse} and "
f"output_mode={output_mode}"
)
self.num_bins = num_bins
self.mask_value = mask_value
self.strong_hash = True if salt is not None else False
self.output_mode = output_mode
self.sparse = sparse
self.salt = None
if salt is not None:
if isinstance(salt, (tuple, list)) and len(salt) == 2:
self.salt = list(salt)
elif isinstance(salt, int):
self.salt = [salt, salt]
else:View on GitHub (pinned to 7a34a03db6)
Solutions
- Drop sparse=True (or set sparse=False) when output_mode='int'.
- If you wanted sparse output, switch output_mode to 'one_hot', 'multi_hot', or 'count'.
Example fix
# before layer = keras.layers.Hashing(num_bins=64, output_mode="int", sparse=True) # after layer = keras.layers.Hashing(num_bins=64, output_mode="int")
Defensive patterns
Strategy: validation
Validate before calling
sparse_ok = sparse and output_mode != "int" layer = Hashing(num_bins=n, output_mode=output_mode, sparse=sparse_ok)
Prevention
- Centralize sparse=True in one config flag and assert output_mode != 'int' when it is set.
When it happens
Trigger: keras.layers.Hashing(num_bins=N, output_mode='int', sparse=True).
Common situations: Copy-pasting sparse=True from a one_hot/multi_hot pipeline into a Hashing layer configured for integer output; enabling sparse globally for memory savings without checking mode compatibility.
Related errors
- Invalid value for argument `output_mode`. Expected one of {a
- The `salt` argument for `Hashing` can only be a tuple of siz
- `salt` can only be used when `oov_method='farmhash'`. Receiv
- `sparse` may only be true if `output_mode` is `'one_hot'`, `
- `sparse=True` can only be used with the TensorFlow backend.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/2152a41fb04d179c.
Report an issue: GitHub.