Function to apply clipping to gradients
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(clip_type), | intent(in) | :: | this |
Instance of the clip type |
||
| integer, | intent(in) | :: | length |
Length of the gradient |
||
| real(kind=real32), | intent(inout), | dimension(length) | :: | gradient |
Gradient to be clipped |
|
| real(kind=real32), | intent(inout), | optional, | dimension(:) | :: | bias |
Bias to be clipped |
pure subroutine apply_clip(this, length, gradient, bias) !! Function to apply clipping to gradients implicit none ! Arguments class(clip_type), intent(in) :: this !! Instance of the clip type integer, intent(in) :: length !! Length of the gradient real(real32), dimension(length), intent(inout) :: gradient !! Gradient to be clipped real(real32), dimension(:), optional, intent(inout) :: bias !! Bias to be clipped ! Local variables real(real32) :: scale !! Scaling factor for the gradient real(real32), dimension(:), allocatable :: bias_ !! Copy of the bias if(present(bias))then bias_ = bias else allocate(bias_(1), source=0._real32) end if ! Clip values to within limits of (min,max) if(this%l_min_max)then gradient = max(this%min,min(this%max,gradient)) bias_ = max(this%min,min(this%max,bias_)) end if ! Clip values to a maximum L2-norm if(this%l_norm)then scale = min(1._real32, & this%norm/sqrt(sum(gradient**2._real32) + & sum(bias_)**2._real32)) if(scale.lt.1._real32)then gradient = gradient * scale bias_ = bias_ * scale end if end if if(present(bias)) bias = bias_ end subroutine apply_clip