keras-team/keras · error · ValueError
Cannot infer argument `num` from shape
Error message
Cannot infer argument `num` from shape {x.shape}. Either provide a tensor with a concrete shape in the `axis` dimension or explicitly pass the `num` argument. What it means
For element-count based behavior (e.g. boolean all/any), num defaults to the size of the reduced axis. In symbolic tracing that dimension may be None, so the count cannot be inferred and you must supply num.
Solutions
- Pass num explicitly
- Run outside symbolic tracing or with concrete shapes
- Reshape the tensor so the axis dim is concrete before the reduction
Example fix
# before keras.ops.all(x) # x.shape[axis] is None under tracing # after keras.ops.all(x, num=128) # or pass a concrete-shaped tensor
Defensive patterns
Strategy: validation
Validate before calling
if num is None:
num = x.shape[axis]
assert num is not None, 'pass num explicitly when axis dim is dynamic' Prevention
- Pass num explicitly for count-based reductions
- Test ops inside a concrete (non-symbolic) shape context first
When it happens
Trigger: Calling an op with num=None on a KerasTensor whose shape[axis] is None inside a functional model or symbolic call
Common situations: Using count-based reductions on boolean tensors inside a symbolic (KerasTensor) trace or functional model
Related errors
- All `axis` values to be kept must have a known shape…
- Array inputs to associative_scan must have the same first…
- Function was called with an invalid input structure…
- Input should have its
- Input should have its last dimension fully-defined…
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/3f3d73247c906221.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/ops/core.py:764
class Unstack(Operation):
def __init__(self, num=None, axis=0, *, name=None):
super().__init__(name=name)
self.num = num
self.axis = axis
def call(self, x):
return backend.core.unstack(x, self.num, self.axis)
def compute_output_spec(self, x):
axis = canonicalize_axis(self.axis, len(x.shape))
output_shapes = x.shape[:axis] + x.shape[axis + 1 :]
num = self.num
if num is None:
num = x.shape[axis]
if num is None:
raise ValueError(
"Cannot infer argument `num` from shape "
f"{x.shape}. Either provide a tensor with a "
"concrete shape in the `axis` dimension or "
"explicitly pass the `num` argument."
)
output = [
KerasTensor(shape=output_shapes, dtype=x.dtype) for _ in range(num)
]
return output
@keras_export("keras.ops.unstack")
def unstack(x, num=None, axis=0):
"""Unpacks the given dimension of a rank-R tensor into rank-(R-1) tensors.
Args:
x: The input tensor.
num: The length of the dimension axis. Automatically inferredView on GitHub (pinned to 7a34a03db6)