keras-team/keras · error · TypeError
Expected an integer value for `size`, got
Error message
Expected an integer value for `size`, got {type(size)}. What it means
Raised by `UpSampling1D` when the `size` argument is not an integer (e.g. a float or string was passed). `size` must be a positive integer specifying the upsampling factor. Convert the value to an int before constructing the layer.
Solutions
- Cast at construction: UpSampling1D(size=int(size))
- Make the config source emit an integer (type the field as int in the schema)
- 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
- Type upsample factors as int in config schemas
- Cast tuner outputs with int(round(x))
- Never pass size=None; omit the arg to use the default 2
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.
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
- Argument `size` should be a positive integer. Received…
- Expected an integer value for `n`, got
- adapt() expects an iterable that yields arrays or tensors…
- All values in column x_col=
- Argument `n` should be a positive integer. Received: n=
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/409e9af784284001.
Report an issue: GitHub.
Appendix: 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)