keras-team/keras · error · ValueError
The `num_bins` for `Hashing` cannot be `None` or non-positiv
Error message
The `num_bins` for `Hashing` cannot be `None` or non-positive values. Received: num_bins={num_bins}. What it means
Hashing maps values into num_bins buckets, so num_bins must be a positive integer. None, zero, or negative values make the hash space empty and are rejected at construction.
Source
Thrown at keras/src/layers/preprocessing/hashing.py:161
sparse=False,
**kwargs,
):
if not tf.available:
raise ImportError(
"Layer Hashing requires TensorFlow. "
"Install it via `pip install tensorflow`."
)
# By default, output int32 when output_mode='int' and floats otherwise.
if "dtype" not in kwargs or kwargs["dtype"] is None:
kwargs["dtype"] = (
"int64" if output_mode == "int" else backend.floatx()
)
super().__init__(**kwargs)
if num_bins is None or num_bins <= 0:
raise ValueError(
"The `num_bins` for `Hashing` cannot be `None` or "
f"non-positive values. Received: num_bins={num_bins}."
)
if output_mode == "int" and (
self.dtype_policy.name not in ("int32", "int64")
):
raise ValueError(
'When `output_mode="int"`, `dtype` should be an integer '
f"type, 'int32' or 'in64'. Received: dtype={kwargs['dtype']}"
)
# 'output_mode' must be one of (INT, ONE_HOT, MULTI_HOT, COUNT)
accepted_output_modes = ("int", "one_hot", "multi_hot", "count")
if output_mode not in accepted_output_modes:
raise ValueError(
"Invalid value for argument `output_mode`. "
f"Expected one of {accepted_output_modes}. "View on GitHub (pinned to 7a34a03db6)
Solutions
- Set num_bins to a positive integer larger than expected cardinality (common practice: a power of two like 2**18)
- Compute num_bins defensively: num_bins = max(1, estimated_cardinality)
- Do not leave num_bins unset in custom wrappers that forward None
Example fix
// before layer = Hashing(num_bins=len(vocab) - len(vocab)) # evaluates to 0 // after layer = Hashing(num_bins=max(2**18, len(vocab)))
Defensive patterns
Strategy: validation
Validate before calling
assert isinstance(num_bins, int) and num_bins > 0, "num_bins must be a positive int"
Type guard
def valid_num_bins(n):
return isinstance(n, int) and n > 0 Try / catch
catch ValueError from Hashing.__init__ and correct num_bins before constructing again
Prevention
- Set num_bins to a positive integer (e.g. 2**18 for high-cardinality hashing)
- Size num_bins above expected cardinality to limit collisions
When it happens
Trigger: Hashing(num_bins=0), Hashing(num_bins=-1), or Hashing(num_bins=None) — the constructor validates num_bins is a positive number.
Common situations: Passing num_bins=0 by mistake; deriving num_bins from data as len(set(x)) when the set is empty; config typo like num_bins=-1 or forgetting the argument (None).
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- `salt` can only be used when `oov_method='farmhash'`. Receiv
- Unknown activation function '{activation}' cannot be seriali
- Could not interpret activation function identifier: {identif
- ConvNeXt does not support the `channels_first` image data fo
- If using `weights="imagenet"` with `include_top=True`, `clas
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/9dcdc4d74d6520a7.
Report an issue: GitHub.