jax-ml/jax · error · TypeError
can only specify one unknown axis size with a `-1` value, go
Error message
can only specify one unknown axis size with a `-1` value, got {orig_newshape} What it means
When reshaping with a -1 placeholder, only one axis may be unspecified because its size is inferred from the others. `_compute_newshape` counts int entries equal to -1 and raises if there is more than one. This mirrors NumPy's rule and is checked before any shape arithmetic.
Source
Thrown at jax/_src/numpy/array_methods.py:525
Refer to :func:`jax.numpy.var` for full documentation.
"""
return reductions.var(self, axis=axis, dtype=dtype, out=out, ddof=ddof,
keepdims=keepdims, where=where, correction=correction)
def _compute_newshape(arr: Array, newshape: DimSize | Shape) -> Shape:
"""Fixes a -1 value in newshape, if present."""
orig_newshape = newshape # for error messages
try:
iter(newshape) # pyrefly: ignore[no-matching-overload]
except TypeError:
newshape = [newshape]
else:
newshape: Sequence[DimSize] # pyrefly: ignore[redefinition]
newshape = core.canonicalize_shape(newshape)
neg1s = [i for i, d in enumerate(newshape) if type(d) is int and d == -1]
if len(neg1s) > 1:
raise TypeError("can only specify one unknown axis size with a `-1` value, "
f"got {orig_newshape}")
if neg1s:
i, = neg1s
other_sizes = (*newshape[:i], *newshape[i+1:])
if (all(isinstance(d, int) for d in (*arr.shape, *other_sizes)) and
arr.size % math.prod(other_sizes) != 0):
raise TypeError(f"cannot reshape array of shape {arr.shape} (size {arr.size}) "
f"into shape {orig_newshape} because the product of "
f"specified axis sizes ({math.prod(other_sizes)}) does "
f"not evenly divide {arr.size}")
sz = core.cancel_divide_tracers(arr.shape, other_sizes)
if sz is not None:
return (*newshape[:i], sz, *newshape[i+1:])
else:
if (all(isinstance(d, int) for d in (*arr.shape, *newshape)) and
arr.size != math.prod(newshape)):
raise TypeError(f"cannot reshape array of shape {arr.shape} (size {arr.size}) "
f"into shape {orig_newshape} (size {math.prod(newshape)})")View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Specify all but one dimension explicitly
- Compute the inferred dimension yourself: `arr.reshape(arr.shape[0], -1)` for batch-major data
Example fix
# before arr.reshape(-1, -1) # after arr.reshape(arr.shape[0], -1)
Defensive patterns
Strategy: validation
Validate before calling
def count_neg1s(shape):
return sum(1 for d in shape if isinstance(d, int) and d == -1)
def assert_valid_reshape(shape):
assert count_neg1s(shape) <= 1, 'at most one -1 allowed' Prevention
- Leave exactly one inferred dimension
- Prefer arr.reshape(arr.shape[0], -1) in batched code
When it happens
Trigger: Calling reshape with two or more -1s, e.g. `arr.reshape(-1, -1)` or `jnp.reshape(arr, (-1, 3, -1))`.
Common situations: Dynamically built reshape shapes where a batch axis and a feature axis are both left as -1; typos when copying shapes.
Related errors
- reshape new_sizes must all be positive, got {}.
- np.reshape order=A is not implemented.
- Unexpected value for 'order' argument: {order}.
- cannot reshape array of shape {arr.shape} (size {arr.size})
- cannot reshape array of shape {arr.shape} (size {arr.size})
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/15c477a3aca0b81c.
Report an issue: GitHub.