keras-team/keras · error · ValueError

You must call `.build(input_shape)` on the layer before usin

Error message

You must call `.build(input_shape)` on the layer before using it.

What it means

Error "You must call `.build(input_shape)` on the layer before using it." thrown in keras-team/keras.

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)

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.