predict_array Module Function

module function predict_array(this, input, 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(array_type), intent(in), dimension(..) :: input

Input graph

integer, intent(in), optional :: verbose

Verbosity level

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

Predicted output


Source Code

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

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

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

    ! Local variables
    integer :: l, s, i, j, layer_id
    !! Loop index
    integer :: num_samples
    !! Number of samples
    integer :: verbose_
    !! Verbosity level
    integer, dimension(2) :: output_shape
    !! Output shape
    logical, allocatable :: mode_store(:)
    !! Storage for inference mode booleans


    !---------------------------------------------------------------------------
    ! Initialise optional arguments
    !---------------------------------------------------------------------------
    if(present(verbose))then
       verbose_ = verbose
    else
       verbose_ = 0
    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
    !---------------------------------------------------------------------------
    output_shape = this%get_output_shape()
    allocate(output(output_shape(1), output_shape(2)))
    do l = 1, size(this%leaf_vertices)
       layer_id = this%auto_graph%vertex(this%leaf_vertices(l))%id
       j = 0
       do i = 1, size(this%model(layer_id)%layer%output, 1)
          j = j + 1
          do s = 1, size(this%model(layer_id)%layer%output, 2)
             output(j,s) = this%model(layer_id)%layer%output(i,s)
          end do
       end do
    end do


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

  end function predict_array