keras-team/keras · error · ValueError

Invalid `output_padding` argument. Each value in `output_pad

Error message

Invalid `output_padding` argument. Each value in `output_padding` must be strictly less than the corresponding `strides` value.
At index {i}, `output_padding` is {op} and `strides` is {s}.
Received: output_padding={self.output_padding}, strides={self.strides}.

What it means

In transpose convolutions, output_padding adds extra size to the output shape and must be strictly smaller than the stride per spatial dimension, otherwise the shape formula is ambiguous/invalid. The constructor validates each (output_padding[i], strides[i]) pair and raises when output_padding[i] >= strides[i].

Source

Thrown at keras/src/layers/convolutional/base_conv_transpose.py:168

                    raise ValueError(
                        "`output_padding` must be strictly less than "
                        f"`strides` for all dimensions. At dimension {i}, "
                        f"`output_padding` is {op} but `strides` is {s}. "
                        f"Received: output_padding={self.output_padding}, "
                        f"strides={self.strides}"
                    )

        if max(self.strides) > 1 and max(self.dilation_rate) > 1:
            raise ValueError(
                "`strides > 1` not supported in conjunction with "
                f"`dilation_rate > 1`. Received: strides={self.strides} and "
                f"dilation_rate={self.dilation_rate}"
            )

        if self.output_padding is not None:
            for i, (op, s) in enumerate(zip(self.output_padding, self.strides)):
                if op >= s:
                    raise ValueError(
                        "Invalid `output_padding` argument. "
                        "Each value in `output_padding` must be strictly "
                        "less than the corresponding `strides` value.\n"
                        f"At index {i}, `output_padding` is {op} and `strides` "
                        f"is {s}.\n"
                        f"Received: output_padding={self.output_padding}, "
                        f"strides={self.strides}."
                    )

    def build(self, input_shape):
        if self.data_format == "channels_last":
            channel_axis = -1
            input_channel = input_shape[-1]
        else:
            channel_axis = 1
            input_channel = input_shape[1]
        self.input_spec = InputSpec(
            min_ndim=self.rank + 2, axes={channel_axis: input_channel}

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Set each output_padding[i] strictly less than strides[i] (commonly strides[i]-1).
  2. If targeting an exact output size, adjust padding/cropping instead of output_padding.
  3. Precompute in config: all(op < s for op, s in zip(output_padding, strides)).

Example fix

# before
keras.layers.Conv2DTranspose(64, 3, strides=2, padding='same', output_padding=(2, 2))

# after
keras.layers.Conv2DTranspose(64, 3, strides=2, padding='same', output_padding=(1, 1))
Defensive patterns

Strategy: validation

Validate before calling

assert all(op < s for op, s in zip(output_padding, strides)), 'output_padding must be < strides elementwise'

Type guard

def valid_output_padding(output_padding, strides) -> bool:
    return all(op < s for op, s in zip(output_padding, strides))

Prevention

When it happens

Trigger: Creating Conv2DTranspose/Conv1DTranspose/Conv3DTranspose with output_padding=(2,2) and strides=(2,2), or any index where the padding value is not strictly less than the stride.

Common situations: Porting PyTorch nn.ConvTranspose2d configs where output_padding equals stride; trying to hit an exact output size and overshooting the padding value.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/90ba5629181e76af. Report an issue: GitHub.