scientific-computing/FKB

Redundant evaluation of db in dense_backward() in mod_dense_layer.F90 ?

septcolor opened this issue · 1 comments

In the dense_backward() routine in src/lib/mod_dense_layer.F90 below,

  subroutine dense_backward(self, g, lr)

    class(Dense), intent(in out) :: self
    real(rk), intent(in) :: g(:), lr
    real(rk), allocatable :: t(:), dw(:,:), db(:)

    db = self % activation_prime(self % z, self % alpha) * g   !<--- [1]

    dw = matmul(&
      reshape(self % i, (/size(self % i), 1/)), &
      reshape(db, (/1, size(db)/))&
    )

    db = self % activation_prime(self % z, self % alpha) * g   !<--- [2]

    self % gradient = matmul(self % w, db)

    ! weight updates
    self % w = self % w - lr * dw
    self % b = self % b - lr * db

  end subroutine dense_backward

the second evaluation of db maked by [2] seems redundant with that
marked by [1] because self % z, self % alpha, and g have not been modified between [1] and [2]. So I guess it may be OK to eliminate [2] (which leads to less computation also).

You're correct, this has been removed. Thanks!