mindspore.Tensor.norm
- Tensor.norm(axis, p=2, keep_dims=False, epsilon=1e-12)[source]
Returns the matrix norm or vector norm of a given tensor.
- Parameters
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 self tensor, 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 dtype of self tensor 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_x.shape), len(input_x.shape)). input_x refers to self tensor.
ValueError – If the length of shape of axis is bigger than the length of shape of self tensor.
- 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 = input_x.norm([0, 1], p=2) >>> print(output) [ 9.165152 10.954452]