keras-team/keras · error · ValueError
Unsupported value `sparse=True` with numpy backend
Error message
Unsupported value `sparse=True` with numpy backend
What it means
`keras.ops.bincount` in the NumPy backend only produces dense count arrays; NumPy has no sparse-array container, so the `sparse=True` option (useful on backends with sparse tensor support) is unconditionally rejected by this guard. It signals a backend capability limitation, not a problem with the input values themselves.
Solutions
- Call bincount with `sparse=False` (the default) and accept a dense output array
- Switch to the TensorFlow or JAX backend if sparse count tensors are required
- Post-process the dense counts if memory allows
Defensive patterns
Strategy: fallback
When it happens
Trigger: Thrown at keras/src/backend/numpy/numpy.py:365 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/db8ac1daf8bbf8c4.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/numpy/numpy.py:365
x2 = convert_to_tensor(x2)
dtype = dtypes.result_type(x1.dtype, x2.dtype)
if dtype in ["int8", "int16", "int32", "uint8", "uint16", "uint32"]:
dtype = config.floatx()
elif dtype in ["int64"]:
dtype = "float64"
return np.heaviside(x1, x2).astype(dtype)
def kaiser(x, beta):
x = convert_to_tensor(x)
return np.kaiser(x, beta).astype(config.floatx())
def bincount(x, weights=None, minlength=0, sparse=False):
if sparse:
raise ValueError("Unsupported value `sparse=True` with numpy backend")
x = convert_to_tensor(x)
dtypes_to_resolve = [x.dtype]
if weights is not None:
weights = convert_to_tensor(weights)
dtypes_to_resolve.append(weights.dtype)
dtype = dtypes.result_type(*dtypes_to_resolve)
else:
dtype = "int32"
if len(x.shape) == 2:
if weights is None:
def bincount_fn(arr):
return np.bincount(arr, minlength=minlength)
bincounts = list(map(bincount_fn, x))
else:
def bincount_fn(arr_w):View on GitHub (pinned to 7a34a03db6)