torch_loss_l1 Subroutine

public subroutine torch_loss_l1(loss_tensor, input_tensor, target_tensor, reduction_type)

Uses

  • proc~~torch_loss_l1~~UsesGraph proc~torch_loss_l1 torch_loss_l1 iso_c_binding iso_c_binding proc~torch_loss_l1->iso_c_binding

Evaluate L1Loss (mean absolute error)

The reduction type controls how element-wise absolute errors are combined. With torch_kNone, no reduction is applied. torch_kMean (default) returns their mean, and torch_kSum returns their sum.

See the PyTorch documentation for the specifics of L1Loss: https://docs.pytorch.org/docs/main/nn.functional.html#torch.nn.functional.l1_loss

Arguments

Type IntentOptional Attributes Name
type(torch_tensor), intent(inout) :: loss_tensor

Tensor to hold the loss value

type(torch_tensor), intent(in) :: input_tensor

Input tensor to evaluate loss at

type(torch_tensor), intent(in) :: target_tensor

Target tensor to evaluate loss against

integer(kind=c_int), intent(in), optional :: reduction_type

Reduction type to use (default: torch_kMean)


Source Code

  subroutine torch_loss_l1(loss_tensor, input_tensor, target_tensor, reduction_type)
    use, intrinsic :: iso_c_binding, only : c_associated, c_int
    type(torch_tensor), intent(inout) :: loss_tensor  !! Tensor to hold the loss value
    type(torch_tensor), intent(in) :: input_tensor  !! Input tensor to evaluate loss at
    type(torch_tensor), intent(in) :: target_tensor  !! Target tensor to evaluate loss against
    integer(c_int), optional, intent(in) :: reduction_type
        !! Reduction type to use (default: torch_kMean)

    integer(c_int) :: reduction_type_value

    interface
      subroutine torch_loss_l1_c(loss_tensor_c, input_tensor_c, target_tensor_c, &
          reduction_type_c) bind(c, name = "torch_loss_l1")
        use, intrinsic :: iso_c_binding, only : c_ptr, c_int
        implicit none
        type(c_ptr), value, intent(in) :: loss_tensor_c
        type(c_ptr), value, intent(in) :: input_tensor_c
        type(c_ptr), value, intent(in) :: target_tensor_c
        integer(c_int), value, intent(in) :: reduction_type_c
      end subroutine torch_loss_l1_c
    end interface

    if (.not. present(reduction_type)) then
      reduction_type_value = torch_kMean
    else
      reduction_type_value = reduction_type
    end if

    if (.not. c_associated(loss_tensor%p)) then
      write(*,*) "Error :: loss tensor has not been constructed"
      stop 1
    end if
    call torch_loss_l1_c(loss_tensor%p, input_tensor%p, target_tensor%p, reduction_type_value)
  end subroutine torch_loss_l1