# megengine.functional.norm¶

norm(inp, ord=None, axis=None, keepdims=False)[源代码]

Calculates the norm of tensor elements over a given axis.

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

• inp (Tensor) – input tensor. Should have a numeric data type.

• ord (Optional[float]) – Order of the norm (see table under Notes). If not specified, the default is 2.

• axis (Optional[int]) – Axis along which to compute vector norms. If axis is an integer, it specifies the axis of inp along which to compute the vector norms.

• keepdims – If this is set to True, the axes which are normed over are left in the result as dimensions with size one.

Norm of the matrix or vector(s).

Now the following norms can be calculated:

• inf: norm-$$\infty$$ (maximum of absolute values).

• -inf: norm-$$-\infty$$ (minimum of absolute values).

• 2: 2-norm (largest singluar value).

The Frobenius norm is given by to sum(abs(x)**ord)**(1./ord):

$\|A\|_F=\left[\sum_{i, j} a b s\left(a_{i, j}\right)^2\right]^{1 / 2}$

>>> import math
>>> x = Tensor([1, 2, 3])
>>> F.norm(x, ord=math.inf)
Tensor(3, dtype=int32, device=xpux:0)
>>> F.norm(x, ord=-math.inf)
Tensor(1, dtype=int32, device=xpux:0)

>>> x = Tensor([[1, 2, 3], [4, 5, 6]])
>>> F.norm(x, ord=2, axis=0)
Tensor([4.1231 5.3852 6.7082], device=xpux:0)
>>> F.norm(x, ord=2, axis=1)
Tensor([3.7417 8.775 ], device=xpux:0)