mindspore.nn.RMSELoss

查看源文件
class mindspore.nn.RMSELoss[源代码]

RMSELoss用来测量 xy 元素之间的均方根误差,其中 x 是输入Tensor, y 是目标值。

假设 xy 为一维Tensor,长度为 Nxy 的loss为:

loss=1Ni=1N(xiyi)2
输入:
  • logits (Tensor) - 输入的预测值Tensor, shape (N,) ,其中 代表任意数量的附加维度。

  • labels (Tensor) - 输入的目标值Tensor,shape (N,) 。一般与 logits 的shape相同。如果 logitslabels 的shape不同,需支持广播。

输出:

Tensor,输出值为加权损失值,其数据类型为float,其shape为 ()

支持平台:

Ascend GPU CPU

样例:

>>> import mindspore
>>> from mindspore import Tensor, nn
>>> import numpy as np
>>> # Case 1: logits.shape = labels.shape = (3,)
>>> loss = nn.RMSELoss()
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
0.57735026
>>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
>>> loss = nn.RMSELoss()
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
1.0