nn_l1_loss function

L1 loss

L1 loss

Creates a criterion that measures the mean absolute error (MAE) between each element in the input xx and target yy.

nn_l1_loss(reduction = "mean")

Arguments

  • reduction: (string, optional): Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 'none': no reduction will be applied, 'mean': the sum of the output will be divided by the number of elements in the output, 'sum': the output will be summed.

Details

The unreduced (i.e. with reduction set to 'none') loss can be described as:

(x,y)=L={l1,,lN},\quadln=xnyn, \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quadl_n = \left| x_n - y_n \right|,

where NN is the batch size. If reduction is not 'none'

(default 'mean'), then:

(x,y)=\mboxmean(L),\mboxifreduction=\mboxmean;\mboxsum(L),\mboxifreduction=\mboxsum. \ell(x, y) =\begin{array}{ll}\mbox{mean}(L), & \mbox{if reduction} = \mbox{'mean';}\\\mbox{sum}(L), & \mbox{if reduction} = \mbox{'sum'.}\end{array}

xx and yy are tensors of arbitrary shapes with a total of nn elements each.

The sum operation still operates over all the elements, and divides by nn. The division by nn can be avoided if one sets reduction = 'sum'.

Shape

  • Input: (N,)(N, *) where * means, any number of additional dimensions
  • Target: (N,)(N, *), same shape as the input
  • Output: scalar. If reduction is 'none', then (N,)(N, *), same shape as the input

Examples

if (torch_is_installed()) { loss <- nn_l1_loss() input <- torch_randn(3, 5, requires_grad = TRUE) target <- torch_randn(3, 5) output <- loss(input, target) output$backward() }
  • Maintainer: Daniel Falbel
  • License: MIT + file LICENSE
  • Last published: 2025-02-14