keras-team/keras · error · ValueError
When setting values directly, `mean` and `variance` must hav
Error message
When setting values directly, `mean` and `variance` must have the same shape. Received: mean shape {np.shape(mean)} and variance shape {np.shape(variance)} What it means
Normalization broadcasts mean and variance across the non-normalized axes, so the two arrays must have identical shapes. __init__ compares np.shape(mean) vs np.shape(variance) and raises on mismatch.
Source
Thrown at keras/src/layers/preprocessing/normalization.py:140
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}"
)
def build(self, input_shape):
if input_shape is None:
return
ndim = len(input_shape)View on GitHub (pinned to 7a34a03db6)
Solutions
- Reshape both to the same shape, typically via .ravel() or reshape to the kept-axis dims
- Recompute both statistics from the same dataset pass
- Verify: assert np.shape(mean) == np.shape(variance)
Example fix
// before layer = Normalization(axis=-1, mean=mu, variance=var) # (768,) vs (1,768) // after import numpy as np layer = Normalization(axis=-1, mean=np.ravel(mu), variance=np.ravel(var))
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
if np.shape(mean) != np.shape(variance):
mean, variance = np.ravel(mean), np.ravel(variance)
assert np.shape(mean) == np.shape(variance) Prevention
- Compute both statistics in the same pass over data
- ravel() both arrays defensively
When it happens
Trigger: Normalizing axis=-1 with mean of shape (768,) but variance of shape (1,768); statistics exported from different sources or with squeeze/reshape applied inconsistently.
Common situations: Loading channel statistics from separate files (mean.npy, std.npy then squaring); mixing per-feature stats computed at different data versions.
Related errors
- The layer was built with input_shape={self._build_input_shap
- The `weights` argument should be either `None` (random initi
- Expected mode to be one of `caffe`, `tf` or `torch`. Receive
- TF-IDF data must be a 1-index array. Received: type(idf_weig
- When using `output_mode={self.output_mode}` and `pad_to_max_
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/9a3575b06f40d0d7.
Report an issue: GitHub.