keras-team/keras · error · ValueError
Expected input to have rank >= 1. Received scalar input
Error message
Expected input to have rank >= 1. Received scalar input {a}. What it means
Generic guard raised by the _assert_1d helper in keras/src/ops/linalg.py: one of the arrays passed to it has ndim < 1, i.e. it is a 0-D scalar, so the operation expecting at least rank-1 input (e.g. norm or similar vectorized linalg op) cannot proceed. The at-fault input is named in the message.
Solutions
- Wrap the scalar in a length-1 array, e.g. keras.ops.convert_to_tensor([x]) or np.atleast_1d(x)
- Check the calling code for accidental use of x.item() or x.squeeze() before the call
- Verify the tensor was not reduced to a scalar by an unintended reduction op
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/ops/linalg.py:841 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/640a08ce07ed8f05.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/ops/linalg.py:841
A tensor of shape `(..., N, M)` containing the pseudoinverse
of each matrix in `x`.
Example:
>>> a = keras.ops.convert_to_tensor([[1., 2.], [3., 4.], [5., 6.]])
>>> keras.ops.pinv(a) @ a
array([[1., 0.],
[0., 1.]], dtype=float32)
"""
if any_symbolic_tensors((x,)):
return Pinv(rcond=rcond).symbolic_call(x)
return backend.linalg.pinv(x, rcond=rcond)
def _assert_1d(*arrays):
for a in arrays:
if a.ndim < 1:
raise ValueError(
f"Expected input to have rank >= 1. Received scalar input {a}."
)
def _assert_2d(*arrays):
for a in arrays:
if a.ndim < 2:
raise ValueError(
"Expected input to have rank >= 2. "
f"Received input with shape {a.shape}."
)
def _assert_square(*arrays):
for a in arrays:
m, n = a.shape[-2:]
if m != n:
raise ValueError(View on GitHub (pinned to 7a34a03db6)