predict_array_from_real Module Function

module function predict_array_from_real(this, input, output_as_array, verbose) result(output)

Predict the output for a generic input

Arguments

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

Instance of network

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

Input graph

logical, intent(in) :: output_as_array

Whether to output as array

integer, intent(in), optional :: verbose

Verbosity level

Return Value type(array_type), dimension(:,:), allocatable

Predicted output


Source Code

  module function predict_array_from_real( this, input, output_as_array, verbose ) &
       result(output)
    !! Predict the output for a generic input
    implicit none

    ! Arguments
    class(network_type), intent(inout) :: this
    !! Instance of network
    class(*), dimension(..), intent(in) :: input
    !! Input graph
    logical, intent(in) :: output_as_array
    !! Whether to output as array
    integer, intent(in), optional :: verbose
    !! Verbosity level

    type(array_type), dimension(:,:), allocatable :: output
    !! Predicted output

    ! Local variables
    integer :: s, i
    !! Loop index
    integer :: num_samples
    !! Number of samples
    integer :: verbose_
    !! Verbosity level
    logical, allocatable :: mode_store(:)
    !! Storage for inference mode booleans


    !---------------------------------------------------------------------------
    ! Initialise optional arguments
    !---------------------------------------------------------------------------
    if(present(verbose))then
       verbose_ = verbose
    else
       verbose_ = 0
    end if
    if(.not.output_as_array)then
       call stop_program("predict_array_from_real: output_as_array must be true")
       return
    end if


    !---------------------------------------------------------------------------
    ! Set number of samples for predicting
    !---------------------------------------------------------------------------
    num_samples = this%save_input( input )
    ! call this%set_batch_size(num_samples)


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

    !---------------------------------------------------------------------------
    ! Forward pass
    !---------------------------------------------------------------------------
    select case(this%use_graph_input)
    case(.true.)
       call this%forward(this%input_graph)
    case default
       call this%forward(this%input_array)
    end select


    !---------------------------------------------------------------------------
    ! Allocate output data
    !---------------------------------------------------------------------------
    allocate(output( &
         size(this%model(this%leaf_vertices(1))%layer%output, 1), &
         size(this%model(this%leaf_vertices(1))%layer%output, 2) &
    ))
    do s = 1, size(this%model(this%leaf_vertices(1))%layer%output, 2)
       do i = 1, size(this%model(this%leaf_vertices(1))%layer%output, 1)
          output(i,s) = this%model(this%leaf_vertices(1))%layer%output(i,s)
       end do
    end do


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

  end function predict_array_from_real