keras-team/keras · error · ValueError
All `axis` values must be in the range
Error message
All `axis` values must be in the range [-ndim, ndim). Received inputs with ndim={ndim}, while axis={axis} What it means
Raised by the axis-validation guard in keras/src/backend/tensorflow/linalg.py norm: every axis value must lie in the half-open range [-ndim, ndim) of the input tensor. At least one axis value is out of that range (e.g. axis=3 for a 2D tensor), which would make the reduction dimension undefined, so the guard fails before computing the norm.
Solutions
- Use axis values within [-x.ndim, x.ndim), e.g. axis=(-1, -2) for the last two dims of a matrix
- Remember negative indices count from the last axis; prefer -1 instead of ndim-1
- Validate axis against x.shape.rank before calling norm
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/tensorflow/linalg.py:62 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/8d92642918bd5587.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/tensorflow/linalg.py:62
def lu_factor(a):
lu, p = tf.linalg.lu(a)
return lu, tf.math.invert_permutation(p)
def norm(x, ord=None, axis=None, keepdims=False):
from keras.src.backend.tensorflow.numpy import moveaxis
x = convert_to_tensor(x)
x_shape = x.shape
ndim = x_shape.rank
if axis is None:
axis = tuple(range(ndim))
elif isinstance(axis, int):
axis = (axis,)
if any(a < -ndim or a >= ndim for a in axis):
raise ValueError(
"All `axis` values must be in the range [-ndim, ndim). "
f"Received inputs with ndim={ndim}, while axis={axis}"
)
axis = axis[0] if len(axis) == 1 else axis
num_axes = 1 if isinstance(axis, int) else len(axis)
if standardize_dtype(x.dtype) == "int64":
dtype = config.floatx()
else:
dtype = dtypes.result_type(x.dtype, float)
x = cast(x, dtype)
# Ref: jax.numpy.linalg.norm
if num_axes == 1:
if ord is None or ord == 2:
return tf.sqrt(
tf.reduce_sum(x * tf.math.conj(x), axis=axis, keepdims=keepdims)
)View on GitHub (pinned to 7a34a03db6)