keras-team/keras · error · ValueError
axis is out of bounds for an array with dimension .
Error message
axis {axis} is out of bounds for an array with dimension {num_dims}. What it means
Guard in canonicalize_axis, which maps a user-supplied axis from the negative-index convention [-num_dims, 0) into [0, num_dims). The check fails when the axis lies outside that half-open interval, i.e. the tensor does not have that many dimensions to index.
Solutions
- Use an axis in the range [-num_dims, num_dims); negative values count from the last dimension.
- Pass a Python int (e.g. via operator.index) rather than a float or numpy scalar like 1.0.
- Reshape or expand dims of the tensor first if the intended axis exceeds its rank.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/common/backend_utils.py:352 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/12ee88358d5445c7.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/common/backend_utils.py:352
strides=strides[i],
padding=padding,
output_padding=current_output_padding,
dilation_rate=dilation_rate[i],
)
output_shape.append(shape_i)
if data_format == "channels_last":
output_shape = [input_shape[0]] + output_shape + [filters]
else:
output_shape = [input_shape[0], filters] + output_shape
return output_shape
def canonicalize_axis(axis, num_dims):
"""Canonicalize an axis in [-num_dims, num_dims) to [0, num_dims)."""
axis = operator.index(axis)
if not -num_dims <= axis < num_dims:
raise ValueError(
f"axis {axis} is out of bounds for an array with dimension "
f"{num_dims}."
)
if axis < 0:
axis = axis + num_dims
return axis
def canonicalize_axes(axis, num_dims):
"""Canonicalize an axis or axes to a tuple of non-negative integers."""
if isinstance(axis, (tuple, list)):
return tuple(canonicalize_axis(a, num_dims) for a in axis)
return (canonicalize_axis(axis, num_dims),)
def standardize_axis_for_numpy(axis):
"""Standardize an axis to a tuple if it is a list in the numpy backend."""
return tuple(axis) if isinstance(axis, list) else axisView on GitHub (pinned to 7a34a03db6)