backprop Subroutine

private pure subroutine backprop(self, y, dw, db)

Applies a backward propagation through the network and returns the weight and bias gradients.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
real(kind=rk), intent(in) :: y(:)
type(array2d), intent(out), allocatable:: dw(:)
type(array1d), intent(out), allocatable:: db(:)

Calls

proc~~backprop~~CallsGraph proc~backprop backprop proc~db_init db_init proc~backprop->proc~db_init proc~dw_init dw_init proc~backprop->proc~dw_init dims dims proc~backprop->dims layers layers proc~backprop->layers interface~array1d array1d proc~db_init->interface~array1d interface~array2d array2d proc~dw_init->interface~array2d proc~array2d_constructor array2d_constructor interface~array2d->proc~array2d_constructor proc~array1d_constructor array1d_constructor interface~array1d->proc~array1d_constructor

Contents

Source Code


Source Code

  pure subroutine backprop(self, y, dw, db)
    !! Applies a backward propagation through the network
    !! and returns the weight and bias gradients.
    class(network_type), intent(in out) :: self
    real(rk), intent(in) :: y(:)
    type(array2d), allocatable, intent(out) :: dw(:)
    type(array1d), allocatable, intent(out) :: db(:)
    integer(ik) :: n, nm

    associate(dims => self % dims, layers => self % layers)

      call db_init(db, dims)
      call dw_init(dw, dims)

      n = size(dims)
      db(n) % array = (layers(n) % a - y) * self % layers(n) % activation_prime(layers(n) % z)
      dw(n-1) % array = matmul(reshape(layers(n-1) % a, [dims(n-1), 1]),&
                               reshape(db(n) % array, [1, dims(n)]))

      do n = size(dims) - 1, 2, -1
        db(n) % array = matmul(layers(n) % w, db(n+1) % array)&
                      * self % layers(n) % activation_prime(layers(n) % z)
        dw(n-1) % array = matmul(reshape(layers(n-1) % a, [dims(n-1), 1]),&
                                 reshape(db(n) % array, [1, dims(n)]))
      end do

    end associate

  end subroutine backprop