Computes the norm of vectors, matrices, and tensors.
Aliases:
tf.compat.v2.linalg.normtf.compat.v2.normtf.linalg.norm
tf.norm(
tensor,
ord='euclidean',
axis=None,
keepdims=None,
name=None
)
This function can compute several different vector norms (the 1-norm, the Euclidean or 2-norm, the inf-norm, and in general the p-norm for p > 0) and matrix norms (Frobenius, 1-norm, 2-norm and inf-norm).
Args:
tensor:Tensorof typesfloat32,float64,complex64,complex128ord: Order of the norm. Supported values are'fro','euclidean',1,2,np.infand any positive real number yielding the corresponding p-norm. Default is'euclidean'which is equivalent to Frobenius norm iftensoris a matrix and equivalent to2-norm for vectors. Some restrictions apply: a) The Frobenius norm'fro'is not defined for vectors, b) Ifaxisis a2-tuple (matrix norm), only'euclidean','fro',1,2,np.infare supported. See the description ofaxison how to compute norms for a batch of vectors or matrices stored in atensor.axis: IfaxisisNone(the default), the input is considered a vector and a single vector norm is computed over the entire set of values in thetensor, i.e. norm(tensor,ord=ord) is equivalent to norm(reshape(tensor, [-1]),ord=ord). Ifaxisis a Python integer, the input is considered a batch of vectors, andaxisdetermines theaxisintensorover which to compute vector norms. Ifaxisis a2-tuple of Python integers it is considered a batch of matrices andaxisdetermines the axes intensorover which to compute a matrix norm. Negative indices are supported. Example: If you are passing atensorthat can be either a matrix or a batch of matrices at runtime, passaxis=[-2,-1] instead ofaxis=Noneto make sure that matrix norms are computed.- ``: If True, the
axisindicated inaxisare kept with size1. Otherwise, the dimensions inaxisare removed from the output shape. : Theof the op.
Returns:
output: ATensorof the same type astensor, containing the vector or matrix norms. Ifkeepdimsis True then the rank ofoutputis equal to the rank oftensor. Otherwise, ifaxisis none theoutputis a scalar, ifaxisis an integer, the rank ofoutputis one less than the rank oftensor, ifaxisis a 2-tuple the rank ofoutputis two less than the rank oftensor.
Raises:
ValueError: Ifordoraxisis invalid.
Numpy Compatibility
Mostly equivalent to numpy.linalg.norm. Not supported: ord <= 0, 2-norm for matrices, nuclear norm. Other differences: a) If axis is None, treats the flattened tensor as a vector regardless of rank. b) Explicitly supports 'euclidean' norm as the default, including for higher order tensors.