keras-team/keras · error · ValueError

Input should have rank >= 2. Received: input.shape = {real.s

Error message

Input should have rank >= 2. Received: input.shape = {real.shape}

What it means

fft2 computes a 2D FFT over the last two axes, so inputs must have rank >= 2. A rank-0 or rank-1 tensor (e.g. a flat vector) fails compute_output_spec.

Source

Thrown at keras/src/ops/math.py:586

        axes = (-2, -1)
        if not isinstance(x, (tuple, list)) or len(x) != 2:
            raise ValueError(
                "Input `x` should be a tuple of two tensors - real and "
                f"imaginary. Received: x={x}"
            )

        real, imag = x
        # Both real and imaginary parts should have the same shape.
        if real.shape != imag.shape:
            raise ValueError(
                "Input `x` should be a tuple of two tensors - real and "
                "imaginary. Both the real and imaginary parts should have the "
                f"same shape. Received: x[0].shape = {real.shape}, "
                f"x[1].shape = {imag.shape}"
            )
        # We are calculating 2D FFT. Hence, rank >= 2.
        if len(real.shape) < 2:
            raise ValueError(
                f"Input should have rank >= 2. "
                f"Received: input.shape = {real.shape}"
            )

        # The axes along which we are calculating FFT should be fully-defined.
        m = real.shape[axes[0]]
        n = real.shape[axes[1]]
        if m is None or n is None:
            raise ValueError(
                f"Input should have its {axes} axes fully-defined. "
                f"Received: input.shape = {real.shape}"
            )

        return (
            KerasTensor(shape=real.shape, dtype=real.dtype),
            KerasTensor(shape=imag.shape, dtype=imag.dtype),
        )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Reshape to at least 2D: keras.ops.reshape(x, (h, w)) or expand_dims for a single row
  2. Use keras.ops.fft (1D) for vector inputs
  3. Check for an accidental flatten() in the preceding layers

Example fix

# before
out = keras.ops.fft2((flat, flat))  # flat.shape == (1024,)
# after
img = keras.ops.reshape(flat, (32, 32))
out = keras.ops.fft2((img, keras.ops.zeros_like(img)))
Defensive patterns

Strategy: validation

Validate before calling

assert len(real.shape) >= 2, f'fft2 needs rank>=2, got {real.shape}'

Prevention

When it happens

Trigger: Passing a 1D signal of shape (1024,) or a scalar to keras.ops.fft2; flattening an image before the transform.

Common situations: Reusing 1D spectrogram code for 2D transforms without reshaping; dataset pipelines that flatten images with reshape(-1).

Related errors


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