keras-team/keras · error · ValueError

Received: {factor_name}={factor}

Error message

Received: {factor_name}={factor}

What it means

Raised by Solarization's _set_factor when `threshold_factor` is a sequence whose length is not 2. The threshold must be a single value in [0, 1] or a [lower, upper] pair (given as fractions of the value range; negative or None values disable thresholding).

Source

Thrown at keras/src/layers/preprocessing/image_preprocessing/solarization.py:102

        self._set_value_range(value_range)

    def _set_value_range(self, value_range):
        if not isinstance(value_range, (tuple, list)):
            raise ValueError(
                self._VALUE_RANGE_VALIDATION_ERROR
                + f"Received: value_range={value_range}"
            )
        if len(value_range) != 2:
            raise ValueError(
                self._VALUE_RANGE_VALIDATION_ERROR
                + f"Received: value_range={value_range}"
            )
        self.value_range = sorted(value_range)

    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)
            lower, upper = [0, factor]
        else:
            raise ValueError(
                self._FACTOR_VALIDATION_ERROR
                + f"Received: {factor_name}={factor}"
            )
        return lower, upper

    def _check_factor_range(self, input_number):
        if input_number > 1.0 or input_number < 0:

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass a single fraction, e.g. threshold_factor=0.5
  2. Pass exactly two bounds, e.g. threshold_factor=(0.2, 0.8)

Example fix

// before
layer = Solarization(value_range=(0, 255), threshold_factor=[0.2, 0.5, 0.8])
// after
layer = Solarization(value_range=(0, 255), threshold_factor=(0.2, 0.8))
Defensive patterns

Strategy: validation

Validate before calling

tf = threshold_factor
assert isinstance(tf, (int, float)) or (isinstance(tf, (tuple, list)) and len(tf) == 2), 'bad threshold_factor'

Type guard

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

Try / catch

try:
    layer = Solarization(value_range=(0, 255), threshold_factor=tf)
except ValueError as e:
    raise ValueError(f"Invalid threshold_factor {tf!r}") from e

Prevention

When it happens

Trigger: Calling Solarization(threshold_factor=[0.2, 0.5, 0.8]) or (0.5,).

Common situations: Hyperparameter sweeps emitting variable-length lists; adapting a 3-value threshold schedule.

Related errors


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