keras-team/keras · error · TypeError

-dimensional array given. Array must be one or…

Error message

{b.ndim}-dimensional array given. Array must be one or two-dimensional

What it means

Raised in keras/src/backend/tensorflow/linalg.py lstsq: the right-hand-side b (after a 1D b is expanded to a column) must be rank 2 of shape (m,) -> (m,1) or (m,k). The received b has some other rank (e.g. 3D), so the guard raises a TypeError before solving the least-squares system.

Solutions

  1. Pass b as a 1D vector of length m or a 2D array of shape (m, k)
  2. Reshape b, e.g. tf.reshape(b, (-1, 1)), to match a's first dimension
  3. Flatten extra trailing dimensions of b if they were not intended
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/tensorflow/linalg.py:222 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/c21fe83374b2ddb3. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/tensorflow/linalg.py:222

        x, full_matrices=full_matrices, compute_uv=compute_uv
    )
    return u, s, tf.linalg.adjoint(v)


def lstsq(a, b, rcond=None):
    a = convert_to_tensor(a)
    b = convert_to_tensor(b)
    if a.shape[0] != b.shape[0]:
        raise ValueError("Leading dimensions of input arrays must match")
    b_orig_ndim = b.ndim
    if b_orig_ndim == 1:
        b = b[:, None]
    if a.ndim != 2:
        raise TypeError(
            f"{a.ndim}-dimensional array given. Array must be two-dimensional"
        )
    if b.ndim != 2:
        raise TypeError(
            f"{b.ndim}-dimensional array given. "
            "Array must be one or two-dimensional"
        )
    m, n = a.shape
    dtype = a.dtype
    eps = tf.experimental.numpy.finfo(dtype).eps
    if a.shape == ():
        s = tf.zeros(0, dtype=a.dtype)
        x = tf.zeros((n, *b.shape[1:]), dtype=a.dtype)
    else:
        if rcond is None:
            rcond = eps * max(n, m)
        else:
            rcond = tf.where(rcond < 0, eps, rcond)
        u, s, vt = svd(a, full_matrices=False)
        mask = s >= tf.convert_to_tensor(rcond, dtype=s.dtype) * s[0]
        safe_s = tf.cast(tf.where(mask, s, 1), dtype=a.dtype)
        s_inv = tf.where(mask, 1 / safe_s, 0)[:, tf.newaxis]

View on GitHub (pinned to 7a34a03db6)