mindspore.ops.norm

mindspore.ops.norm(input_x, axis, p=2, keep_dims=False, epsilon=1e-12)[source]

Returns the matrix norm or vector norm of a given tensor.

\[output = sum(abs(input)**p)**(1/p)\]
Parameters
  • input_x (Tensor) – Input tensor. The dtype must be float32 or float16.

  • axis (Union[int, list, tuple]) – Specifies which dimension or dimensions of input to calculate the norm across.

  • p (int) – The order of norm. Default: 2. p is greater than or equal to 0.

  • keep_dims (bool) – Whether the output tensors have dim retained or not. Default: False.

  • epsilon (float) – A value added to the denominator for numerical stability. Default: 1e-12.

Returns

Tensor, has the same dtype as input, which shape depends on the args axis. For example, if the size of input is (2, 3, 4), axis is [0, 1], Outputs’ shape will be (4,).

Raises
  • TypeError – If input is not a Tensor.

  • TypeError – If dtype of input is not one of: float16, float32.

  • TypeError – If p is not an int.

  • TypeError – If axis is not an int, a tuple or a list.

  • TypeError – If axis is a tuple or a list, but the element of axis is not an int.

  • TypeError – If keep_dims is not a bool.

  • TypeError – If epsilon is not a float.

  • ValueError – If the element of axis is out of the range (-len(input.shape), len(input.shape)).

  • ValueError – If the length of shape of axis is bigger than the length of shape of input.

Supported Platforms:

Ascend GPU CPU

Examples

>>> input_x = Tensor(np.array([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]).astype(np.float32))
>>> output = ops.norm(input_x, [0, 1], p=2)
>>> print(output)
[ 9.165152 10.954452]