{"record":{"id":"4e2e220ea153c3bb","repo":"keras-team/keras","slug":"when-setting-values-directly-both-mean-and-var","errorCode":null,"errorMessage":"When setting values directly, both `mean` and `variance` must be set. Received: mean={mean} and variance={variance}","messagePattern":"When setting values directly, both `mean` and `variance` must be set\\. Received: mean=(.+?) and variance=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/normalization.py","lineNumber":133,"sourceCode":"        # Standardize `axis` to a tuple.\n        if axis is None:\n            axis = ()\n        elif isinstance(axis, int):\n            axis = (axis,)\n        else:\n            axis = tuple(axis)\n        self.axis = axis\n\n        self.input_mean = mean\n        self.input_variance = variance\n        self.invert = invert\n        self.supports_masking = True\n        self._build_input_shape = None\n        self.mean = None\n\n        # Set `mean` and `variance` if passed.\n        if (mean is not None) != (variance is not None):\n            raise ValueError(\n                \"When setting values directly, both `mean` and `variance` \"\n                f\"must be set. Received: mean={mean} and variance={variance}\"\n            )\n        if mean is not None:\n            # Verify mean and variance have the same shape.\n            if np.shape(mean) != np.shape(variance):\n                raise ValueError(\n                    \"When setting values directly, `mean` and `variance` \"\n                    \"must have the same shape. Received: \"\n                    f\"mean shape {np.shape(mean)} and \"\n                    f\"variance shape {np.shape(variance)}\"\n                )\n            # Verify mean rank <= number of axes.\n            if len(np.shape(mean)) > len(self.axis):\n                raise ValueError(\n                    \"The rank of `mean` must be less than or equal to the \"\n                    f\"number of axes ({len(self.axis)}). Received: \"\n                    f\"mean shape {np.shape(mean)} for axis {self.axis}\"","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/normalization.py#L115-L151","documentation":"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.","triggerScenarios":"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.","commonSituations":"Hand-loading preprocessing statistics from a JSON/CSV where one field is missing; refactoring pipelines that previously used adapt().","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"],"exampleFix":"// before\nlayer = Normalization(mean=mu)\n// after\nlayer = Normalization(mean=mu, variance=sigma2)","handlingStrategy":"validation","validationCode":"assert (mean is None) == (variance is None), 'set both or neither'","typeGuard":"def stats_pair_valid(mean, variance):\n    return (mean is None) == (variance is None)","tryCatchPattern":null,"preventionTips":["Store mean and variance together in one stats file","Load stats via a helper that fails if either key is missing"],"tags":["keras","normalization","argument-validation"],"backgroundTag":"missing-required-parameter","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}