huber_array Module Function

module function huber_array(delta, gamma) result(output)

Huber loss function

Arguments

Type IntentOptional Attributes Name
class(array_type), intent(in), target :: delta
real(kind=real32), intent(in) :: gamma

Return Value type(array_type), pointer


Source Code

  module function huber_array(delta, gamma) result( output )
    !! Huber loss function
    implicit none
    class(array_type), intent(in), target :: delta
    real(real32), intent(in) :: gamma
    type(array_type), pointer :: output

    type(array_type), pointer :: b_array

    output => delta%create_result()
    where (abs(delta%val) .le. gamma)
       output%val = 0.5_real32 * (delta%val)**2._real32
    elsewhere
       output%val = gamma * (abs(delta%val) - 0.5_real32 * gamma)
    end where

    output%get_partial_left => get_partial_huber
    output%get_partial_left_val => get_partial_huber_val
    if(delta%requires_grad)then
       output%requires_grad = .true.
       output%is_forward = delta%is_forward
       output%operation = 'huber'
       output%left_operand => delta
       output%owns_left_operand = delta%is_temporary
    end if
    allocate(b_array)
    b_array%is_sample_dependent = .false.
    b_array%is_scalar = .true.
    b_array%requires_grad = .false.
    call b_array%allocate(array_shape=[1, 1])
    b_array%val(1,1) = gamma
    output%right_operand => b_array
    output%owns_right_operand = .true.

  end function huber_array