{"record":{"id":"d147b2f4e034741f","repo":"keras-team/keras","slug":"cholesky-inverse-failed-e","errorCode":null,"errorMessage":"Cholesky inverse failed: {e}","messagePattern":"Cholesky inverse failed: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/linalg.py","lineNumber":94,"sourceCode":"    Returns:\n        A tensor of shape `(..., M, M)` representing the inverse of `x`.\n\n    Raises:\n        ValueError: If `x` is not a symmetric positive-definite matrix.\n    \"\"\"\n    if any_symbolic_tensors((x,)):\n        return CholeskyInverse(upper=upper).symbolic_call(x)\n    return _cholesky_inverse(x, upper=upper)\n\n\ndef _cholesky_inverse(x, upper=False):\n    x = backend.convert_to_tensor(x)\n    _assert_2d(x)\n    _assert_square(x)\n    try:\n        return backend.linalg.cholesky_inverse(x, upper=upper)\n    except Exception as e:\n        raise ValueError(f\"Cholesky inverse failed: {e}\")\n\n\nclass Det(Operation):\n    def call(self, x):\n        return _det(x)\n\n    def compute_output_spec(self, x):\n        _assert_2d(x)\n        _assert_square(x)\n        return KerasTensor(x.shape[:-2], x.dtype)\n\n\n@keras_export([\"keras.ops.det\", \"keras.ops.linalg.det\"])\ndef det(x):\n    \"\"\"Computes the determinant of a square tensor.\n\n    Args:\n        x: Input tensor of shape `(..., M, M)`.","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/linalg.py#L76-L112","documentation":"cholesky_inverse validates the input then calls backend.linalg.cholesky_inverse; backend failures — most often a matrix that is not a valid Cholesky factor (not lower/upper triangular per the upper flag, or from a non-SPD source) — are re-raised under this wrapper message.","triggerScenarios":"keras.ops.linalg.cholesky_inverse(L) where L is a full (non-triangular) matrix; passing a Cholesky factor computed with upper=True but calling inverse with upper=False; factors from a non-SPD source matrix.","commonSituations":"Reconstructing covariance inverses from cached factors; mixing conventions between libraries (scipy.linalg.cholesky defaults to upper, numpy to lower).","solutions":["Feed only genuine Cholesky factors; compute L = cholesky(x, upper=upper) and pass the same upper to cholesky_inverse.","Symmetrize and jitter the source matrix before factorizing.","If the input is triangular but from another library, transpose it when conventions differ."],"exampleFix":"# before\nL = scipy.linalg.cholesky(cov)          # upper by default\nxinv = keras.ops.linalg.cholesky_inverse(L)  # expects lower\n\n# after\nxinv = keras.ops.linalg.cholesky_inverse(L, upper=True)","handlingStrategy":"validation","validationCode":"import numpy as np\nX = np.asarray(x)\nassert X.ndim == 2 and X.shape[0] == X.shape[1]\ntri = np.triu(X) if upper else np.tril(X)\nassert np.allclose(X, tri), 'input is not triangular for the given upper flag'","typeGuard":"def is_cholesky_factor(x, upper=False):\n    X = np.asarray(x)\n    if X.ndim != 2 or X.shape[0] != X.shape[1]:\n        return False\n    T = np.triu(X) if upper else np.tril(X)\n    return np.allclose(X, T) and np.all(np.diag(T) > 0)","tryCatchPattern":"try:\n    xinv = keras.ops.linalg.cholesky_inverse(L, upper=upper)\nexcept ValueError as e:\n    if 'Cholesky inverse failed' in str(e):\n        L = keras.ops.linalg.cholesky(symmetrize(source), upper=upper)\n        xinv = keras.ops.linalg.cholesky_inverse(L, upper=upper)\n    else:\n        raise","preventionTips":["Store the upper flag together with the cached factor.","Note scipy defaults to upper, numpy/keras to lower."],"tags":["keras","linalg","cholesky","matrix-inverse"],"backgroundTag":"matrix-not-positive-definite","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}