r"""Calculates the mean absolute error (MAE) between each element in the pred :math:`x` and label :math:`y`. The mean absolute error can be described as: .. math:: \ell(x,y) = mean\left(L \right) where .. math:: L = \{l_1,\dots,l_N\}, \quad l_n = \lef
(pred: Tensor, label: Tensor, reduction: str = "mean")
| 37 | |
| 38 | @_reduce_output |
| 39 | def l1_loss(pred: Tensor, label: Tensor, reduction: str = "mean") -> Tensor: |
| 40 | r"""Calculates the mean absolute error (MAE) between |
| 41 | each element in the pred :math:`x` and label :math:`y`. |
| 42 | |
| 43 | The mean absolute error can be described as: |
| 44 | |
| 45 | .. math:: |
| 46 | |
| 47 | \ell(x,y) = mean\left(L \right) |
| 48 | |
| 49 | where |
| 50 | |
| 51 | .. math:: |
| 52 | |
| 53 | L = \{l_1,\dots,l_N\}, \quad |
| 54 | l_n = \left| x_n - y_n \right|, |
| 55 | |
| 56 | :math:`x` and :math:`y` are tensors of arbitrary shapes with a total |
| 57 | of :math:`N` elements each. :math:`N` is the batch size. |
| 58 | |
| 59 | Args: |
| 60 | pred: predicted result from model. |
| 61 | label: ground truth to compare. |
| 62 | reduction: the reduction to apply to the output: 'none' | 'mean' | 'sum'. |
| 63 | |
| 64 | Returns: |
| 65 | loss value. |
| 66 | |
| 67 | Shape: |
| 68 | * ``pred``: :math:`(N, *)` where :math:`*` means any number of additional |
| 69 | dimensions. |
| 70 | * ``label``: :math:`(N, *)`. Same shape as ``pred``. |
| 71 | |
| 72 | Examples: |
| 73 | |
| 74 | >>> pred = Tensor([3, 3, 3, 3]) |
| 75 | >>> label = Tensor([2, 8, 6, 1]) |
| 76 | >>> F.nn.l1_loss(pred, label) |
| 77 | Tensor(2.75, device=xpux:0) |
| 78 | >>> F.nn.l1_loss(pred, label, reduction="none") |
| 79 | Tensor([1 5 3 2], dtype=int32, device=xpux:0) |
| 80 | >>> F.nn.l1_loss(pred, label, reduction="sum") |
| 81 | Tensor(11, dtype=int32, device=xpux:0) |
| 82 | |
| 83 | """ |
| 84 | diff = pred - label |
| 85 | return abs(diff) |
| 86 | |
| 87 | |
| 88 | @_reduce_output |