keras-team/keras · error · TypeError

Expected an integer value for `size`, got {type(size)}.

Error message

Expected an integer value for `size`, got {type(size)}.

What it means

UpSampling1D.__init__ requires size to be a real Python int (bools explicitly rejected because bool subclasses int). Floats, strings and None raise this TypeError at layer construction.

Source

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

     [[ 6.  7.  8.]
      [ 6.  7.  8.]
      [ 9. 10. 11.]
      [ 9. 10. 11.]]]

    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)

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Cast at construction: UpSampling1D(size=int(size))
  2. Make the config source emit an integer (type the field as int in the schema)
  3. If a tuner proposes the value, round and cast in the objective function

Example fix

# before
size = cfg.get('upsample', 2.0)
layer = UpSampling1D(size=size)  # TypeError

# after
size = int(cfg.get('upsample', 2))
layer = UpSampling1D(size=size)
Defensive patterns

Strategy: type-guard

Validate before calling

def coerce_size(size):
    if not (isinstance(size, int) and not isinstance(size, bool)):
        raise TypeError(f'size must be int, got {type(size)}')
    return size

Type guard

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

Prevention

When it happens

Trigger: UpSampling1D(size=2.0), UpSampling1D(size='2'), UpSampling1D(size=None), or UpSampling1D(size=True). Raised before any input is seen.

Common situations: Config files (YAML/JSON) yielding floats like 2.0; hyperparameter tuners returning continuous values that should be integers; copying size from a variable typed as float.

Related errors


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