{"record":{"id":"83bcd7d844993e6e","repo":"keras-team/keras","slug":"input-should-have-rank-2-received-input-shape-83bcd7","errorCode":null,"errorMessage":"Input should have rank >= 2. Received: input.shape = {real.shape}","messagePattern":"Input should have rank >= 2\\. Received: input\\.shape = (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/math.py","lineNumber":586,"sourceCode":"        axes = (-2, -1)\n        if not isinstance(x, (tuple, list)) or len(x) != 2:\n            raise ValueError(\n                \"Input `x` should be a tuple of two tensors - real and \"\n                f\"imaginary. Received: x={x}\"\n            )\n\n        real, imag = x\n        # Both real and imaginary parts should have the same shape.\n        if real.shape != imag.shape:\n            raise ValueError(\n                \"Input `x` should be a tuple of two tensors - real and \"\n                \"imaginary. Both the real and imaginary parts should have the \"\n                f\"same shape. Received: x[0].shape = {real.shape}, \"\n                f\"x[1].shape = {imag.shape}\"\n            )\n        # We are calculating 2D FFT. Hence, rank >= 2.\n        if len(real.shape) < 2:\n            raise ValueError(\n                f\"Input should have rank >= 2. \"\n                f\"Received: input.shape = {real.shape}\"\n            )\n\n        # The axes along which we are calculating FFT should be fully-defined.\n        m = real.shape[axes[0]]\n        n = real.shape[axes[1]]\n        if m is None or n is None:\n            raise ValueError(\n                f\"Input should have its {axes} axes fully-defined. \"\n                f\"Received: input.shape = {real.shape}\"\n            )\n\n        return (\n            KerasTensor(shape=real.shape, dtype=real.dtype),\n            KerasTensor(shape=imag.shape, dtype=imag.dtype),\n        )\n","sourceCodeStart":568,"sourceCodeEnd":604,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/math.py#L568-L604","documentation":"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.","triggerScenarios":"Passing a 1D signal of shape (1024,) or a scalar to keras.ops.fft2; flattening an image before the transform.","commonSituations":"Reusing 1D spectrogram code for 2D transforms without reshaping; dataset pipelines that flatten images with reshape(-1).","solutions":["Reshape to at least 2D: keras.ops.reshape(x, (h, w)) or expand_dims for a single row","Use keras.ops.fft (1D) for vector inputs","Check for an accidental flatten() in the preceding layers"],"exampleFix":"# before\nout = keras.ops.fft2((flat, flat))  # flat.shape == (1024,)\n# after\nimg = keras.ops.reshape(flat, (32, 32))\nout = keras.ops.fft2((img, keras.ops.zeros_like(img)))","handlingStrategy":"validation","validationCode":"assert len(real.shape) >= 2, f'fft2 needs rank>=2, got {real.shape}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remove flatten() before 2D transforms","Reshape flat vectors to (h, w) explicitly"],"tags":["keras","fft2","rank","shape"],"backgroundTag":"invalid-tensor-rank","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}