keras-team/keras · error · ValueError

The `{name}` argument should be a number (or a list of two n

Error message

The `{name}` argument should be a number (or a list of two numbers) in the range [{self._FACTOR_BOUNDS[0]}, {self._FACTOR_BOUNDS[1]}]. Received: factor={factor}

What it means

RandomElasticTransform normalizes its factor argument (e.g. alpha) via _set_factor_by_name. A factor may be a single number or exactly a two-element sequence giving lower and upper bounds; the sequence form must have len == 2. This ValueError fires when a sequence of any other length is passed.

Source

Thrown at keras/src/layers/preprocessing/image_preprocessing/random_elastic_transform.py:137

            self.height_axis = -2
            self.width_axis = -1
            self.channel_axis = -3
        else:
            self.height_axis = -3
            self.width_axis = -2
            self.channel_axis = -1

    def _set_factor_by_name(self, factor, name):
        error_msg = (
            f"The `{name}` argument should be a number "
            "(or a list of two numbers) "
            "in the range "
            f"[{self._FACTOR_BOUNDS[0]}, {self._FACTOR_BOUNDS[1]}]. "
            f"Received: factor={factor}"
        )
        if isinstance(factor, (tuple, list)):
            if len(factor) != 2:
                raise ValueError(error_msg)
            if (
                factor[0] > self._FACTOR_BOUNDS[1]
                or factor[1] < self._FACTOR_BOUNDS[0]
            ):
                raise ValueError(error_msg)
            lower, upper = sorted(factor)
        elif isinstance(factor, (int, float)):
            if (
                factor < self._FACTOR_BOUNDS[0]
                or factor > self._FACTOR_BOUNDS[1]
            ):
                raise ValueError(error_msg)
            factor = abs(factor)
            lower, upper = [max(-factor, self._FACTOR_BOUNDS[0]), factor]
        else:
            raise ValueError(error_msg)
        return lower, upper

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass a 2-element list [lower, upper], e.g. factor=[0.1, 0.5]
  2. Or pass a single number, e.g. factor=0.3
  3. Flatten doubly-nested config values before constructing the layer

Example fix

# before
layers.RandomElasticTransform(alpha_factor=[0.2])
# after
layers.RandomElasticTransform(alpha_factor=[0.0, 0.2])
Defensive patterns

Strategy: validation

Validate before calling

def normalize_factor(v):
    if isinstance(v, (tuple, list)):
        if len(v) != 2:
            raise ValueError("factor sequence must have exactly 2 elements")
        return list(v)
    return v

Type guard

def is_factor_pair(v) -> bool:
    return isinstance(v, (tuple, list)) and len(v) == 2

Try / catch

try:
    layer = RandomElasticTransform(alpha_factor=f)
except ValueError:
    f = [0.0, 0.5]
    layer = RandomElasticTransform(alpha_factor=f)

Prevention

When it happens

Trigger: factor=[0.1, 0.2, 0.3] (three entries), factor=[0.2] (single-element list), or an empty list factor=[].

Common situations: Reusing a config list meant for per-channel or per-parameter ranges; wrapping the factor in an extra list when loading from JSON/YAML (e.g. [[0.1, 0.2]]); hyperparameter-sweep code that emits variable-length lists.

Related errors


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