keras-team/keras · error · ValueError

The number of dimensions in `inputs` must match the number o

Error message

The number of dimensions in `inputs` must match the number of dimensions in `shape`. Received inputs.shape={inputs.shape} and shape={self.shape}

What it means

The slice op (used by keras.ops.slice) requires len(shape) == inputs.ndim because it replaces whole axes of the output. A 2D shape against a 3D tensor, for example, is ambiguous and rejected.

Source

Thrown at keras/src/ops/core.py:440

        return ScatterUpdate(reduction=reduction).symbolic_call(
            inputs, indices, updates
        )
    return backend.core.scatter_update(
        inputs, indices, updates, reduction=reduction
    )


class Slice(Operation):
    def __init__(self, shape, *, name=None):
        super().__init__(name=name)
        self.shape = shape

    def call(self, inputs, start_indices):
        return backend.core.slice(inputs, start_indices, self.shape)

    def compute_output_spec(self, inputs, start_indices):
        if len(self.shape) != len(inputs.shape):
            raise ValueError(
                "The number of dimensions in `inputs` must match the number of "
                f"dimensions in `shape`. Received inputs.shape={inputs.shape} "
                f"and shape={self.shape}"
            )
        if hasattr(start_indices, "__len__") and len(start_indices) != len(
            inputs.shape
        ):
            raise ValueError(
                "The number of dimensions in `start_indices` must match the "
                "number of dimensions in `inputs`. Received "
                f"start_indices={start_indices} and inputs.shape={inputs.shape}"
            )

        final_shape = []
        for i, (input_dim, slice_dim) in enumerate(
            zip(inputs.shape, self.shape)
        ):
            if slice_dim != -1:

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Provide one entry per dimension in shape
  2. Derive the slice shape from inputs.shape programmatically

Example fix

# before
keras.ops.slice(x3d, (0, 0), (5, 5))  # x3d.ndim == 3

# after
keras.ops.slice(x3d, (0, 0, 0), (5, 5, 3))
Defensive patterns

Strategy: validation

Validate before calling

assert len(shape) == len(x.shape), (
    f'shape {shape} vs inputs ndim {len(x.shape)}')

Prevention

When it happens

Trigger: keras.ops.slice(x, start, shape) with len(shape) != x.ndim

Common situations: Slicing fixed-size patches from images/sequences; refactoring shapes (channels-last changes) without updating slice shapes

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/f745500c3a61f44a. Report an issue: GitHub.