keras-team/keras · error · ValueError
All `HashedCrossing` inputs should be dense tensors…
Error message
All `HashedCrossing` inputs should be dense tensors. Received: inputs={inputs} What it means
Because HashedCrossing uses tf.sparse.cross, which only accepts dense tensors, this check in _check_input_shape_and_type fires when any input is a tf.RaggedTensor or tf.SparseTensor. Convert ragged or sparse inputs to dense first (e.g. tf.sparse.to_dense / tensor.to_tensor).
Solutions
- Convert ragged inputs with x.to_tensor() (possibly after trimming/padding to (batch,1))
- Convert sparse inputs with tf.sparse.to_dense(x)
- Move dense conversion upstream into the tf.data pipeline
Example fix
// before out = layer([ragged_a, b]) // after dense_a = ragged_a.to_tensor() out = layer([dense_a, b])
Defensive patterns
Strategy: validation
Validate before calling
inputs = [x.to_tensor() if hasattr(x, "to_tensor") else tf.sparse.to_dense(x) if isinstance(x, tf.SparseTensor) else x for x in inputs]
Type guard
def all_dense(inputs):
import tensorflow as tf
return not any(isinstance(x, (tf.RaggedTensor, tf.SparseTensor)) for x in inputs) Try / catch
catch ValueError from call() and densify inputs (x.to_tensor() / tf.sparse.to_dense(x)) before retrying
Prevention
- Convert RaggedTensor/SparseTensor inputs to dense with to_tensor()/tf.sparse.to_dense before HashedCrossing
- Do not pipe padded or ragged-batch datasets directly into a crossing layer
When it happens
Trigger: Any input x for which isinstance(x, (tf.RaggedTensor, tf.SparseTensor)) is true when the layer is called.
Common situations: Using ragged or sparse data pipelines (NLP token batches, padded sequences) directly with HashedCrossing without densifying.
Related errors
- All `HashedCrossing` inputs should have an integer or…
- All `HashedCrossing` inputs should have equal shape…
- All `HashedCrossing` inputs should have shape `()`…
- Expected as input a list/tuple of 2 tensors. Received…
- Expected the two input tensors to have identical shapes…
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/a235ad3d7fd3aea7.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/layers/preprocessing/hashed_crossing.py:218
def _check_input_shape_and_type(self, inputs):
first_shape = tuple(inputs[0].shape)
rank = len(first_shape)
if rank > 2 or (rank == 2 and first_shape[-1] != 1):
raise ValueError(
"All `HashedCrossing` inputs should have shape `()`, "
"`(batch_size)` or `(batch_size, 1)`. "
f"Received: inputs={inputs}"
)
if not all(tuple(x.shape) == first_shape for x in inputs[1:]):
raise ValueError(
"All `HashedCrossing` inputs should have equal shape. "
f"Received: inputs={inputs}"
)
if any(
isinstance(x, (tf.RaggedTensor, tf.SparseTensor)) for x in inputs
):
raise ValueError(
"All `HashedCrossing` inputs should be dense tensors. "
f"Received: inputs={inputs}"
)
if not all(
tf.as_dtype(x.dtype).is_integer or x.dtype == tf.string
for x in inputs
):
raise ValueError(
"All `HashedCrossing` inputs should have an integer or "
f"string dtype. Received: inputs={inputs}"
)
View on GitHub (pinned to 7a34a03db6)