keras-team/keras · error · ValueError

Invalid `ord` argument. Expected one of {'fro', 'nuc'} when

Error message

Invalid `ord` argument. Expected one of {'fro', 'nuc'} when using string. Received: ord={ord}

What it means

Norm.__init__ validates the ord argument: when a string is supplied it must be exactly 'fro' (Frobenius) or 'nuc' (nuclear). These string norms are only defined for matrices, so anything else — 'l2', 'inf', 'L2' — is rejected at construction time.

Source

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

    x = backend.convert_to_tensor(x)
    _assert_2d(x)
    if backend.backend() == "tensorflow":
        try:
            _assert_square(x)
        except ValueError as e:
            raise ValueError(
                f"LU decomposition failed: {e}. LU decomposition is only "
                "supported for square matrices in Tensorflow."
            )
    return backend.linalg.lu_factor(x)


class Norm(Operation):
    def __init__(self, ord=None, axis=None, keepdims=False, *, name=None):
        super().__init__(name=name)
        if isinstance(ord, str):
            if ord not in ("fro", "nuc"):
                raise ValueError(
                    "Invalid `ord` argument. "
                    "Expected one of {'fro', 'nuc'} when using string. "
                    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

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Use numeric ords for vectors/matrices: 1, 2, np.inf, -np.inf.
  2. Use 'fro' or 'nuc' strings only, and only with 2 axes.
  3. Fix casing/typos: it is 'fro', not 'Fro' or 'frobenius'.

Example fix

# before
n = keras.ops.linalg.norm(x, ord='l2')

# after
n = keras.ops.linalg.norm(x, ord=2)
Defensive patterns

Strategy: validation

Validate before calling

assert not isinstance(ord, str) or ord in ('fro', 'nuc'), ord

Type guard

def valid_ord(o):
    return o is None or (isinstance(o, (int, float)) and not isinstance(o, bool)) or (isinstance(o, str) and o in ('fro', 'nuc'))

Prevention

When it happens

Trigger: keras.ops.linalg.Norm(ord='l2'); keras.ops.linalg.norm(x, ord='inf'); passing a NumPy-style string not in the allowed set.

Common situations: Porting code from numpy.linalg.norm and assuming string ords exist; typos or casing issues like 'Fro' or 'frobenius'.

Related errors


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