compute_nll Function

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

Compute the negative log likelihood of a model

Type Bound

nll_loss_type

Arguments

Type IntentOptional Attributes Name
class(nll_loss_type), intent(in), target :: this

Instance of the physics-informed neural network loss function

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

Negative log likelihood loss


Source Code

  function compute_nll(this, predicted, expected) result(output)
    !! Compute the negative log likelihood of a model
    implicit none

    ! Arguments
    class(nll_loss_type), intent(in), target :: this
    !! Instance of the physics-informed neural network loss function
    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
    !! Negative log likelihood loss

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

    output => mean(-log(expected(1,1) - predicted(1,1) + this%epsilon) )
    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(-log(expected(i,s) - predicted(i,s) + this%epsilon) )

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

  end function compute_nll