keras-team/keras · error · ValueError
In `axis`, axis is out of bounds for array of dimension
Error message
In `axis`, axis {a} is out of bounds for array of dimension {result_dims} What it means
Bounds check in the JAX sparse backend's axis helper while resolving each entry of the axis argument: an index a must satisfy -result_dims <= a < result_dims, where result_dims is the input rank (plus len(axis) when insert_dims=True). Out-of-range indices (e.g. axis=3 on a 2D tensor) are rejected.
Solutions
- Use indices in [0, ndim) or the equivalent negative form [-ndim, 0)
- Print x.ndim (or the passed input_shape) and correct the axis values to fit that rank
- For expand_dims-style calls remember the valid range extends by the number of inserted dims
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/jax/sparse.py:43 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/ffaab03c48fda001.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/jax/sparse.py:43
- The broadcast dimensions: list of dimensions not in `axis`, for use as
the `broadcast_dimensions` argument of `broadcast_in_dim`.
"""
if axis is None:
raise ValueError("Received `None` value for `axis`")
if isinstance(axis, int):
axis = (axis,)
# Check uniqueness.
if len(set(axis)) != len(axis):
raise ValueError(f"Repeated axis in `axis`: {axis}")
result_dims = len(input_shape)
if insert_dims:
result_dims += len(axis)
# Resolve negative values.
canonical_axis = []
for a in axis:
if not -result_dims <= a < result_dims:
raise ValueError(
f"In `axis`, axis {a} is out of bounds for array "
f"of dimension {result_dims}"
)
if a < 0:
a = a + result_dims
canonical_axis.append(a)
# Check uniqueness again after resolving negative values.
if len(set(canonical_axis)) != len(canonical_axis):
raise ValueError(f"Repeated axis in `axis`: {canonical_axis}")
canonical_axis = sorted(canonical_axis)
# Compute output shape.
output_shape = list(input_shape)
for i in canonical_axis:
if insert_dims:
output_shape.insert(i, 1)
else:View on GitHub (pinned to 7a34a03db6)