get_gradients Module Function

pure module function get_gradients(this, clip_method) result(gradients)

Get the gradients of the layer

This function returns the gradients of the layer as a single array. This has been modified from the neural-fortran library

Arguments

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

Instance of the layer

type(clip_type), intent(in), optional :: clip_method

Method to clip the gradients

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

Gradients of the layer


Source Code

  pure module function get_gradients(this, clip_method) result(gradients)
    !! Get the gradients of the layer
    !!
    !! This function returns the gradients of the layer as a single array.
    !! This has been modified from the neural-fortran library
    use athena__clipper, only: clip_type
    implicit none

    ! Arguments
    class(learnable_layer_type), intent(in) :: this
    !! Instance of the layer
    type(clip_type), optional, intent(in) :: clip_method
    !! Method to clip the gradients
    real(real32), dimension(this%num_params) :: gradients
    !! Gradients of the layer

    ! Local variables
    integer :: i, start_idx, end_idx
    !! Loop indices

    if(.not.allocated(this%params))then
       return
    end if
    start_idx = 0
    end_idx = 0
    do i = 1, size(this%params)
       start_idx = end_idx + 1
       end_idx = start_idx + size(this%params(i)%val,1) - 1
       if(.not.associated(this%params(i)%grad))then
          gradients(start_idx:end_idx) = 0._real32
       else
          gradients(start_idx:end_idx) = this%params(i)%grad%val(:,1)
       end if
    end do

    if(present(clip_method)) call clip_method%apply(size(gradients),gradients)

  end function get_gradients