keras-team/keras · error · ValueError

You cannot pass both `shape` and `batch_shape` at the same…

Error message

You cannot pass both `shape` and `batch_shape` at the same time.

What it means

Guard in InputLayer.__init__ (no input_tensor branch): `shape` (per-sample shape, batch dimension implied) and `batch_shape` (full shape including batch axis) are two mutually exclusive ways to declare the input's dimensions, so passing both is rejected to avoid ambiguity over which one defines the layer.

Solutions

  1. Keep only `shape` (without the batch axis) and optionally pass batch_size separately
  2. Keep only `batch_shape`, including the batch dimension as its first entry
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/layers/core/input_layer.py:79 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/c7796fb5ff56014b. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/layers/core/input_layer.py:79

                    "When providing the `input_tensor` argument, you "
                    "cannot provide an incompatible `batch_shape` argument."
                )
            if dtype is not None and input_tensor.dtype != dtype:
                raise ValueError(
                    "When providing the `input_tensor` argument, you "
                    "cannot provide an incompatible `dtype` argument."
                )
            if sparse is not None and input_tensor.sparse != sparse:
                raise ValueError(
                    "When providing the `input_tensor` argument, you "
                    "cannot provide an incompatible `sparse` argument."
                )
            batch_shape = input_tensor.shape
            dtype = input_tensor.dtype
            sparse = input_tensor.sparse
        else:
            if shape is not None and batch_shape is not None:
                raise ValueError(
                    "You cannot pass both `shape` and `batch_shape` at the "
                    "same time."
                )
            if batch_size is not None and batch_shape is not None:
                raise ValueError(
                    "You cannot pass both `batch_size` and `batch_shape` "
                    "at the same time."
                )
            if shape is None and batch_shape is None:
                raise ValueError("You must pass a `shape` argument.")

            if shape is not None:
                shape = backend.standardize_shape(shape)
                batch_shape = (batch_size,) + shape

        self._batch_shape = backend.standardize_shape(batch_shape)
        self._dtype = backend.standardize_dtype(dtype)
        self.sparse = bool(sparse)

View on GitHub (pinned to 7a34a03db6)