keras-team/keras · error · ValueError

The `factor` argument should be a number (or a list of two n

Error message

The `factor` 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

When factor is a tuple/list, the base image preprocessing layer first checks it has exactly two elements. This instance of the shared error message is raised when the sequence length differs from 2, e.g. factor=[0.1, 0.2, 0.3] or factor=[0.5].

Source

Thrown at keras/src/layers/preprocessing/image_preprocessing/base_image_preprocessing_layer.py:39

            factor = factor or 0.0
            self._set_factor(factor)
        elif factor is not None:
            raise ValueError(
                f"Layer {self.__class__.__name__} does not take "
                f"a `factor` argument. Received: factor={factor}"
            )

    def _set_factor(self, factor):
        error_msg = (
            "The `factor` 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)
        self.factor = lower, upper

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass exactly two values (lower, upper), e.g. factor=(0.1, 0.2).
  2. Or pass a single float for a symmetric range, e.g. factor=0.2 means (-0.2, 0.2).

Example fix

# before
layer = keras.layers.RandomContrast(factor=[0.1, 0.2, 0.3])
# after
layer = keras.layers.RandomContrast(factor=(0.1, 0.2))
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(factor, (tuple, list)):
    assert len(factor) == 2, f"factor sequence must have 2 elements, got {len(factor)}"

Type guard

def is_valid_factor(f):
    if isinstance(f, (int, float)):
        return True
    return isinstance(f, (tuple, list)) and len(f) == 2 and all(isinstance(x, (int, float)) for x in f)

Prevention

When it happens

Trigger: Passing a list/tuple factor whose length is not 2 to a factor-based layer such as RandomContrast or Solarization.

Common situations: Generating factor ranges programmatically (e.g. a sweep of many values) and passing the whole list instead of picking endpoints.

Related errors


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