print Module Subroutine

module subroutine print(this, file)

Uses

Print the network to a file

Arguments

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

Instance of network

character(len=*), intent(in) :: file

File to print the network to


Source Code

  module subroutine print(this, file)
    !! Print the network to a file
    use coreutils, only: to_upper
    use athena__io_utils, only: athena__version__
    implicit none

    ! Arguments
    class(network_type), intent(in) :: this
    !! Instance of network
    character(*), intent(in) :: file
    !! File to print the network to

    ! Local variables
    integer :: l, v, e, vertex_index, unit
    !! Loop index
    integer :: operator_in, operator_out
    !! Operators for the layer
    character(3) :: operator_str
    !! String to store the operator
    character(256) :: suffix, fmt
    !! Suffix for the layer
    integer, dimension(:), allocatable :: input_list, output_list

    open(newunit=unit,file=file,status='replace')

    write(unit,'("NETWORK_SETTINGS")')
    write(unit,'(3X,"ATHENA_VERSION = ",A)') trim(adjustl(athena__version__))
    if(allocated(this%name)) write(unit,'(3X,"NAME = ",A)') trim(adjustl(this%name))
    write(unit,'(3X,"EPOCH = ",I0)') this%epoch
    write(unit,'(3X,"BATCH_SIZE = ",I0)') this%batch_size
    write(unit,'(3X,"ACCURACY = ",F0.9)') this%accuracy_val
    write(unit,'(3X,"LOSS = ",F0.9)') this%loss_val
    if(allocated(this%accuracy_method))then
       write(unit,'(3X,"ACCURACY_METHOD = ",A)') trim(adjustl(this%accuracy_method))
    end if
    if(allocated(this%loss_method))then
       write(unit,'(3X,"LOSS_METHOD = ",A)') trim(adjustl(this%loss_method))
    end if
    if(allocated(this%optimiser))then
       write(unit,'(3X,"OPTIMISER: ",A)') trim(adjustl(this%optimiser%name))
       call this%optimiser%print_to_unit(unit=unit)
       write(unit,'(3X,"END OPTIMISER")')
    end if
    write(unit,'("END NETWORK_SETTINGS")')

    do v = 1, size(this%vertex_order,dim=1), 1
       l = this%vertex_order(v)
       operator_in = -1
       operator_out = -1
       allocate(input_list(0), output_list(0))
       do e = 1, this%auto_graph%num_edges
          if(-this%auto_graph%edge(e)%index(2).eq.l)then
             if(operator_in.gt.0.and.this%auto_graph%edge(e)%id.ne.operator_in)then
                write(0,*) "WARNING: multiple operators for layer ", l
                write(0,*) "  using operator ", this%auto_graph%edge(e)%id
             end if
             operator_in = this%auto_graph%edge(e)%id
             vertex_index = &
                  findloc( this%vertex_order, this%auto_graph%edge(e)%index(1), 1 )
             input_list = [ input_list, vertex_index ]
          end if
          if(this%auto_graph%edge(e)%index(1).eq.l)then
             if(operator_out.gt.0.and.this%auto_graph%edge(e)%id.ne.operator_out)then
                write(0,*) "WARNING: multiple operators for layer ", l
                write(0,*) "  using operator ", this%auto_graph%edge(e)%id
             end if
             operator_in = this%auto_graph%edge(e)%id
             vertex_index = &
                  findloc( this%vertex_order, this%auto_graph%edge(e)%index(2), 1 )
             output_list = [ output_list, vertex_index ]
          end if
       end do

       suffix = ""
       select case(operator_in)
       case(1)
          operator_str = " ||"
       case(2)
          operator_str = " +"
       case(3)
          operator_str = " *"
       end select
       ! get size of input_list and make the formatted string
       if(size(input_list).eq.0)then
          write(suffix,'(A," []")') trim(operator_str)
       else
          write(fmt,'("(A,A,"" ["",",I0,"(1X,I0),"" ]"")")') size(input_list)
          write(suffix,fmt) trim(suffix), operator_str, input_list
       end if
       ! select case(operator_out)
       ! case(1)
       !    operator_str = " ||"
       ! case(2)
       !    operator_str = " +"
       ! case(3)
       !    operator_str = " *"
       ! end select
       ! if(size(output_list).gt.0)then
       !    write(fmt,'("(A,A,"" ["",",I0,"(1X,I0),"" ]"")")') size(output_list)
       !    write(suffix,fmt) trim(suffix), operator_str, output_list
       ! end if

       write(unit,'(A,A)') to_upper(trim(this%model(l)%layer%name)), trim(suffix)
       call this%model(l)%layer%print(unit=unit, print_header_footer=.false.)

       write(unit,'("END ",A)') to_upper(trim(this%model(l)%layer%name))
       deallocate(input_list, output_list)
    end do
    close(unit)

  end subroutine print