keras-team/keras · error · ValueError

Invalid coordinates rank: expected at least rank 2…

Error message

Invalid coordinates rank: expected at least rank 2. Received input with shape: {coordinates.shape}

What it means

Rank guard in `map_coordinates` (NumPy backend), checked after the leading-dimension consistency test: `coordinates` must be at least rank 2 — one axis for the input dimension index plus at least one axis enumerating sampling points (e.g. (ndim, H, W) for 2D remapping). A 1D coordinates array cannot describe sampling locations and is rejected with its shape shown.

Solutions

  1. Give coordinates at least two axes, e.g. shape (2, N) for N 2D sampling points
  2. Stack per-dimension index vectors with keras.ops.stack(..., axis=0) or np.stack to reach rank 2
  3. For single-point sampling, keep shape (ndim, 1) rather than a flat (ndim,) vector
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/numpy/ops/image.py:931 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/63cc1de9f6c60b0a. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/numpy/ops/image.py:931

    homography_matrix = np.linalg.solve(coefficient_matrix, target_vector)
    homography_matrix = np.reshape(homography_matrix, [-1, 8])
    return homography_matrix.astype(compute_dtype)


def map_coordinates(
    inputs, coordinates, order, fill_mode="constant", fill_value=0.0
):
    inputs = convert_to_tensor(inputs)
    coordinates = convert_to_tensor(coordinates)
    if coordinates.shape[0] != len(inputs.shape):
        raise ValueError(
            "First dim of `coordinates` must be the same as the rank of "
            "`inputs`. "
            f"Received inputs with shape: {inputs.shape} and coordinate "
            f"leading dim of {coordinates.shape[0]}"
        )
    if len(coordinates.shape) < 2:
        raise ValueError(
            "Invalid coordinates rank: expected at least rank 2."
            f" Received input with shape: {coordinates.shape}"
        )
    if fill_mode not in MAP_COORDINATES_FILL_MODES:
        raise ValueError(
            "Invalid value for argument `fill_mode`. Expected one of "
            f"{set(MAP_COORDINATES_FILL_MODES.keys())}. Received: "
            f"fill_mode={fill_mode}"
        )
    if order not in range(2):
        raise ValueError(
            "Invalid value for argument `order`. Expected one of "
            f"{[0, 1]}. Received: order={order}"
        )
    # SciPy's implementation of map_coordinates handles boundaries incorrectly,
    # unless mode='reflect'. For order=1, this only affects interpolation
    # outside the bounds of the original array.
    # https://github.com/scipy/scipy/issues/2640

View on GitHub (pinned to 7a34a03db6)