mindspore.ops.L2Loss

View Source On Gitee
class mindspore.ops.L2Loss[source]

Calculates half of the L2 norm, but do not square the result.

Set input as x and output as loss.

\[loss = \frac{\sum x ^ 2}{2}\]
Inputs:
  • input_x (Tensor) - Tensor for computing the L2 norm. Data type must be float16, float32 or float64.

Outputs:

Tensor, has a Scalar Tensor with the same data type as input_x.

Raises
  • TypeError – If input_x is not a Tensor.

  • TypeError – If dtype of input_x is not float16, float32 or float64.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.float16)
>>> l2_loss = ops.L2Loss()
>>> output = l2_loss(input_x)
>>> print(output)
7.0