keras-team/keras · error · ValueError

Received: {factor_name}={factor}

Error message

Received: {factor_name}={factor}

What it means

Raised by RandomTranslation's _set_factor when width_factor or height_factor is a tuple/list whose length is not 2. Translation factors must be a single number in [-1.0, 1.0] or a 2-element [lower, upper] pair.

Source

Thrown at keras/src/layers/preprocessing/image_preprocessing/random_translation.py:149

                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(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 fraction, e.g. width_factor=0.2 (means up to ±20%)
  2. Pass exactly two values as bounds, e.g. width_factor=(0.05, 0.2)

Example fix

// before
layer = RandomTranslation(0.1, [0.1])
// after
layer = RandomTranslation(0.1, (0.0, 0.1))
Defensive patterns

Strategy: validation

Validate before calling

for name, f in (("height_factor", h), ("width_factor", w)):
    assert isinstance(f, (int, float)) or (isinstance(f, (tuple, list)) and len(f) == 2), name

Type guard

import numbers
def is_translation_factor(f):
    return isinstance(f, numbers.Number) or (isinstance(f, (tuple, list)) and len(f) == 2 and all(isinstance(x, numbers.Number) for x in f))

Try / catch

try:
    layer = RandomTranslation(h, w)
except ValueError as e:
    raise ValueError(f"Bad translation factors: h={h!r}, w={w!r}") from e

Prevention

When it happens

Trigger: Calling RandomTranslation(height_factor=0.1, width_factor=[0.1]) or width_factor=(0.05, 0.1, 0.2) — any sequence with len != 2.

Common situations: Specifying per-axis ranges in config files and accidentally supplying one or three values; passing a 1-element list from a hyperparameter search.

Related errors


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