keras-team/keras · error · ValueError
Received an invalid value for `units`, expected a positive i
Error message
Received an invalid value for `units`, expected a positive integer. Received: units={units} What it means
Dense requires units to be a positive Python int (strict isinstance check: bools, floats, numpy ints, and strings all fail). Keras 3 hardened this check because the unit count determines the kernel shape; invalid values previously surfaced later as cryptic shape errors.
Source
Thrown at keras/src/layers/core/dense.py:102
def __init__(
self,
units,
activation=None,
use_bias=True,
kernel_initializer="glorot_uniform",
bias_initializer="zeros",
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
lora_rank=None,
lora_alpha=None,
quantization_config=None,
**kwargs,
):
if not isinstance(units, int) or units <= 0:
raise ValueError(
"Received an invalid value for `units`, expected a positive "
f"integer. Received: units={units}"
)
super().__init__(activity_regularizer=activity_regularizer, **kwargs)
self.units = units
self.activation = activations.get(activation)
self.use_bias = use_bias
self.kernel_initializer = initializers.get(kernel_initializer)
self.bias_initializer = initializers.get(bias_initializer)
self.kernel_regularizer = regularizers.get(kernel_regularizer)
self.bias_regularizer = regularizers.get(bias_regularizer)
self.kernel_constraint = constraints.get(kernel_constraint)
self.bias_constraint = constraints.get(bias_constraint)
self.lora_rank = lora_rank
self.lora_alpha = lora_alpha if lora_alpha is not None else lora_rank
self.lora_enabled = False
self.quantization_config = quantization_configView on GitHub (pinned to 7a34a03db6)
Solutions
- Coerce to a positive int before construction: units = int(units), ensure units > 0.
- Fix the config source to store a plain positive integer.
- Wrap numpy scalars with int() when passing in.
Example fix
# before units = float(cfg['units']) # 128.0 -> raises layer = keras.layers.Dense(units) # after units = int(cfg['units']) assert units > 0 layer = keras.layers.Dense(units)
Defensive patterns
Strategy: validation
Validate before calling
units = int(cfg['units'])
assert isinstance(units, int) and units > 0, f'bad units: {units!r}' Type guard
def valid_units(units) -> bool:
return isinstance(units, int) and not isinstance(units, bool) and units > 0 Prevention
- Coerce config values to int at load time
- Wrap numpy scalars with int() before passing to Dense
When it happens
Trigger: Calling keras.layers.Dense(units) with units as a float (128.0), np.int64, a string from config, or units <= 0.
Common situations: Units read from JSON/YAML config as string/float; hyperparameter sweeps yielding 0; numpy scalars from computations; Keras 2->3 migration where loose types were accepted.
Related errors
- You must build the layer before accessing `kernel`.
- adapt() expects an iterable that yields arrays or tensors wi
- Unsupported data type: {type(data)}. `adapt` supports `np.nd
- Expected an integer value for `n`, got {type(n)}.
- Expected an integer value for `size`, got {type(size)}.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/bb585994352eb803.
Report an issue: GitHub.