keras-team/keras · error · ValueError
When setting values directly, both `mean` and `variance`…
Error message
When setting values directly, both `mean` and `variance` must be set. Received: mean={mean} and variance={variance} What it means
Normalization can be initialized with a fixed mean and variance instead of calling adapt(), but they must be supplied together since a Gaussian standardization needs both. This ValueError fires at construction when exactly one of `mean`/`variance` is None (i.e. only one was provided).
Solutions
- Supply both mean and variance with identical shapes
- Or supply neither and call layer.adapt(data) to compute them
- If you truly only know one statistic, you cannot use direct-set mode; use adapt
Example fix
// before layer = Normalization(mean=mu) // after layer = Normalization(mean=mu, variance=sigma2)
Defensive patterns
Strategy: validation
Validate before calling
assert (mean is None) == (variance is None), 'set both or neither'
Type guard
def stats_pair_valid(mean, variance):
return (mean is None) == (variance is None) Prevention
- Store mean and variance together in one stats file
- Load stats via a helper that fails if either key is missing
When it happens
Trigger: keras.layers.Normalization(mean=[0.5]) without variance, or Normalization(variance=[0.1]) without mean; passing one precomputed statistic and expecting the layer to infer the other.
Common situations: Hand-loading preprocessing statistics from a JSON/CSV where one field is missing; refactoring pipelines that previously used adapt().
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- adapt() expects an iterable that yields arrays or tensors…
- adapt() received an empty iterable (no batches). Expected…
- All `axis` values must be in the range
- All `axis` values to be kept must have a known shape…
- Argument `n` should be a positive integer. Received: n=
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/4e2e220ea153c3bb.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/layers/preprocessing/normalization.py:133
# Standardize `axis` to a tuple.
if axis is None:
axis = ()
elif isinstance(axis, int):
axis = (axis,)
else:
axis = tuple(axis)
self.axis = axis
self.input_mean = mean
self.input_variance = variance
self.invert = invert
self.supports_masking = True
self._build_input_shape = None
self.mean = None
# Set `mean` and `variance` if passed.
if (mean is not None) != (variance is not None):
raise ValueError(
"When setting values directly, both `mean` and `variance` "
f"must be set. Received: mean={mean} and variance={variance}"
)
if mean is not None:
# Verify mean and variance have the same shape.
if np.shape(mean) != np.shape(variance):
raise ValueError(
"When setting values directly, `mean` and `variance` "
"must have the same shape. Received: "
f"mean shape {np.shape(mean)} and "
f"variance shape {np.shape(variance)}"
)
# Verify mean rank <= number of axes.
if len(np.shape(mean)) > len(self.axis):
raise ValueError(
"The rank of `mean` must be less than or equal to the "
f"number of axes ({len(self.axis)}). Received: "
f"mean shape {np.shape(mean)} for axis {self.axis}"View on GitHub (pinned to 7a34a03db6)