get_gradients Module Function

pure module function get_gradients(this) result(gradients)

Get gradients

Arguments

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

Instance of network

Return Value real(kind=real32), dimension(this%num_params)

Gradients


Source Code

  pure module function get_gradients(this) result(gradients)
    !! Get gradients
    implicit none

    ! Arguments
    class(network_type), intent(in) :: this
    !! Instance of network
    real(real32), dimension(this%num_params) :: gradients
    !! Gradients

    ! Local variables
    integer :: l, i, start_idx, end_idx
    !! Loop index

    start_idx = 0
    end_idx   = 0
    do l = 1, this%num_layers
       select type(current => this%model(l)%layer)
       class is(learnable_layer_type)
          do i = 1, size(current%params)
             if(associated(current%params(i)%grad))then
                start_idx = end_idx + 1
                end_idx = end_idx + size(current%params(i)%val, 1)
                gradients(start_idx:end_idx) = [ &
                     sum(current%params(i)%grad%val, dim=2) / &
                     real(size(current%params(i)%grad%val, dim=2), real32) &
                ]
             end if
          end do
       end select
    end do
    call this%optimiser%clip_dict%apply(size(gradients),gradients)

  end function get_gradients