mindspore.ops.l1_loss
- mindspore.ops.l1_loss(input, target, reduction='mean')[源代码]
用于计算预测值和目标值之间的平均绝对误差。
假设
和 为一维Tensor,长度 ,reduction 设置为”none”,则计算 和 的loss不进行降维操作。公式如下:
其中,
为batch size。如果 reduction 是mean或者sum,则:
- 参数:
input (Tensor) - 预测值,任意维度的Tensor。
target (Tensor) - 目标值,通常情况与 input 的shape相同。如果 input 和 target 的shape不同,需要保证他们之间可以互相广播。
reduction (str, optional) - 应用于loss的reduction类型。取值为
"mean"
,"sum"
或"none"
。默认值:'mean'
。
- 返回:
Tensor或Scalar,如果 reduction 为 none ,则返回与 input 具有相同shape和dtype的Tensor。否则,将返回Scalar。
- 异常:
TypeError - input 不是Tensor。
TypeError - target 不是Tensor。
ValueError - reduction 不为”mean”、”sum”或”none”。
- 支持平台:
Ascend
GPU
CPU
样例:
>>> x = ms.Tensor([[1, 2, 3], [4, 5, 6]], mstype.float32) >>> target = ms.Tensor([[6, 5, 4], [3, 2, 1]], mstype.float32) >>> output = ops.l1_loss(x, target, reduction="mean") >>> print(output) 3.0