keras-team/keras · error · ValueError
Expected input to have rank >= 2. Received input with shape
Error message
Expected input to have rank >= 2. Received input with shape {a.shape}. What it means
Keras 3 linalg ops (cholesky, cholesky_inverse, det, eig, eigh and their compute_output_spec paths) require matrices of rank >= 2. The internal _assert_2d helper in keras/src/ops/linalg.py checks every input tensor and raises this ValueError when any tensor has fewer than 2 dimensions, i.e. you passed a vector or scalar where a matrix (or batch of matrices) is required.
Source
Thrown at keras/src/ops/linalg.py:849
[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(
"Expected a square matrix. "
f"Received non-square input with shape {a.shape}"
)
def _assert_a_b_compat(a, b):
if a.ndim == b.ndim:
if a.shape[-2] != b.shape[-2]:View on GitHub (pinned to 7a34a03db6)
Solutions
- Reshape the input to at least rank 2: x = keras.ops.reshape(x, (1, -1)) or keras.ops.expand_dims(x, -1); for square-matrix ops wrap as (1, n, n).
- If x came from a layer that outputs rank-1, restructure the upstream layer so it emits (batch, features).
- Batch your matrices into a single (..., m, n) tensor rather than passing per-matrix vectors/scalars.
- Add a shape check before the call: if keras.ops.ndim(x) < 2: raise a clear error at your own boundary.
Example fix
// before import numpy as np from keras import ops w = np.array([1.0, 2.0, 3.0]) val = ops.det(w) # ValueError: rank 1 < 2 // after M = np.array([[2.0, 1.0], [1.0, 3.0]]) # a proper 2-D matrix val = ops.det(M)
Defensive patterns
Strategy: validation
Validate before calling
import keras
def as_2d(x):
if keras.ops.ndim(x) < 2:
x = keras.ops.expand_dims(x, -1) # or reshape to (1, n) as appropriate
return x
x = as_2d(x)
L = keras.ops.cholesky(x) Type guard
import keras
def is_rank2_plus(x) -> bool:
return getattr(x, "ndim", keras.ops.ndim(x)) >= 2 Prevention
- Log keras.ops.shape(x) before any linalg call (cholesky, det, eig, eigh).
- Standardize on (..., m, n) layout for all matrices entering linalg ops.
- Reshape foreign inputs explicitly at your API boundary instead of relying on implicit broadcasting.
When it happens
Trigger: Calling keras.ops.cholesky(x), keras.ops.cholesky_inverse(x), keras.ops.det(x), keras.ops.eig(x), or keras.ops.eigh(x) with a 0-D or 1-D tensor (e.g. shape (n,) instead of (n, n)); using these ops inside a functional Keras model where an upstream layer (Flatten, a squeeze, a Dense applied without a batch axis) produces rank-1 output.
Common situations: Feeding eigendecomposition or determinant ops a raw 1-D array; building a custom layer that calls linalg ops on activations; passing a single row-vector (n,) instead of a stacked matrix; symbolic shape inference in a functional Model where a previous layer collapsed dimensions.
Related errors
- Expected a square matrix. Received non-square input with sha
- Incompatible shapes between `a` and `b`. Expected `a.shape[-
- Incompatible shapes between `a` and `b`. Expected `a.shape[-
- Inputs to `cdist` must have rank >= 2. Received shapes: x.sh
- Input should have rank >= 1. Received: input.shape = {x.shap
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/597d7697c7ffaec2.
Report an issue: GitHub.