{"record":{"id":"c92ef971f6e32e45","repo":"keras-team/keras","slug":"received-an-invalid-value-for-argument-units-ex-c92ef9","errorCode":null,"errorMessage":"Received an invalid value for argument `units`, expected a positive integer, got {units}.","messagePattern":"Received an invalid value for argument `units`, expected a positive integer, got (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/rnn/simple_rnn.py","lineNumber":99,"sourceCode":"        units,\n        activation=\"tanh\",\n        use_bias=True,\n        kernel_initializer=\"glorot_uniform\",\n        recurrent_initializer=\"orthogonal\",\n        bias_initializer=\"zeros\",\n        kernel_regularizer=None,\n        recurrent_regularizer=None,\n        bias_regularizer=None,\n        kernel_constraint=None,\n        recurrent_constraint=None,\n        bias_constraint=None,\n        dropout=0.0,\n        recurrent_dropout=0.0,\n        seed=None,\n        **kwargs,\n    ):\n        if units <= 0:\n            raise ValueError(\n                \"Received an invalid value for argument `units`, \"\n                f\"expected a positive integer, got {units}.\"\n            )\n        super().__init__(**kwargs)\n        self.seed = seed\n        self.seed_generator = backend.random.SeedGenerator(seed)\n\n        self.units = units\n        self.activation = activations.get(activation)\n        self.use_bias = use_bias\n\n        self.kernel_initializer = initializers.get(kernel_initializer)\n        self.recurrent_initializer = initializers.get(recurrent_initializer)\n        self.bias_initializer = initializers.get(bias_initializer)\n\n        self.kernel_regularizer = regularizers.get(kernel_regularizer)\n        self.recurrent_regularizer = regularizers.get(recurrent_regularizer)\n        self.bias_regularizer = regularizers.get(bias_regularizer)","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/rnn/simple_rnn.py#L81-L117","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Set units to a positive integer, e.g. SimpleRNN(units=64)","If units comes from a config, validate/clamp it to >= 1 before layer construction","In hyperparameter searches, constrain the units search space to positive integers (e.g. [8, 16, 32, 64])"],"exampleFix":"# before\nlayer = keras.layers.SimpleRNN(units=0, input_shape=(10, 5))\n\n# after\nlayer = keras.layers.SimpleRNN(units=64, input_shape=(10, 5))","handlingStrategy":"validation","validationCode":"units = int(cfg.get('units', 0))\nif units <= 0:\n    raise ValueError(f'units must be a positive integer, got {units}')\nlayer = keras.layers.SimpleRNN(units=units)","typeGuard":"def is_valid_units(u) -> bool:\n    return isinstance(u, int) and not isinstance(u, bool) and u > 0","tryCatchPattern":null,"preventionTips":["Constrain hyperparameter search spaces for units to positive integers","Validate config-derived layer parameters before model construction"],"tags":["keras","rnn","constructor-validation","units"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}