keras-team/keras · error · ValueError

Received an invalid value for argument `units`, expected a p

Error message

Received an invalid value for argument `units`, expected a positive integer, got {units}.

What it means

SimpleRNN (and other RNN layers) validates that `units` is a positive integer in `__init__` before building the layer. `units` controls the dimensionality of the recurrent hidden state, so zero or negative values are meaningless and rejected immediately. The check runs at construction time, so the error surfaces before any data is seen.

Source

Thrown at keras/src/layers/rnn/simple_rnn.py:99

        units,
        activation="tanh",
        use_bias=True,
        kernel_initializer="glorot_uniform",
        recurrent_initializer="orthogonal",
        bias_initializer="zeros",
        kernel_regularizer=None,
        recurrent_regularizer=None,
        bias_regularizer=None,
        kernel_constraint=None,
        recurrent_constraint=None,
        bias_constraint=None,
        dropout=0.0,
        recurrent_dropout=0.0,
        seed=None,
        **kwargs,
    ):
        if units <= 0:
            raise ValueError(
                "Received an invalid value for argument `units`, "
                f"expected a positive integer, got {units}."
            )
        super().__init__(**kwargs)
        self.seed = seed
        self.seed_generator = backend.random.SeedGenerator(seed)

        self.units = units
        self.activation = activations.get(activation)
        self.use_bias = use_bias

        self.kernel_initializer = initializers.get(kernel_initializer)
        self.recurrent_initializer = initializers.get(recurrent_initializer)
        self.bias_initializer = initializers.get(bias_initializer)

        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.recurrent_regularizer = regularizers.get(recurrent_regularizer)
        self.bias_regularizer = regularizers.get(bias_regularizer)

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Set units to a positive integer, e.g. SimpleRNN(units=64)
  2. If units comes from a config, validate/clamp it to >= 1 before layer construction
  3. In hyperparameter searches, constrain the units search space to positive integers (e.g. [8, 16, 32, 64])

Example fix

# before
layer = keras.layers.SimpleRNN(units=0, input_shape=(10, 5))

# after
layer = keras.layers.SimpleRNN(units=64, input_shape=(10, 5))
Defensive patterns

Strategy: validation

Validate before calling

units = int(cfg.get('units', 0))
if units <= 0:
    raise ValueError(f'units must be a positive integer, got {units}')
layer = keras.layers.SimpleRNN(units=units)

Type guard

def is_valid_units(u) -> bool:
    return isinstance(u, int) and not isinstance(u, bool) and u > 0

Prevention

When it happens

Trigger: Calling keras.layers.SimpleRNN(units=0), SimpleRNN(units=-1), or passing a variable/config value that evaluates to <= 0 (e.g. a hyperparameter search that probes 0, or units derived from a computation that returned 0).

Common situations: Hyperparameter sweeps that include 0 in the search space; reading units from a YAML/JSON config where the key is missing and defaults to 0; copying tutorial code and editing units to a wrong value; programmatic model builders computing units from another quantity.

Related errors


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