keras-team/keras · error · ValueError
Invalid `output_size`. Expected length 3 (D, H, W). Got: out
Error message
Invalid `output_size`. Expected length 3 (D, H, W). Got: output_size={output_size} What it means
The 3D branch of keras.ops.image.reconstruct_patches requires output_size as a 3-element sequence (D, H, W). It raises when len(output_size) != 3, before touching tensors, because the target volume shape is undefined.
Source
Thrown at keras/src/ops/image.py:1328
f"size * grid. Got output_size=({H},{W}), "
f"grid=({gH},{gW}), size=({pH},{pW})."
)
if _unbatched:
x = backend.numpy.squeeze(x, axis=0)
return x
def _reconstruct_patches_3d(
patches,
size,
output_size,
strides=None,
padding="valid",
data_format=None,
):
if len(output_size) != 3:
raise ValueError(
"Invalid `output_size`. Expected length 3 (D, H, W). "
f"Got: output_size={output_size}"
)
if padding not in ("same", "valid"):
raise ValueError(
f"Invalid `padding`. Expected 'same' or 'valid'. Got: {padding}"
)
_validate_reconstruct_strides(size, strides, "reconstruct_patches")
data_format = backend.standardize_data_format(data_format)
if data_format == "channels_first":
# Reconstruct in channels_last layout, then move channels back.
# Patches are (flat, gD, gH, gW) unbatched or (B, flat, gD, gH, gW).
if len(patches.shape) == 4:
patches = backend.numpy.transpose(patches, axes=(1, 2, 3, 0))
elif len(patches.shape) == 5:
patches = backend.numpy.transpose(patches, axes=(0, 2, 3, 4, 1))
else:
raise ValueError(View on GitHub (pinned to 7a34a03db6)
Solutions
- Pass exactly three spatial ints: output_size=(D,H,W), no batch/channel axes
- Confirm the patches tensor really is the 3D kind (rank 5 batched / rank 4 unbatched)
- Add an assert len(output_size)==3 guard in the data pipeline
Example fix
# before reconstruct_patches(patches, size=(4,8,8), output_size=(28,28)) # after reconstruct_patches(patches, size=(4,8,8), output_size=(16,28,28))
Defensive patterns
Strategy: validation
Validate before calling
if len(output_size) != 3:
raise ValueError('output_size must be (D,H,W)') Type guard
def is_3d_output_size(output_size) -> bool:
return hasattr(output_size, '__len__') and len(output_size) == 3 Prevention
- Mirror the dimensionality of size and output_size
- Add length asserts in preprocessing pipelines
When it happens
Trigger: reconstruct_patches on rank-5 (batched 3D) or rank-4 (unbatched 3D) patches with output_size of length 2 or 4, e.g. (28,28) or (D,H,W,C).
Common situations: Porting 2D reconstruction code to 3D volumes (medical CT/MRI, video) and not extending output_size; including a channel or batch entry in output_size; passing a numpy array where a typo drops an element.
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
- For `padding='same'`, `output_size` width ({W}) must be in t
- `padding='valid'` requires output_size to equal size * grid.
- `patches` has unexpected rank for 3D reconstruction. Expecte
- For `padding='same'`, `output_size` depth ({D}) must be in t
- `padding='valid'` requires output_size to equal size * grid.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/e264e7a20097cd59.
Report an issue: GitHub.