mindspore.ops.LpNorm

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

Return the p-norm of a matrix or vector.

\[output = \|input\|_{p}=\left(\sum_{i=1}^{n}\left|input\right|^{p}\right)^{1 / p}\]
Parameters
  • axis (int,list,tuple) – Specifies which dimension or dimensions of input to calculate the norm across.

  • p (int, optional) – The order of norm. Default: 2 .

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

  • epsilon (float, optional) – The lower bound value, when the calculated norm is less than this value, replace this result with epsilon. Default: 1e-12 .

Inputs:
  • input (Tensor) - Input tensor of type float16, float32.

Outputs:

Tensor, has the same dtype as input, its shape depends on axis. For example, if the shape of input is \((2, 3, 4)\), axis is \([0, 1]\), output 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.

  • ValueError – If the element of axis is out of the range \([-r, r)\), where \(r\) is the rank of input.

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

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> input_x = Tensor(np.array([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]).astype(np.float32))
>>> op = ops.LpNorm(axis=[0, 1], p=2, keep_dims=False)
>>> output = op(input_x)
>>> print(output)
[ 9.165152 10.954452]