{"record":{"id":"6057577a586fc0af","repo":"keras-team/keras","slug":"padding-should-have-two-elements-received-padd","errorCode":null,"errorMessage":"`padding` should have two elements. Received: padding={padding}.","messagePattern":"`padding` should have two elements\\. Received: padding=(.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/reshaping/zero_padding2d.py","lineNumber":78,"sourceCode":"        - If `data_format` is `\"channels_first\"`:\n          `(batch_size, channels, height, width)`\n\n    Output shape:\n        4D tensor with shape:\n        - If `data_format` is `\"channels_last\"`:\n          `(batch_size, padded_height, padded_width, channels)`\n        - If `data_format` is `\"channels_first\"`:\n          `(batch_size, channels, padded_height, padded_width)`\n    \"\"\"\n\n    def __init__(self, padding=(1, 1), data_format=None, **kwargs):\n        super().__init__(**kwargs)\n        self.data_format = backend.standardize_data_format(data_format)\n        if isinstance(padding, int):\n            self.padding = ((padding, padding), (padding, padding))\n        elif hasattr(padding, \"__len__\"):\n            if len(padding) != 2:\n                raise ValueError(\n                    \"`padding` should have two elements. \"\n                    f\"Received: padding={padding}.\"\n                )\n            height_padding = argument_validation.standardize_tuple(\n                padding[0], 2, \"1st entry of padding\", allow_zero=True\n            )\n            width_padding = argument_validation.standardize_tuple(\n                padding[1], 2, \"2nd entry of padding\", allow_zero=True\n            )\n            self.padding = (height_padding, width_padding)\n        else:\n            raise ValueError(\n                \"`padding` should be either an int, a tuple of 2 ints \"\n                \"(symmetric_height_crop, symmetric_width_crop), \"\n                \"or a tuple of 2 tuples of 2 ints \"\n                \"((top_crop, bottom_crop), (left_crop, right_crop)). \"\n                f\"Received: padding={padding}.\"\n            )","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/reshaping/zero_padding2d.py#L60-L96","documentation":"ZeroPadding2D.__init__ accepts padding as an int, or a 2-element sequence (height, width), each element further standardized to a (before, after) pair. Passing a sequence with a length other than 2 — e.g. a flat 4-tuple like (1,1,2,2) — raises immediately at construction.","triggerScenarios":"ZeroPadding2D(padding=(1,1,2,2)) (flat 4-tuple, length 4); ZeroPadding2D(padding=[1]) (length 1); ZeroPadding2D(padding=((1,1),(2,2),(3,3))) (length 3). Correct forms: 2, (2,2), or ((1,1),(2,2)).","commonSituations":"Assuming the Keras API mirrors PyTorch's nn.ZeroPad2d, which does take a flat 4-tuple (left,right,top,bottom); building padding programmatically and flattening the nested structure; config files storing padding as a flat list.","solutions":["Use the Keras shape: int, (h, w), or ((h_top, h_bottom), (w_left, w_right))","If porting from PyTorch, convert torch's (left,right,top,bottom) to Keras ((top,bottom),(left,right))","Validate the padding structure in config-loading code before layer construction"],"exampleFix":"# before (PyTorch style, invalid in Keras)\nlayer = ZeroPadding2D(padding=(1, 1, 2, 2))\n\n# after\nlayer = ZeroPadding2D(padding=((1, 1), (2, 2)))","handlingStrategy":"validation","validationCode":"def normalize_padding2d(p):\n    if isinstance(p, int):\n        return ((p, p), (p, p))\n    if len(p) != 2:\n        raise ValueError('padding must be int, (h, w), or ((h1,h2),(w1,w2))')\n    def pair(v):\n        return (v, v) if isinstance(v, int) else tuple(v)\n    return (pair(p[0]), pair(p[1]))\n\nlayer = ZeroPadding2D(padding=normalize_padding2d(cfg['padding']))","typeGuard":"def is_valid_padding2d(p) -> bool:\n    if isinstance(p, int):\n        return True\n    return hasattr(p, '__len__') and len(p) == 2","tryCatchPattern":null,"preventionTips":["Use Keras nested form ((top,bottom),(left,right)), not PyTorch's flat 4-tuple","Keep padding configs in the nested structure end-to-end","Write a helper that converts torch-style padding when porting models"],"tags":["keras","zero-padding2d","argument-validation","padding"],"backgroundTag":"invalid-argument-shape","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}