test Module Subroutine

module subroutine test(this, input, output, verbose)

Test the network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

class(*), intent(in), dimension(..) :: input

Input data

class(*), intent(in), dimension(:,:) :: output

Output data

integer, intent(in), optional :: verbose

Verbosity level


Source Code

  module subroutine test( &
       this, input, output, verbose &
  )
    !! Test the network
    implicit none

    ! Arguments
    class(network_type), intent(inout) :: this
    !! Instance of network
    class(*), dimension(..), intent(in) :: input
    !! Input data
    class(*), dimension(:,:), intent(in) :: output
    !! Output data
    integer, optional, intent(in) :: verbose
    !! Verbosity level

    ! Local variables
    integer :: l, sample, num_samples
    !! Loop index
    integer :: verbose_
    !! Verbosity level
    logical :: use_accuracy
    !! Whether accuracy evaluation is available
    real(real32) :: acc_val, loss_val
    !! Loss and accuracy
    class(*), allocatable, dimension(:,:) :: data_poly
    !! Polymorphic data array
    type(array_type), pointer :: loss => null()
    !! Loss
    logical, allocatable :: mode_store(:)
    !! Storage for inference mode booleans


    !---------------------------------------------------------------------------
    ! Initialise optional arguments
    !---------------------------------------------------------------------------
    if(present(verbose))then
       verbose_ = verbose
    else
       verbose_ = 0
    end if
    use_accuracy = associated(this%get_accuracy)

    do l = 1, size(this%metrics,dim=1)
       this%metrics(l)%val = 0._real32
    end do
    loss_val  = 0._real32
    acc_val = 0._real32


    num_samples = this%save_input( input )


    !---------------------------------------------------------------------------
    ! Reset batch size for testing
    !---------------------------------------------------------------------------
    call this%set_batch_size(1)


    !---------------------------------------------------------------------------
    ! Enable inference mode
    !---------------------------------------------------------------------------
    call this%set_inference_mode(mode_store)


    !---------------------------------------------------------------------------
    ! Testing loop
    !---------------------------------------------------------------------------
    test_loop1: do sample = 1, num_samples

       ! Forward pass
       !------------------------------------------------------------------------
       select case(this%use_graph_input)
       case(.true.)
          data_poly = get_sample( &
               this%input_graph, sample, sample, 1 &
          )
       case default
          data_poly = get_sample_array( &
               this%input_array, sample, sample, 1, &
               as_graph = .false. &
          )
       end select
       call this%forward(data_poly)
       deallocate(data_poly)


       ! Compute loss and accuracy (for monitoring)
       !------------------------------------------------------------------------
       loss => this%loss_eval(sample, sample)
       loss_val = sum(loss%val)
       call loss%nullify_graph()
       deallocate(loss)
       nullify(loss)
       if(use_accuracy)then
          acc_val = this%accuracy_eval(output, sample, sample)
          this%metrics(2)%val = this%metrics(2)%val + acc_val
       end if
       this%metrics(1)%val = this%metrics(1)%val + loss_val

    end do test_loop1


    ! Normalise metrics by number of samples
    !---------------------------------------------------------------------------
    if(use_accuracy)then
       this%accuracy_val = this%metrics(2)%val / real(num_samples, real32)
    else
       this%accuracy_val = 0._real32
    end if
    this%loss_val     = this%metrics(1)%val / real(num_samples, real32)


    !---------------------------------------------------------------------------
    ! Restore training/inference mode
    !---------------------------------------------------------------------------
    call this%restore_mode(mode_store)

  end subroutine test