{"record":{"id":"3e21a9445ca6b2f2","repo":"keras-team/keras","slug":"input-should-have-rank-1-received-input-shape","errorCode":null,"errorMessage":"Input should have rank >= 1. Received: input.shape = {x.shape}","messagePattern":"Input should have rank >= 1\\. Received: input\\.shape = (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/math.py","lineNumber":442,"sourceCode":"    >>> y = keras.ops.convert_to_tensor([[1.0, 0.0], [0.0, 1.0]])\n    >>> keras.ops.cdist(x, y)\n    array([[1.       , 1.       ],\n           [1.       , 1.4142135]], dtype=float32)\n    \"\"\"\n    if any_symbolic_tensors((x, y)):\n        return CDist().symbolic_call(x, y)\n    return backend.math.cdist(x, y)\n\n\nclass ExtractSequences(Operation):\n    def __init__(self, sequence_length, sequence_stride, *, name=None):\n        super().__init__(name=name)\n        self.sequence_length = sequence_length\n        self.sequence_stride = sequence_stride\n\n    def compute_output_spec(self, x):\n        if len(x.shape) < 1:\n            raise ValueError(\n                f\"Input should have rank >= 1. \"\n                f\"Received: input.shape = {x.shape}\"\n            )\n        if x.shape[-1] is not None:\n            num_sequences = (\n                1 + (x.shape[-1] - self.sequence_length) // self.sequence_stride\n            )\n        else:\n            num_sequences = None\n        new_shape = x.shape[:-1] + (num_sequences, self.sequence_length)\n        return KerasTensor(shape=new_shape, dtype=x.dtype)\n\n    def call(self, x):\n        return backend.math.extract_sequences(\n            x,\n            sequence_length=self.sequence_length,\n            sequence_stride=self.sequence_stride,\n        )","sourceCodeStart":424,"sourceCodeEnd":460,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/math.py#L424-L460","documentation":"keras.ops.extract_sequences slices the last axis into windows of sequence_length with stride sequence_stride, so it needs input of rank >= 1. The compute_output_spec raises this when x.shape is empty, i.e. a rank-0 scalar.","triggerScenarios":"Calling keras.ops.extract_sequences(x, sequence_length, sequence_stride) on a 0-D scalar tensor; using the op after an ops.squeeze or an all-axes reduction removed every dimension; passing a Python scalar converted via keras.ops.array without a shape.","commonSituations":"Transformer-encoder preprocessing where squeeze(axis=-1) on scalar outputs precedes sequence extraction; feeding per-sample scalars that should be (1,) shaped; dynamic dimension removal in a functional graph leaving scalar symbolic tensors.","solutions":["Keep at least one axis: wrap scalars with ops.reshape(x, (1,)) or ops.expand_dims; if upstream code squeezed all axes, squeeze only the axes you actually collapsed.","Validate rank before the call: if len(x.shape) == 0: reshape to (1,).","In custom layers, keep a feature axis (..., 1) rather than fully reducing to scalars before sequence ops."],"exampleFix":"// before\nfrom keras import ops\ns = ops.array(7.0)                     # rank 0\nseqs = ops.extract_sequences(s, 3, 1)  # ValueError\n\n// after\ns = ops.reshape(ops.array(7.0), (1,))   # rank 1\nseqs = ops.extract_sequences(s, 3, 1)","handlingStrategy":"validation","validationCode":"from keras import ops\n\ndef ensure_rank1(x):\n    if len(ops.shape(x)) < 1:\n        x = ops.reshape(x, (1,))\n    return x\n\nseqs = ops.extract_sequences(ensure_rank1(x), seq_len, stride)","typeGuard":"import keras\n\ndef rank1_plus(x) -> bool:\n    return keras.ops.ndim(x) >= 1","tryCatchPattern":null,"preventionTips":["Never fully squeeze before extract_sequences; keep a trailing axis.","Convert scalars with ops.reshape(v, (1,)) at ingestion.","Add rank checks in custom layers that chain reductions with sequence ops."],"tags":["keras","extract-sequences","rank-error","shape-validation","sequence-ops"],"backgroundTag":"tensor-rank-or-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}