keras-team/keras · error · ValueError
You must call `.build(input_shape)` on the layer before…
Error message
You must call `.build(input_shape)` on the layer before using it.
What it means
Generic build-order guard in Normalization.call(): the layer is being invoked before build(input_shape) has run and before adapt produced self.mean/self.variance, so there are no normalization statistics to apply.
Solutions
- Call layer.build(input_shape) explicitly, or build via layer(Input(...)) / model.build(...) before calling the layer on data
- When loading a saved model, ensure build weights are restored (e.g. call model(...) on sample data or use model.load_weights after building)
- If using adapt(), the layer builds automatically during adapt(); make sure adapt ran successfully before inference
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/layers/preprocessing/normalization.py:432 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/2356bd7ec7283d6e.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/layers/preprocessing/normalization.py:432
def finalize_state(self):
if self.input_mean is not None or not self.built:
return
# In the adapt case, we make constant tensors for mean and variance with
# proper broadcast shape and dtype each time `finalize_state` is called.
self.mean = ops.reshape(self.adapt_mean, self._broadcast_shape)
self.mean = ops.cast(self.mean, self.compute_dtype)
self.variance = ops.reshape(self.adapt_variance, self._broadcast_shape)
self.variance = ops.cast(self.variance, self.compute_dtype)
def call(self, inputs):
# This layer can be called in tf.data
# even with another backend after it has been adapted.
# However it must use backend-native logic for adapt().
if self.mean is None:
# May happen when in tf.data when mean/var was passed explicitly
raise ValueError(
"You must call `.build(input_shape)` "
"on the layer before using it."
)
inputs = self.backend.core.convert_to_tensor(
inputs, dtype=self.compute_dtype
)
# Ensure the weights are in the correct backend. Without this, it is
# possible to cause breakage when using this layer in tf.data.
mean = self.convert_weight(self.mean)
variance = self.convert_weight(self.variance)
if self.invert:
return self.backend.numpy.add(
mean,
self.backend.numpy.multiply(
inputs,
self.backend.numpy.maximum(
self.backend.numpy.sqrt(variance), backend.epsilon()
),View on GitHub (pinned to 7a34a03db6)