keras-team/keras · error · ValueError
{name} must be >= 0. Received: {name}={value}
Error message
{name} must be >= 0. Received: {name}={value} What it means
Shared validator for the pad/crop images ops: any explicitly supplied padding/cropping amount (or related numeric arg) must be non-negative. A negative value is rejected before any tensor op because negative padding/cropping is undefined.
Source
Thrown at keras/src/ops/image.py:1536
"""
if any_symbolic_tensors((inputs, coordinates)):
return MapCoordinates(
order,
fill_mode,
fill_value,
).symbolic_call(inputs, coordinates)
return backend.image.map_coordinates(
inputs,
coordinates,
order,
fill_mode,
fill_value,
)
def _validate_non_negative(value, name):
if value is not None and value < 0:
raise ValueError(f"{name} must be >= 0. Received: {name}={value}")
def _validate_pad_images_args(
top_padding,
left_padding,
bottom_padding,
right_padding,
target_height,
target_width,
):
if [top_padding, bottom_padding, target_height].count(None) != 1:
raise ValueError(
"Must specify exactly two of "
"top_padding, bottom_padding, target_height. "
f"Received: top_padding={top_padding}, "
f"bottom_padding={bottom_padding}, "
f"target_height={target_height}"
)View on GitHub (pinned to 7a34a03db6)
Solutions
- Use crop_images to shrink, pad_images to grow — never negative pad values
- If deriving padding from target sizes, clamp: max(0, target - height - other) or switch to specifying target sizes directly
- Validate all padding/cropping args against 0 before the call
Example fix
# before pad_images(img, top_padding=height - target_height, ...) # negative # after crop_images(img, top_cropping=target_height - height if False else 0, ...) # or just: pad_images(img, target_height=target_h, target_width=target_w, ...) # derive non-negative pads
Defensive patterns
Strategy: validation
Validate before calling
for name, v in [('top_padding', top_padding), ...]:
if v is not None and v < 0:
raise ValueError(f'{name} must be >= 0') Type guard
def all_non_negative(vals) -> bool:
return all(v is None or v >= 0 for v in vals) Prevention
- Clamp derived paddings with max(0, x)
- Use crop to shrink, pad to grow
When it happens
Trigger: keras.ops.image.pad_images / crop_images (or the corresponding layers) with a negative top/bottom/left/right padding or cropping value, e.g. top_padding=-4, often computed as target - height - other_side and going negative.
Common situations: Using negative padding to emulate cropping; target_height smaller than the image so derived padding becomes negative; arithmetic on config values that can go below zero for some samples in a pipeline.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Must specify exactly two of top_padding, bottom_padding, tar
- Must specify exactly two of left_padding, right_padding, tar
- The `weights` argument should be either `None` (random initi
- `padding` should have two elements. Received: padding={paddi
- Invalid padding: {padding}
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/c9b4acef74964d27.
Report an issue: GitHub.