keras-team/keras · error · ValueError

`factor` argument must have values larger than -1. Received:

Error message

`factor` argument must have values larger than -1. Received: factor={factor}

What it means

RandomHeight scales image heights by a factor sampled from [height_lower, height_upper]; both bounds must exceed -1 so sampled factors stay meaningful (a factor of -1 would collapse the height to zero or negative). Passing factor <= -1, or a tuple containing such a value, triggers this ValueError in __init__.

Source

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

    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)
            img_wd = inputs_shape[-2]
            height_factor = backend.random.uniform(
                shape=[],
                minval=(1.0 + self.height_lower),
                maxval=(1.0 + self.height_upper),

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Keep both bounds strictly greater than -1, e.g. RandomHeight(factor=(-0.2, 0.3))
  2. Express fractions as decimals in (-1, inf), e.g. -0.5 not -50
  3. Validate factor bounds programmatically before constructing the layer

Example fix

# before
layer = RandomHeight(factor=(-1.5, 0.5))
# after
layer = RandomHeight(factor=(-0.5, 0.5))
Defensive patterns

Strategy: validation

Validate before calling

lo, hi = tuple(factor) if isinstance(factor, (tuple, list)) else (-factor, factor)
assert lo > -1 and hi > -1, f'factor bounds must be > -1: {factor}'

Prevention

When it happens

Trigger: Constructing RandomHeight(factor=-1.0), factor=-1.2, or factor=(-1.5, 0.2); note a single positive n becomes (-n, n), so a lone value below -1 also fails.

Common situations: Misreading factor as a percentage and passing -50 for -50%; copy-pasting crop/zoom parameters from other domains into height scaling; symmetric ranges like (-2, 2) that exceed the domain.

Related errors


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