{"record":{"id":"597d7697c7ffaec2","repo":"keras-team/keras","slug":"expected-input-to-have-rank-2-received-input-w-597d76","errorCode":null,"errorMessage":"Expected input to have rank >= 2. Received input with shape {a.shape}.","messagePattern":"Expected input to have rank >= 2\\. Received input with shape (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/linalg.py","lineNumber":849,"sourceCode":"           [0., 1.]], dtype=float32)\n    \"\"\"\n    if any_symbolic_tensors((x,)):\n        return Pinv(rcond=rcond).symbolic_call(x)\n    return backend.linalg.pinv(x, rcond=rcond)\n\n\ndef _assert_1d(*arrays):\n    for a in arrays:\n        if a.ndim < 1:\n            raise ValueError(\n                f\"Expected input to have rank >= 1. Received scalar input {a}.\"\n            )\n\n\ndef _assert_2d(*arrays):\n    for a in arrays:\n        if a.ndim < 2:\n            raise ValueError(\n                \"Expected input to have rank >= 2. \"\n                f\"Received input with shape {a.shape}.\"\n            )\n\n\ndef _assert_square(*arrays):\n    for a in arrays:\n        m, n = a.shape[-2:]\n        if m != n:\n            raise ValueError(\n                \"Expected a square matrix. \"\n                f\"Received non-square input with shape {a.shape}\"\n            )\n\n\ndef _assert_a_b_compat(a, b):\n    if a.ndim == b.ndim:\n        if a.shape[-2] != b.shape[-2]:","sourceCodeStart":831,"sourceCodeEnd":867,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/linalg.py#L831-L867","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nimport numpy as np\nfrom keras import ops\nw = np.array([1.0, 2.0, 3.0])\nval = ops.det(w)  # ValueError: rank 1 < 2\n\n// after\nM = np.array([[2.0, 1.0], [1.0, 3.0]])  # a proper 2-D matrix\nval = ops.det(M)","handlingStrategy":"validation","validationCode":"import keras\n\ndef as_2d(x):\n    if keras.ops.ndim(x) < 2:\n        x = keras.ops.expand_dims(x, -1)  # or reshape to (1, n) as appropriate\n    return x\n\nx = as_2d(x)\nL = keras.ops.cholesky(x)","typeGuard":"import keras\n\ndef is_rank2_plus(x) -> bool:\n    return getattr(x, \"ndim\", keras.ops.ndim(x)) >= 2","tryCatchPattern":null,"preventionTips":["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."],"tags":["keras","linalg","shape-validation","rank-error","matrix-ops"],"backgroundTag":"tensor-rank-or-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}