torch_loss_mse Subroutine

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

Uses

  • proc~~torch_loss_mse~~UsesGraph proc~torch_loss_mse torch_loss_mse iso_c_binding iso_c_binding proc~torch_loss_mse->iso_c_binding

Evaluate MSELoss

Note that the reduction type relates to the operation to perform within a (mini)batch. With torch_kNone, no reduction is applied and the loss tensor will have the same dimensions as the input tensor. If torch_kMean (default) or torch_kSum is applied then the loss tensor will differ in the first dimension (for the batch), which will be collapsed.

We refer to the PyTorch docs for the specifics of how this works for MSELoss https://docs.pytorch.org/docs/main/nn.functional.html#torch.nn.functional.mse_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, intent(in), optional :: reduction_type

Reduction type to use over batches (default: torch_kMean)


Source Code

  subroutine torch_loss_mse(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  !! Reduction type to use over batches (default: torch_kMean)

    integer(c_int) :: reduction_type_value

    interface
      subroutine torch_loss_mse_c(loss_tensor_c, input_tensor_c, target_tensor_c, &
          reduction_type_c) bind(c, name = 'torch_loss_mse')
        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_mse_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_mse_c(loss_tensor%p, input_tensor%p, target_tensor%p, reduction_type_value)
  end subroutine torch_loss_mse