{"record":{"id":"5eab214b16e0ef50","repo":"keras-team/keras","slug":"for-padding-same-output-size-width-w-mu","errorCode":null,"errorMessage":"For `padding='same'`, `output_size` width ({W}) must be in the range ((gW-1)*pW, gW*pW], i.e. ({static_gW * pW - pW}, {static_gW * pW}]. Got: gW={static_gW}, pW={pW}.","messagePattern":"For `padding='same'`, `output_size` width \\((.+?)\\) must be in the range \\(\\(gW-1\\)\\*pW, gW\\*pW\\], i\\.e\\. \\((.+?), (.+?)\\]\\. Got: gW=(.+?), pW=(.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/image.py","lineNumber":1295,"sourceCode":"    x = backend.numpy.transpose(x, axes=(0, 1, 3, 2, 4, 5))\n    x = backend.numpy.reshape(x, (B, gH * pH, gW * pW, C))\n\n    if padding == \"same\":\n        static_gH = patches.shape[1]\n        static_gW = patches.shape[2]\n        if isinstance(static_gH, int) and not (\n            static_gH * pH - pH < H <= static_gH * pH\n        ):\n            raise ValueError(\n                f\"For `padding='same'`, `output_size` height ({H}) must \"\n                f\"be in the range ((gH-1)*pH, gH*pH], i.e. \"\n                f\"({static_gH * pH - pH}, {static_gH * pH}]. \"\n                f\"Got: gH={static_gH}, pH={pH}.\"\n            )\n        if isinstance(static_gW, int) and not (\n            static_gW * pW - pW < W <= static_gW * pW\n        ):\n            raise ValueError(\n                f\"For `padding='same'`, `output_size` width ({W}) must \"\n                f\"be in the range ((gW-1)*pW, gW*pW], i.e. \"\n                f\"({static_gW * pW - pW}, {static_gW * pW}]. \"\n                f\"Got: gW={static_gW}, pW={pW}.\"\n            )\n        pad_total_h = gH * pH - H\n        pad_total_w = gW * pW - W\n        begin = [0, pad_total_h // 2, pad_total_w // 2, 0]\n        out_shape = [B, H, W, C]\n        x = ops.slice(x, begin, out_shape)\n    else:\n        if gH * pH != H or gW * pW != W:\n            raise ValueError(\n                f\"`padding='valid'` requires output_size to equal \"\n                f\"size * grid. Got output_size=({H},{W}), \"\n                f\"grid=({gH},{gW}), size=({pH},{pW}).\"\n            )\n","sourceCodeStart":1277,"sourceCodeEnd":1313,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/image.py#L1277-L1313","documentation":"In keras.ops.image reconstruct_patches (2D), padding='same' re-crops a padded reconstruction down to output_size. That crop is only consistent when the requested width W lies in ((gW-1)*pW, gW*pW], where gW is the patch-grid width and pW the patch width. Outside that window no 'same' extraction could have produced this grid for that image size, so the op raises.","triggerScenarios":"keras.ops.image.reconstruct_patches(patches, size, output_size, padding='same') with output_size width <= (gW-1)*pW or > gW*pW, e.g. gW=4, pW=8 but W=20 (needs 24<W<=32) or W=40.","commonSituations":"Reconstructing patches extracted with different padding/strides than assumed; passing a resized image size instead of the original extract-time width; swapping height/width in output_size; hardcoding output_size for a variable patch grid.","solutions":["Pass the exact width of the image that was originally patched (extract_patches input width) — it always satisfies the range","Otherwise pick any W in ((gW-1)*pW, gW*pW], e.g. the full grid width gW*pW","Recompute the grid: for same-padding extraction gW = ceil(W_original / pW); use that to sanity-check","If you need an output not covered by the grid, switch to padding='valid' with exact size*grid"],"exampleFix":"# before\nreconstruct_patches(patches, size=(8,8), output_size=(20,20), padding='same')\n# gW=4, pW=8 -> W must be in (24,32]\n\n# after\nreconstruct_patches(patches, size=(8,8), output_size=(20,28), padding='same')","handlingStrategy":"validation","validationCode":"gW, pW = int(patches.shape[2]), size[1]\nassert (gW-1)*pW < output_size[1] <= gW*pW, 'W out of same-range'  # if gW static","typeGuard":"def same_w_ok(patches, size, output_size) -> bool:\n    gW = patches.shape[2]\n    return not isinstance(gW, int) or ((gW-1)*size[1] < output_size[1] <= gW*size[1])","tryCatchPattern":null,"preventionTips":["Always pass the exact extract-time image size as output_size","Store extract params (size, strides, padding) alongside the patches tensor"],"tags":["keras","image","patches","shape-mismatch","input-validation"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}