keras-team/keras · error · ValueError
Invalid Reduction Key: {key}. Expected keys are "{cls.all()}
Error message
Invalid Reduction Key: {key}. Expected keys are "{cls.all()}" What it means
Keras legacy losses validate the reduction argument against the allowed ReductionV2 keys (auto, none, sum, sum_over_batch_size). Passing anything else - e.g. the Keras 1 style 'sum_over_batch' or a string with wrong casing - raises this ValueError. It exists because reduction controls how per-sample losses are aggregated and an unrecognized key would silently change training math.
Source
Thrown at keras/src/legacy/losses.py:18
from keras.src.api_export import keras_export
@keras_export("keras._legacy.losses.Reduction")
class Reduction:
AUTO = "auto"
NONE = "none"
SUM = "sum"
SUM_OVER_BATCH_SIZE = "sum_over_batch_size"
@classmethod
def all(cls):
return (cls.AUTO, cls.NONE, cls.SUM, cls.SUM_OVER_BATCH_SIZE)
@classmethod
def validate(cls, key):
if key not in cls.all():
raise ValueError(
f'Invalid Reduction Key: {key}. Expected keys are "{cls.all()}"'
)
View on GitHub (pinned to 7a34a03db6)
Solutions
- Use one of the valid keys: 'auto', 'none', 'sum', or 'sum_over_batch_size'
- If loading a saved config, map legacy values: 'sum_over_batch' -> 'sum_over_batch_size', 'mean' -> 'sum_over_batch_size'
- Pass tf.keras.losses.Reduction enum members (Reduction.SUM_OVER_BATCH_SIZE) instead of raw strings
Example fix
# before loss = keras.losses.CategoricalCrossentropy(reduction='sum_over_batch') # after loss = keras.losses.CategoricalCrossentropy(reduction='sum_over_batch_size')
Defensive patterns
Strategy: validation
Validate before calling
from keras.src.legacy.losses import Reduction
valid = {'auto', 'none', 'sum', 'sum_over_batch_size'}
assert reduction in valid | {r.value for r in Reduction}, reduction Type guard
def is_valid_reduction(r) -> bool:
return r in {'auto', 'none', 'sum', 'sum_over_batch_size'} Try / catch
try:
loss = Loss(reduction=reduction)
except ValueError as e:
if 'Invalid Reduction Key' in str(e):
reduction = 'sum_over_batch_size'
else:
raise Prevention
- Normalize reduction values through one constant/config mapping
- Validate config-derived strings against the whitelist at load time
When it happens
Trigger: Calling keras.losses.* or a legacy loss class with reduction='sum_over_batch', 'mean', 'SUM', or a custom string; restoring reduction from a saved config/JSON that contains an outdated key.
Common situations: Migrating old Keras/TF 1.x scripts, loading models from JSON configs saved by older versions, mixing TF1/TF2 enums and strings.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Invalid quantization mode. Expected one of {dtype_policies.Q
- Invalid value for argument `output_mode`. Expected one of {a
- `sparse` may only be true if `output_mode` is `"one_hot"`, `
- The `salt` argument for `Hashing` can only be a tuple of siz
- {self._VALUE_RANGE_VALIDATION_ERROR}Received: value_range={v
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/446c989907ebce52.
Report an issue: GitHub.