keras-team/keras · error · ValueError
`sparse=True` is not supported with numpy backend
Error message
`sparse=True` is not supported with numpy backend
What it means
`keras.ops.convert_to_tensor` in the NumPy backend raises this whenever it is asked to build a sparse tensor (`sparse=True`). NumPy has no native sparse array representation, so the sparse path of the backend-agnostic op is unsupported here; this guard rejects the request up front and is purely a backend capability limitation, not a data error.
Solutions
- Convert with `sparse=None`/`sparse=False` to get a dense NumPy array
- Use a backend with sparse tensor support (TensorFlow or JAX) if sparse inputs are essential
- Materialize sparse data to dense beforehand (e.g. via scipy.sparse .toarray()) when memory allows
Defensive patterns
Strategy: fallback
When it happens
Trigger: Thrown at keras/src/backend/numpy/ops/core.py:35 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/f8fbb4e3118cae2c.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/numpy/ops/core.py:35
class Variable(KerasVariable):
def _initialize(self, value):
self._value = value
def _direct_assign(self, value):
self._value = np.array(value, dtype=self._dtype)
def _convert_to_tensor(self, value, dtype=None):
return convert_to_tensor(value, dtype=dtype)
# Overload native accessor.
def __array__(self):
return self.value
def convert_to_tensor(x, dtype=None, sparse=None, ragged=None):
if sparse:
raise ValueError("`sparse=True` is not supported with numpy backend")
if ragged:
raise ValueError("`ragged=True` is not supported with numpy backend")
if dtype is not None:
dtype = standardize_dtype(dtype)
if isinstance(x, Variable):
if dtype and dtype != x.dtype:
return x.value.astype(dtype)
return x.value
if not is_tensor(x) and standardize_dtype(dtype) == "bfloat16":
# Can't create bfloat16 arrays on the fly (e.g. from a h5 Dataset).
# Instead we convert "as is" (to stored dtype) and cast.
return np.asarray(x).astype(dtype)
if dtype is None:
dtype = result_type(
*[getattr(item, "dtype", type(item)) for item in tree.flatten(x)]
)
return np.array(x, dtype=dtype)
View on GitHub (pinned to 7a34a03db6)