keras-team/keras · error · ValueError

`factor` argument cannot have an upper bound lesser than the

Error message

`factor` argument cannot have an upper bound lesser than the lower bound. Received: factor={factor}

What it means

The deprecated RandomHeight augmentation layer accepts `factor` either as a single number (interpreted symmetrically as (-factor, factor)) or as a 2-tuple (lower, upper). A tuple whose second element is smaller than the first (e.g. (0.4, 0.2)) makes __init__ raise this ValueError because the layer would sample uniformly from an empty interval.

Source

Thrown at keras/src/legacy/layers.py:80


@keras_export("keras._legacy.layers.RandomHeight")
class RandomHeight(Layer):
    """DEPRECATED."""

    def __init__(self, factor, interpolation="bilinear", seed=None, **kwargs):
        super().__init__(**kwargs)
        self.seed_generator = backend.random.SeedGenerator(seed)
        self.factor = factor
        if isinstance(factor, (tuple, list)):
            self.height_lower = factor[0]
            self.height_upper = factor[1]
        else:
            self.height_lower = -factor
            self.height_upper = factor

        if self.height_upper < self.height_lower:
            raise ValueError(
                "`factor` argument cannot have an upper bound lesser than the "
                f"lower bound. Received: factor={factor}"
            )
        if self.height_lower < -1.0 or self.height_upper < -1.0:
            raise ValueError(
                "`factor` argument must have values larger than -1. "
                f"Received: factor={factor}"
            )
        self.interpolation = interpolation
        self.seed = seed

    def call(self, inputs, training=True):
        inputs = tf.convert_to_tensor(inputs, dtype=self.compute_dtype)

        def random_height_inputs(inputs):
            """Inputs height-adjusted with random ops."""
            inputs_shape = tf.shape(inputs)
            img_hd = tf.cast(inputs_shape[-3], tf.float32)

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Order the tuple as (lower, upper), e.g. RandomHeight(factor=(0.2, 0.8))
  2. Use the modern keras.layers.RandomHeight, which validates the same documented contract
  3. Assert lower <= upper on augmentation configs at load time

Example fix

# before
layer = RandomHeight(factor=(0.8, 0.2))
# after
layer = RandomHeight(factor=(0.2, 0.8))
Defensive patterns

Strategy: validation

Validate before calling

lo, hi = tuple(factor) if isinstance(factor, (tuple, list)) else (-factor, factor)
assert lo <= hi, f'factor bounds crossed: {factor}'

Prevention

When it happens

Trigger: Constructing keras._legacy.layers.RandomHeight(factor=(0.8, 0.2)) or any (lower, upper) pair with upper < lower — commonly a swapped argument order.

Common situations: Swapping lower/upper when hand-writing augmentation configs; YAML sweeps where bounds are generated independently and can cross; porting from APIs whose argument order is (upper, lower).

Related errors


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