keras-team/keras · error · ValueError

Invalid `ord` argument for vector norm. Received: ord={self.

Error message

Invalid `ord` argument for vector norm. Received: ord={self.ord}

What it means

Norm.compute_output_spec checks that string ords ('fro'/'nuc') are only used when the reduction spans 2 axes (a matrix). If axis is None or resolves to a single axis (a vector), a string ord is meaningless and this error is raised.

Source

Thrown at keras/src/ops/linalg.py:309

                    f"Received: ord={ord}"
                )
        if isinstance(axis, int):
            axis = [axis]
        self.ord = ord
        self.axis = axis
        self.keepdims = keepdims

    def compute_output_spec(self, x):
        output_dtype = backend.standardize_dtype(x.dtype)
        if "int" in output_dtype or output_dtype == "bool":
            output_dtype = backend.floatx()
        if self.axis is None:
            axis = tuple(range(len(x.shape)))
        else:
            axis = self.axis
        num_axes = len(axis)
        if num_axes == 1 and isinstance(self.ord, str):
            raise ValueError(
                "Invalid `ord` argument for vector norm. "
                f"Received: ord={self.ord}"
            )
        elif num_axes == 2 and self.ord not in (
            None,
            "fro",
            "nuc",
            float("inf"),
            float("-inf"),
            1,
            -1,
            2,
            -2,
        ):
            raise ValueError(
                "Invalid `ord` argument for matrix norm. "
                f"Received: ord={self.ord}"
            )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. For vector norms use ord=None or numeric ords (1, 2, inf).
  2. Restrict 'fro'/'nuc' to axis settings covering exactly 2 dimensions.
  3. Branch on tensor rank: strings only when len(axis) == 2.

Example fix

# before
row_norms = keras.ops.linalg.norm(X, ord='fro', axis=1)

# after
row_norms = keras.ops.linalg.norm(X, ord=2, axis=1)
Defensive patterns

Strategy: validation

Validate before calling

axes = tuple(range(len(x.shape))) if axis is None else (axis,)
if len(axes) == 1:
    assert not isinstance(ord, str), 'string ord only valid for 2-axis norms'

Type guard

def norm_args_consistent(x, ord, axis):
    axes = tuple(range(len(x.shape))) if axis is None else (axis,)
    return not (len(axes) == 1 and isinstance(ord, str))

Prevention

When it happens

Trigger: keras.ops.linalg.norm(vector, ord='fro'); Norm(axis=1, ord='nuc') applied to a batch of vectors.

Common situations: Writing generic norm code that passes ord='fro' for all inputs; switching a norm call from full-matrix to per-row reduction without updating ord.

Related errors


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