jax-ml/jax · error · ValueError
Number of samples, {num}, must be non-negative.
Error message
Number of samples, {num}, must be non-negative. What it means
The differentiable implementation behind jnp.linspace validates num (number of samples) and rejects negative values, since a negative sample count has no mathematical meaning and cannot be traced into a valid computation.
Source
Thrown at jax/_src/numpy/array_creation.py:596
Array([[ 0. , 5. ],
[ 1.25, 6.25],
[ 2.5 , 7.5 ],
[ 3.75, 8.75],
[ 5. , 10. ]], dtype=float32)
"""
num = core.concrete_dim_or_error(num, "'num' argument of jnp.linspace")
axis = core.concrete_or_error(operator.index, axis, "'axis' argument of jnp.linspace")
return _linspace(start, stop, num, endpoint, retstep, dtype, axis, device=device)
@api.jit(static_argnames=('num', 'endpoint', 'retstep', 'dtype', 'axis', 'device'))
def _linspace(start: ArrayLike, stop: ArrayLike, num: int = 50,
endpoint: bool = True, retstep: bool = False,
dtype: DTypeLike | None = None,
axis: int = 0,
*, device: xc.Device | Sharding | None = None) -> Array | tuple[Array, Array]:
"""Implementation of linspace differentiable in start and stop args."""
if num < 0:
raise ValueError(f"Number of samples, {num}, must be non-negative.")
start, stop = util.ensure_arraylike("linspace", start, stop)
if dtype is None:
dtype = dtypes.to_inexact_dtype(dtypes.result_type(start, stop))
else:
dtype = dtypes.check_and_canonicalize_user_dtype(dtype, "linspace")
computation_dtype = dtypes.to_inexact_dtype(dtype)
start = start.astype(computation_dtype)
stop = stop.astype(computation_dtype)
bounds_shape = list(lax.broadcast_shapes(np.shape(start), np.shape(stop)))
broadcast_start = util._broadcast_to(start, bounds_shape)
broadcast_stop = util._broadcast_to(stop, bounds_shape)
axis = len(bounds_shape) + axis + 1 if axis < 0 else axis
bounds_shape.insert(axis, 1)
div = (num - 1) if endpoint else num
if num > 1:
delta: Array = lax.convert_element_type(stop - start, computation_dtype) / asarray(div, dtype=computation_dtype)View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Clamp num: max(num, 0) or validate before calling
- Recheck the count formula — linspace wants a count, not a step; use jnp.arange for step-based spacing
Example fix
# before a = jnp.linspace(0.0, 1.0, num=(stop - start) // step) # can be negative # after n = max(int((stop - start) // step), 0) a = jnp.linspace(0.0, 1.0, num=n)
Defensive patterns
Strategy: validation
Validate before calling
num = max(int(num), 0) jnp.linspace(start, stop, num=num)
Type guard
null
Try / catch
null
Prevention
- Clamp computed sample counts
- Use arange when spacing (not count) is the natural parameter
When it happens
Trigger: jnp.linspace(0, 1, num=-5) or num computed from an expression that can go negative (e.g. (b - a) // step).
Common situations: Computing sample counts from user parameters or deltas where rounding/division can produce -1 or lower; passing None-ish defaults that coerce to negative numbers.
Related errors
- `compute_on`'s compute_type argument must be a string.
- Argument '{x}' of type '{typ}' is not a valid JAX type
- Argument '{arg}' of type {type(arg)} is not a valid JAX type
- Expected kind to be a dtype, string, or tuple; got {kind=}
- at least one array or dtype is required
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/195ff19ee39ad0cb.
Report an issue: GitHub.