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 [0, 1.0]. Received: {factor_name}={factor}

What it means

Raised by RandomShear's constructor when the `factor` argument is a tuple or list whose length is not exactly 2. Keras expects either a single float in [0, 1.0] or a pair [lower, upper] defining the shear range; any other sequence length is rejected immediately at layer construction.

Source

Thrown at keras/src/layers/preprocessing/image_preprocessing/random_shear.py:118

                f"{self._SUPPORTED_FILL_MODE}."
            )
        if interpolation not in self._SUPPORTED_INTERPOLATION:
            raise NotImplementedError(
                f"Unknown `interpolation` {interpolation}. Expected of one "
                f"{self._SUPPORTED_INTERPOLATION}."
            )

        self.fill_mode = fill_mode
        self.fill_value = fill_value
        self.interpolation = interpolation
        self.seed = seed
        self.generator = SeedGenerator(seed)
        self.supports_jit = False

    def _set_factor_with_name(self, factor, factor_name):
        if isinstance(factor, (tuple, list)):
            if len(factor) != 2:
                raise ValueError(
                    self._FACTOR_VALIDATION_ERROR
                    + f"Received: {factor_name}={factor}"
                )
            self._check_factor_range(factor[0])
            self._check_factor_range(factor[1])
            lower, upper = sorted(factor)
        elif isinstance(factor, (int, float)):
            self._check_factor_range(factor)
            factor = abs(factor)
            lower, upper = [-factor, factor]
        else:
            raise ValueError(
                self._FACTOR_VALIDATION_ERROR
                + f"Received: {factor_name}={factor}"
            )
        return lower, upper

    def _check_factor_range(self, input_number):

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass a single number, e.g. RandomShear(factor=0.2)
  2. Pass exactly two numbers as lower/upper bounds, e.g. RandomShear(factor=(0.1, 0.5))

Example fix

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

Strategy: validation

Validate before calling

def valid_shear_factor(f):
    if isinstance(f, (int, float)):
        return 0.0 <= f <= 1.0
    return isinstance(f, (tuple, list)) and len(f) == 2 and all(isinstance(x, (int, float)) and 0.0 <= x <= 1.0 for x in f)

Type guard

def is_shear_factor(f) -> bool:
    return (isinstance(f, (int, float)) and 0.0 <= f <= 1.0) or (
        isinstance(f, (tuple, list)) and len(f) == 2
        and all(isinstance(x, (int, float)) and 0.0 <= x <= 1.0 for x in f)
    )

Try / catch

try:
    layer = keras.layers.RandomShear(factor=f)
except ValueError as e:
    raise ValueError(f"Invalid shear factor from config: {f!r}") from e

Prevention

When it happens

Trigger: Calling layers.RandomShear(factor=[0.1, 0.2, 0.3]) or RandomShear(factor=(0.2,)) — any tuple/list with len != 2 passed to __init__ via _set_factor_with_name.

Common situations: Copying a config from an augmentation pipeline that used three values, passing a numpy array of shape (3,), or passing a nested list from a YAML/JSON hyperparameter file.

Related errors


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