compute_mae Function

private function compute_mae(this, predicted, expected) result(output)

Compute the mean absolute error of a model

Type Bound

mae_loss_type

Arguments

Type IntentOptional Attributes Name
class(mae_loss_type), intent(in), target :: this
type(array_type), intent(inout), dimension(:,:), target :: predicted

Predicted values

type(array_type), intent(in), dimension(size(predicted,1),size(predicted,2)) :: expected

Expected values

Return Value type(array_type), pointer

Mean absolute error


Source Code

  function compute_mae(this, predicted, expected) result(output)
    !! Compute the mean absolute error of a model
    implicit none

    ! Arguments
    class(mae_loss_type), intent(in), target :: this
    type(array_type), dimension(:,:), intent(inout), target :: predicted
    !! Predicted values
    type(array_type), dimension(size(predicted,1),size(predicted,2)), intent(in) :: &
         expected
    !! Expected values
    type(array_type), pointer :: output
    !! Mean absolute error

    ! Local variables
    integer :: s, i
    !! Loop indices
    type(array_type), pointer :: ptr
    !! Temporary pointer for calculations

    output => mean( abs( predicted(1,1) - expected(1,1) ) ) / &
         2._real32
    if(any(shape(predicted).gt.1))then
       do s = 1, size(predicted,2)
          do i = 1, size(predicted,1)
             if(i.eq.1 .and. s.eq.1) cycle
             if(.not.predicted(i,s)%allocated .or. &
                  .not.expected(i,s)%allocated) cycle
             ptr => mean( abs( predicted(i,s) - expected(i,s) ) ) / &
                  2._real32

             output => output + ptr
          end do
       end do
    end if

  end function compute_mae