keras-team/keras · error · ValueError

All `axis` values must be in the range [-ndim, ndim). Receiv

Error message

All `axis` values must be in the range [-ndim, ndim). Received inputs with ndim={ndim}, while axis={self.axis}

What it means

During build(), Normalization validates that every entry of self.axis lies in [-ndim, ndim) of the actual input. Values outside that half-open range are rejected because no such axis exists to normalize.

Source

Thrown at keras/src/layers/preprocessing/normalization.py:162

                    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)
        self._build_input_shape = input_shape

        if any(a < -ndim or a >= ndim for a in self.axis):
            raise ValueError(
                "All `axis` values must be in the range [-ndim, ndim). "
                f"Received inputs with ndim={ndim}, while axis={self.axis}"
            )

        # Axes to be kept, replacing negative values with positive equivalents.
        # Sorted to avoid transposing axes.
        self._keep_axis = tuple(
            sorted([d if d >= 0 else d + ndim for d in self.axis])
        )
        # All axes to be kept should have known shape.
        for d in self._keep_axis:
            if input_shape[d] is None:
                raise ValueError(
                    "All `axis` values to be kept must have a known shape. "
                    f"Received axis={self.axis}, "
                    f"inputs.shape={input_shape}, "
                    f"with unknown axis at index {d}"
                )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Set axis to a valid index for your input rank, commonly -1 (last/feature axis)
  2. Ensure the input has the expected rank, e.g. expand dims for a missing channel axis
  3. Sanity-check: assert -ndim <= axis < ndim before building

Example fix

// before
layer = Normalization(axis=2)
layer.build((None, 10))  # ValueError
// after
layer = Normalization(axis=-1)
layer.build((None, 10))
Defensive patterns

Strategy: validation

Validate before calling

ndim = len(input_shape)
assert all(-ndim <= a < ndim for a in axis_list), 'axis out of range'

Type guard

def axis_ok(axis, ndim): return all(-ndim <= a < ndim for a in axis)

Prevention

When it happens

Trigger: Normalization(axis=2) receiving 2-D input (batch, features); axis=-3 on 2-D input; a saved layer whose axis fit the training data but not the new input shape.

Common situations: Reusing a layer across models with different input ranks (sequence vs single sample); off-by-one axis values copied from another layer's config.

Related errors


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