keras-team/keras · error · ValueError

Argument `size` should be a positive integer. Received: size

Error message

Argument `size` should be a positive integer. Received: size={size}

What it means

UpSampling1D.__init__ rejects size <= 0: upsampling by a zero or negative factor is meaningless and would produce an empty or invalid sequence length, so Keras fails at construction time.

Source

Thrown at keras/src/layers/reshaping/up_sampling1d.py:50

    Args:
        size: Integer. Upsampling factor.

    Input shape:
        3D tensor with shape: `(batch_size, steps, features)`.

    Output shape:
        3D tensor with shape: `(batch_size, upsampled_steps, features)`.
    """

    def __init__(self, size=2, **kwargs):
        super().__init__(**kwargs)
        if not isinstance(size, int) or isinstance(size, bool):
            raise TypeError(
                f"Expected an integer value for `size`, got {type(size)}."
            )
        if size <= 0:
            raise ValueError(
                "Argument `size` should be a positive integer. "
                f"Received: size={size}"
            )
        self.size = size
        self.input_spec = InputSpec(ndim=3)

    def compute_output_shape(self, input_shape):
        size = (
            self.size * input_shape[1] if input_shape[1] is not None else None
        )
        return [input_shape[0], size, input_shape[2]]

    def call(self, inputs):
        return ops.repeat(x=inputs, repeats=self.size, axis=1)

    def get_config(self):
        config = {"size": self.size}
        base_config = super().get_config()

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Clamp and validate the factor to >= 1 before constructing the layer
  2. Trace the upstream expression producing 0 — often integer division or a ratio < 1 truncated by int()
  3. Skip the upsampling layer entirely when the factor is 1 (identity) or 0 means 'no upsample' in your config semantics

Example fix

# before
factor = int(target_len // input_len)  # can be 0
layer = UpSampling1D(size=factor)

# after
factor = max(int(target_len // input_len), 1)
layer = UpSampling1D(size=factor)
Defensive patterns

Strategy: validation

Validate before calling

def validated_size(size):
    if not (isinstance(size, int) and not isinstance(size, bool)):
        raise TypeError('size must be int')
    if size <= 0:
        raise ValueError(f'size must be >= 1, got {size}')
    return size

Type guard

def is_positive_int(v) -> bool:
    return isinstance(v, int) and not isinstance(v, bool) and v > 0

Prevention

When it happens

Trigger: UpSampling1D(size=0) or UpSampling1D(size=-1), typically from a computed or config-supplied factor.

Common situations: Hyperparameter sweeps proposing 0 or negatives; size derived from a ratio that rounds or truncates to 0 (e.g. int(target/len) with small len); stale config defaults.

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


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