keras-team/keras · error · ValueError

Both input arrays must be (arrays of) 2 or 3-dimensional…

Error message

Both input arrays must be (arrays of) 2 or 3-dimensional vectors, but they are {x1_dim} and {x2_dim} dimensional instead.

What it means

The NumPy `cross` (vector cross product) implementation moves the vector axis to the end of each input and then checks that the resulting last dimension is 2 or 3 for both operands — cross products are only defined for 2D (with implied zero z) or 3D vectors. If either operand's vector dimension is any other size, this guard reports both dimensions (x1_dim and x2_dim) so you can see which input is not a stack of 2D/3D vectors.

Solutions

  1. Ensure the axis picked by `axisa`/`axisb` (or `axis`) has size 2 or 3 on both inputs
  2. Fix the axis arguments if the wrong dimension was treated as the vector axis
  3. Pad 2D vectors to 3D or reshape inputs so the trailing vector dimension is 3
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/numpy/numpy.py:565 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/60f34fd5ad1d918a. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/numpy/numpy.py:565

def cross(x1, x2, axisa=-1, axisb=-1, axisc=-1, axis=None):
    axis = standardize_axis_for_numpy(axis)
    x1 = convert_to_tensor(x1)
    x2 = convert_to_tensor(x2)
    dtype = dtypes.result_type(x1.dtype, x2.dtype)
    x1 = x1.astype(dtype)
    x2 = x2.astype(dtype)

    if axis is not None:
        axisa = axis
        axisb = axis
        axisc = axis
    x1 = np.moveaxis(x1, axisa, -1)
    x2 = np.moveaxis(x2, axisb, -1)

    x1_dim = x1.shape[-1]
    x2_dim = x2.shape[-1]
    if x1_dim not in (2, 3) or x2_dim not in (2, 3):
        raise ValueError(
            "Both input arrays must be (arrays of) 2 or 3-dimensional "
            f"vectors, but they are {x1_dim} and {x2_dim} dimensional "
            "instead."
        )

    # NumPy>=2.5 removed support for 2-dimensional vectors in `np.cross`
    # (https://numpy.org/doc/stable/release/2.5.0-notes.html), so pad them
    # to 3 dimensions ourselves (with an implicit zero z-component) before
    # delegating the cross product of the resulting 3-dimensional vectors
    # to `np.cross`.
    def _pad_2d_vector_to_3d(x):
        if x.shape[-1] == 2:
            return np.pad(x, [(0, 0)] * (x.ndim - 1) + [(0, 1)])
        return x

    x1 = _pad_2d_vector_to_3d(x1)
    x2 = _pad_2d_vector_to_3d(x2)

View on GitHub (pinned to 7a34a03db6)