init_recurrent Subroutine

private subroutine init_recurrent(this, input_shape, verbose)

Initialise the recurrent layer

Type Bound

recurrent_layer_type

Arguments

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

Instance of the recurrent layer

integer, intent(in), dimension(:) :: input_shape

Shape of the input

integer, intent(in), optional :: verbose

Verbosity level


Source Code

  subroutine init_recurrent(this, input_shape, verbose)
    !! Initialise the recurrent layer
    implicit none

    ! Arguments
    class(recurrent_layer_type), intent(inout) :: this
    !! Instance of the recurrent layer
    integer, dimension(:), intent(in) :: input_shape
    !! Shape of the input
    integer, optional, intent(in) :: verbose
    !! Verbosity level

    ! Local variables
    integer :: num_inputs
    !! Temporary variable
    integer :: verbose_ = 0


    !---------------------------------------------------------------------------
    ! Initialise optional arguments
    !---------------------------------------------------------------------------
    if(present(verbose)) verbose_ = verbose


    !---------------------------------------------------------------------------
    ! Initialise number of inputs
    !---------------------------------------------------------------------------
    if(.not.allocated(this%input_shape)) call this%set_shape(input_shape)
    this%input_size = this%input_shape(1)
    this%output_shape = [this%hidden_size]
    this%num_params = this%get_num_params()


    !---------------------------------------------------------------------------
    ! Allocate weight, weight steps (velocities), output, and activation
    !---------------------------------------------------------------------------
    allocate(this%weight_shape(2,2))
    this%weight_shape(:,1) = [ this%hidden_size, this%input_size ]
    this%weight_shape(:,2) = [ this%hidden_size, this%hidden_size ]

    if(this%use_bias)then
       this%bias_shape = [ this%hidden_size, this%hidden_size ]
       allocate(this%params(4))
    else
       allocate(this%params(2))
    end if
    call this%params(1)%allocate([this%weight_shape(:,1), 1])
    call this%params(1)%set_requires_grad(.true.)
    this%params(1)%fix_pointer = .true.
    this%params(1)%is_sample_dependent = .false.
    this%params(1)%is_temporary = .false.
    call this%params(2)%allocate([this%weight_shape(:,2), 1])
    call this%params(2)%set_requires_grad(.true.)
    this%params(2)%fix_pointer = .true.
    this%params(2)%is_sample_dependent = .false.
    this%params(2)%is_temporary = .false.

    num_inputs = this%input_size + this%hidden_size
    if(this%use_bias)then
       num_inputs = num_inputs + 2 * this%hidden_size
       call this%params(3)%allocate([this%bias_shape(1), 1])
       call this%params(3)%set_requires_grad(.true.)
       this%params(3)%fix_pointer = .true.
       this%params(3)%is_sample_dependent = .false.
       this%params(3)%is_temporary = .false.
       call this%params(4)%allocate([this%bias_shape(2), 1])
       call this%params(4)%set_requires_grad(.true.)
       this%params(4)%fix_pointer = .true.
       this%params(4)%is_sample_dependent = .false.
       this%params(4)%is_temporary = .false.
    end if


    !---------------------------------------------------------------------------
    ! Initialise weights (kernels)
    !---------------------------------------------------------------------------
    call this%kernel_init%initialise( &
         this%params(1)%val(:,1), &
         fan_in = num_inputs, fan_out = this%hidden_size, &
         spacing = [ this%hidden_size ] &
    )
    call this%kernel_init%initialise( &
         this%params(2)%val(:,1), &
         fan_in = num_inputs, fan_out = this%hidden_size, &
         spacing = [ this%hidden_size ] &
    )

    ! Initialise biases
    !---------------------------------------------------------------------------
    if(this%use_bias)then
       call this%bias_init%initialise( &
            this%params(3)%val(:,1), &
            fan_in = num_inputs, fan_out = this%hidden_size &
       )
       call this%bias_init%initialise( &
            this%params(4)%val(:,1), &
            fan_in = num_inputs, fan_out = this%hidden_size &
       )
    end if


    !---------------------------------------------------------------------------
    ! Allocate arrays and initialise time_step
    !---------------------------------------------------------------------------
    if(allocated(this%output)) deallocate(this%output)
    allocate(this%output(1,1))
    this%time_step = 0

  end subroutine init_recurrent