megengine.functional.nn.l1_loss

l1_loss(pred, label, reduction='mean')[源代码]

Calculates the mean absolute error (MAE) between each element in the pred \(x\) and label \(y\).

The mean absolute error can be described as:

\[\ell(x,y) = mean\left(L \right)\]

where

\[L = \{l_1,\dots,l_N\}, \quad l_n = \left| x_n - y_n \right|,\]

\(x\) and \(y\) are tensors of arbitrary shapes with a total of \(N\) elements each. \(N\) is the batch size.

参数
  • pred (Tensor) – predicted result from model.

  • label (Tensor) – ground truth to compare.

  • reduction (str) – the reduction to apply to the output: ‘none’ | ‘mean’ | ‘sum’. Default: ‘mean’

返回类型

Tensor

返回

loss value.

实际案例

import numpy as np
import megengine as mge
import megengine.functional as F

ipt = mge.tensor(np.array([3, 3, 3, 3]).astype(np.float32))
tgt = mge.tensor(np.array([2, 8, 6, 1]).astype(np.float32))
loss = F.nn.l1_loss(ipt, tgt)
print(loss.numpy())

Outputs:

2.75