keras-team/keras · error · ValueError
`padding='valid'` requires output_size to equal size *…
Error message
`padding='valid'` requires output_size to equal size * grid. Got output_size=({D},{H},{W}), grid=({gD},{gH},{gW}), size=({pD},{pH},{pW}). What it means
Raised during 3D patch reconstruction with padding='valid' when output_size (D, H, W) is not exactly grid * size along every dimension. With valid padding each patch covers a distinct block, so output_size[d] must equal grid[d] * size[d] for depth, height, and width.
Solutions
- Compute output_size from the patches grid: (gD*pD, gH*pH, gW*pW)
- If extraction used 'same', pass padding='same' with the original volume size
- Print patches.shape and size, then assert equality per axis
Example fix
# before reconstruct_patches(p, size=(4,8,8), output_size=(10,28,28)) # after gD, gH, gW = p.shape[1], p.shape[2], p.shape[3] reconstruct_patches(p, size=(4,8,8), output_size=(gD*4, gH*8, gW*8))
Defensive patterns
Strategy: validation
Validate before calling
gD, gH, gW = patches.shape[1], patches.shape[2], patches.shape[3]
expected = (gD*size[0], gH*size[1], gW*size[2])
assert tuple(output_size) == expected, f'{output_size} != {expected}' Type guard
def valid_3d_consistent(patches, size, output_size) -> bool:
g = patches.shape[1:4]
return tuple(output_size) == (g[0]*size[0], g[1]*size[1], g[2]*size[2]) Prevention
- Compute output_size from the grid, never store a stale constant
- Pair extract/reconstruct calls with identical padding/strides
When it happens
Trigger: reconstruct_patches(..., padding='valid') (default) on 3D patches with any axis of output_size != grid*patch for that axis.
Common situations: Default padding forgotten while passing the original same-padded volume size; extracting with 'same' but reconstructing with 'valid'; per-axis dimension mix-ups in (D,H,W).
Understand the failure class
Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.
Related errors
- For `padding='same'`, `output_size` depth
- For `padding='same'`, `output_size` width
- `padding='valid'` requires output_size to equal size *…
- First dim of `coordinates` must be the same as the rank of…
- Invalid `output_size`. Expected length 3 (D, H, W). Got…
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/1141f6ba2180124b.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/ops/image.py:1436
f"be in the range ((gW-1)*pW, gW*pW], i.e. "
f"({static_gW * pW - pW}, {static_gW * pW}]. "
f"Got: gW={static_gW}, pW={pW}."
)
pad_total_d = gD * pD - D
pad_total_h = gH * pH - H
pad_total_w = gW * pW - W
begin = [
0,
pad_total_d // 2,
pad_total_h // 2,
pad_total_w // 2,
0,
]
out_shape = [B, D, H, W, C]
x = ops.slice(x, begin, out_shape)
else:
if gD * pD != D or gH * pH != H or gW * pW != W:
raise ValueError(
f"`padding='valid'` requires output_size to equal "
f"size * grid. Got output_size=({D},{H},{W}), "
f"grid=({gD},{gH},{gW}), size=({pD},{pH},{pW})."
)
if _unbatched:
x = backend.numpy.squeeze(x, axis=0)
return x
class MapCoordinates(Operation):
def __init__(self, order, fill_mode="constant", fill_value=0, *, name=None):
super().__init__(name=name)
self.order = order
self.fill_mode = fill_mode
self.fill_value = fill_value
def call(self, inputs, coordinates):View on GitHub (pinned to 7a34a03db6)