keras-team/keras · error · ValueError

Axis of input tensor should have a defined dimension but…

Error message

Axis {self.axis} of input tensor should have a defined dimension but the layer received an input with shape {input_shape}.

What it means

Raised in GroupNormalization.build() when the dimension of the input tensor at self.axis is None (unknown), which happens when the layer is built from a symbolic/placeholder input whose axis extent is undetermined. GroupNormalization must slice the axis into self.groups channels, so an undefined extent cannot be validated or normalized.

Solutions

  1. Build the layer against an input whose dimension at the given axis is statically known (e.g. pass an explicit Input(shape=(...)) with a fixed channel count)
  2. If using a dataset with unknown shapes, set shapes explicitly or use tf.ensure_shape / a FixedSize padding so the axis extent is defined
  3. Use groups=-1 to infer groups from the channel dimension once the shape is known
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/layers/normalization/group_normalization.py:107 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/470b73eef408242a. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/layers/normalization/group_normalization.py:107

        self.supports_masking = True
        self.autocast = False
        self.groups = groups
        self.axis = axis
        self.epsilon = epsilon
        self.center = center
        self.scale = scale
        self.beta_initializer = initializers.get(beta_initializer)
        self.gamma_initializer = initializers.get(gamma_initializer)
        self.beta_regularizer = regularizers.get(beta_regularizer)
        self.gamma_regularizer = regularizers.get(gamma_regularizer)
        self.beta_constraint = constraints.get(beta_constraint)
        self.gamma_constraint = constraints.get(gamma_constraint)

    def build(self, input_shape):
        dim = input_shape[self.axis]

        if dim is None:
            raise ValueError(
                f"Axis {self.axis} of input tensor should have a defined "
                "dimension but the layer received an input with shape "
                f"{input_shape}."
            )

        if self.groups == -1:
            self.groups = dim

        if dim < self.groups:
            raise ValueError(
                f"Number of groups ({self.groups}) cannot be more than the "
                f"number of channels ({dim})."
            )

        if dim % self.groups != 0:
            raise ValueError(
                f"Number of groups ({self.groups}) must be a multiple "
                f"of the number of channels ({dim})."

View on GitHub (pinned to 7a34a03db6)