keras-team/keras · error · TypeError

Invalid value for `axis` argument: expected an int or a…

Error message

Invalid value for `axis` argument: expected an int or a list/tuple of ints. Received: axis={axis}

What it means

UnitNormalization.__init__ only accepts axis as an int or a list/tuple of ints. Any other type (None, string, float, tensor) fails the isinstance chain, since axis determines which dims the L2 normalization sums over.

Solutions

  1. Pass an int like axis=-1 or a list of ints like axis=[1, 2]
  2. Convert single-element arrays with int() or list() before constructing the layer
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at keras/src/layers/normalization/unit_normalization.py:34 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/9b4b6920496e4cee. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/layers/normalization/unit_normalization.py:34

    >>> normalized_data = keras.layers.UnitNormalization()(data)
    >>> np.sum(normalized_data[0, :] ** 2)
    1.0

    Args:
        axis: Integer or list/tuple. The axis or axes to normalize across.
            Typically, this is the features axis or axes. The left-out axes are
            typically the batch axis or axes. `-1` is the last dimension
            in the input. Defaults to `-1`.
    """

    def __init__(self, axis=-1, **kwargs):
        super().__init__(**kwargs)
        if isinstance(axis, (list, tuple)):
            self.axis = list(axis)
        elif isinstance(axis, int):
            self.axis = axis
        else:
            raise TypeError(
                "Invalid value for `axis` argument: "
                "expected an int or a list/tuple of ints. "
                f"Received: axis={axis}"
            )
        self.supports_masking = True

        self._build_at_init()

    def call(self, inputs):
        return ops.normalize(inputs, axis=self.axis, order=2, epsilon=1e-12)

    def compute_output_shape(self, input_shape):
        # Ensure axis is always treated as a list
        if isinstance(self.axis, int):
            axes = [self.axis]
        else:
            axes = self.axis

View on GitHub (pinned to 7a34a03db6)