keras-team/keras · error · ValueError
Input should have rank >= 1. Received: input.shape = {x.shap
Error message
Input should have rank >= 1. Received: input.shape = {x.shape} What it means
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.
Source
Thrown at keras/src/ops/math.py:442
>>> y = keras.ops.convert_to_tensor([[1.0, 0.0], [0.0, 1.0]])
>>> keras.ops.cdist(x, y)
array([[1. , 1. ],
[1. , 1.4142135]], dtype=float32)
"""
if any_symbolic_tensors((x, y)):
return CDist().symbolic_call(x, y)
return backend.math.cdist(x, y)
class ExtractSequences(Operation):
def __init__(self, sequence_length, sequence_stride, *, name=None):
super().__init__(name=name)
self.sequence_length = sequence_length
self.sequence_stride = sequence_stride
def compute_output_spec(self, x):
if len(x.shape) < 1:
raise ValueError(
f"Input should have rank >= 1. "
f"Received: input.shape = {x.shape}"
)
if x.shape[-1] is not None:
num_sequences = (
1 + (x.shape[-1] - self.sequence_length) // self.sequence_stride
)
else:
num_sequences = None
new_shape = x.shape[:-1] + (num_sequences, self.sequence_length)
return KerasTensor(shape=new_shape, dtype=x.dtype)
def call(self, x):
return backend.math.extract_sequences(
x,
sequence_length=self.sequence_length,
sequence_stride=self.sequence_stride,
)View on GitHub (pinned to 7a34a03db6)
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.
Example fix
// before from keras import ops s = ops.array(7.0) # rank 0 seqs = ops.extract_sequences(s, 3, 1) # ValueError // after s = ops.reshape(ops.array(7.0), (1,)) # rank 1 seqs = ops.extract_sequences(s, 3, 1)
Defensive patterns
Strategy: validation
Validate before calling
from keras import ops
def ensure_rank1(x):
if len(ops.shape(x)) < 1:
x = ops.reshape(x, (1,))
return x
seqs = ops.extract_sequences(ensure_rank1(x), seq_len, stride) Type guard
import keras
def rank1_plus(x) -> bool:
return keras.ops.ndim(x) >= 1 Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Expected input to have rank >= 2. Received input with shape
- Inputs to `cdist` must have rank >= 2. Received shapes: x.sh
- Architecture configuration does not match {weights_name} var
- The `weights` argument should be either `None` (random initi
- Expected mode to be one of `caffe`, `tf` or `torch`. Receive
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/3e21a9445ca6b2f2.
Report an issue: GitHub.