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

RandomErasing validates its factor/scale argument in _set_factor_by_name during __init__. The sequence form must be exactly two numbers; any other list/tuple length raises this ValueError before the layer is used, so an invalid erase-area range fails fast.

Source

Thrown at keras/src/layers/preprocessing/image_preprocessing/random_erasing.py:93

            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 factor as [lower, upper], e.g. RandomErasing(factor=[0.02, 0.33])
  2. Or pass a single number such as factor=0.2
  3. Validate config list lengths before layer construction

Example fix

# before
layers.RandomErasing(factor=[0.02])
# after
layers.RandomErasing(factor=[0.02, 0.33])
Defensive patterns

Strategy: validation

Validate before calling

f = [0.02, 0.33]
assert isinstance(f, (tuple, list)) and len(f) == 2, "factor must be [lower, upper]"

Type guard

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

Try / catch

try:
    layer = RandomErasing(factor=f)
except ValueError:
    layer = RandomErasing(factor=0.25)

Prevention

When it happens

Trigger: RandomErasing(factor=[0.02]) (single element), factor=[0.02, 0.2, 0.4], or factor=[] when constructing the layer.

Common situations: Converting RandomErasing from torchvision, where scale is a 2-tuple, and accidentally keeping a 3-element tuple from a custom sampler; config files with truncated lists; copy-paste from a layer that takes per-dimension triples.

Related errors


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