keras-team/keras · error · ValueError

When setting values directly, both `mean` and `variance` mus

Error message

When setting values directly, both `mean` and `variance` must be set. Received: mean={mean} and variance={variance}

What it means

Normalization accepts direct mean/variance inputs only as a pair. __init__ raises when exactly one of mean, variance is None, because normalization (x-mean)/sqrt(var) needs both to be well defined.

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)

Solutions

  1. Supply both mean and variance with identical shapes
  2. Or supply neither and call layer.adapt(data) to compute them
  3. 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

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 parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/4e2e220ea153c3bb. Report an issue: GitHub.