keras-team/keras · error · ValueError
`sparse=True` cannot be used with backend
Error message
`sparse=True` cannot be used with backend {backend.backend()} What it means
Discretization was constructed with sparse=True on a backend that does not support sparse tensors (detected via backend.SUPPORTS_SPARSE_TENSORS, false for numpy and openvino). Sparse output modes only work on the TensorFlow (and other sparse-aware) backends, so the combination is rejected at construction.
Solutions
- Set sparse=False when using the numpy or openvino backend
- Switch to the TensorFlow backend (KERAS_BACKEND=tensorflow) if sparse outputs are required
Defensive patterns
Strategy: fallback
When it happens
Trigger: Thrown at keras/src/layers/preprocessing/discretization.py:102 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/0bf2575012aca6b5.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/layers/preprocessing/discretization.py:102
>>> layer(input)
array([[0, 2, 3, 2],
[1, 3, 3, 1]])
"""
def __init__(
self,
bin_boundaries=None,
num_bins=None,
epsilon=0.01,
output_mode="int",
sparse=False,
dtype=None,
name=None,
):
super().__init__(name=name, dtype=dtype)
if sparse and not backend.SUPPORTS_SPARSE_TENSORS:
raise ValueError(
f"`sparse=True` cannot be used with backend {backend.backend()}"
)
if sparse and output_mode == "int":
raise ValueError(
"`sparse=True` may only be used if `output_mode` is "
"`'one_hot'`, `'multi_hot'`, or `'count'`. "
f"Received: sparse={sparse} and "
f"output_mode={output_mode}"
)
argument_validation.validate_string_arg(
output_mode,
allowable_strings=(
"int",
"one_hot",
"multi_hot",
"count",
),View on GitHub (pinned to 7a34a03db6)