tf.keras.ops.norm

Matrix or vector norm.

This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.

x Input tensor.
ord Order of the norm (see table under Notes). The default is None.
axis If axis is an integer, it specifies the axis of x along which to compute the vector norms. If axis is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed.
keepdims If this is set to True, the axes which are reduced are left in the result as dimensions with size one.

For values of ord < 1, the result is, strictly speaking, not a mathematical 'norm', but it may still be useful for various numerical purposes. The following norms can be calculated:

  • For matrices:
    • ord=None: Frobenius norm
    • ord="fro": Frobenius norm
    • ord="nuc": nuclear norm
    • ord=np.inf: max(sum(abs(x), axis=1))
    • ord=-np.inf: min(sum(abs(x), axis=1))
    • ord=0: not supported
    • ord=1: max(sum(abs(x), axis=0))
    • ord=-1: min(sum(abs(x), axis=0))
    • ord=2: 2-norm (largest sing. value)
    • ord=-2: smallest singular value
    • other: not supported
  • For vectors:
    • ord=None: 2-norm
    • ord="fro": not supported
    • ord="nuc": not supported
    • ord=np.inf: max(abs(x))
    • ord=-np.inf: min(abs(x))
    • ord=0: sum(x != 0)
    • ord=1: as below
    • ord=-1: as below
    • ord=2: as below
    • ord=-2: as below
    • other: sum(abs(x)**ord)**(1./ord)

Norm of the matrix or vector(s).

Example:

x = keras.ops.reshape(keras.ops.arange(9, dtype="float32") - 4, (3, 3))
keras.ops.linalg.norm(x)
7.7459664