torch_loss_cross_entropy Subroutine

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

Uses

  • proc~~torch_loss_cross_entropy~~UsesGraph proc~torch_loss_cross_entropy torch_loss_cross_entropy iso_c_binding iso_c_binding proc~torch_loss_cross_entropy->iso_c_binding

Evaluate CrossEntropyLoss

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, intent(in), optional :: reduction_type

Optional reduction type (default: torch_kMean)


Source Code

  subroutine torch_loss_cross_entropy(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, optional, intent(in) :: reduction_type  !! Optional reduction type (default: torch_kMean)

    integer(c_int) :: reduction_type_value

    interface
      subroutine torch_loss_cross_entropy_c(loss_tensor_c, input_tensor_c, target_tensor_c, &
          reduction_type_c) bind(c, name = 'torch_loss_cross_entropy')
        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_cross_entropy_c
    end interface

    ! Process optional arguments
    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_cross_entropy_c(loss_tensor%p, input_tensor%p, target_tensor%p, &
                                    reduction_type_value)
  end subroutine torch_loss_cross_entropy