Skip to content

Apply optimizer to model weights without data copy #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/nf/nf_dense_layer.f90
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ module nf_dense_layer
procedure :: backward
procedure :: forward
procedure :: get_gradients
procedure :: get_gradients_ptr
procedure :: get_num_params
procedure :: get_params
procedure :: get_params_ptr
procedure :: init
procedure :: set_params

Expand Down Expand Up @@ -96,6 +98,12 @@ module function get_params(self) result(params)
!! Parameters of this layer
end function get_params

module subroutine get_params_ptr(self, w_ptr, b_ptr)
class(dense_layer), intent(in), target :: self
real, pointer, intent(out) :: w_ptr(:)
real, pointer, intent(out) :: b_ptr(:)
end subroutine get_params_ptr

module function get_gradients(self) result(gradients)
!! Return the gradients of this layer.
!! The gradients are ordered as weights first, biases second.
Expand All @@ -105,6 +113,12 @@ module function get_gradients(self) result(gradients)
!! Gradients of this layer
end function get_gradients

module subroutine get_gradients_ptr(self, dw_ptr, db_ptr)
class(dense_layer), intent(in), target :: self
real, pointer, intent(out) :: dw_ptr(:)
real, pointer, intent(out) :: db_ptr(:)
end subroutine get_gradients_ptr

module subroutine set_params(self, params)
!! Set the parameters of this layer.
!! The parameters are ordered as weights first, biases second.
Expand Down
18 changes: 18 additions & 0 deletions src/nf/nf_dense_layer_submodule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ module function get_params(self) result(params)
end function get_params


module subroutine get_params_ptr(self, w_ptr, b_ptr)
class(dense_layer), intent(in), target :: self
real, pointer, intent(out) :: w_ptr(:)
real, pointer, intent(out) :: b_ptr(:)
w_ptr(1:size(self % weights)) => self % weights
b_ptr => self % biases
end subroutine get_params_ptr


module function get_gradients(self) result(gradients)
class(dense_layer), intent(in), target :: self
real, allocatable :: gradients(:)
Expand All @@ -93,6 +102,15 @@ module function get_gradients(self) result(gradients)
end function get_gradients


module subroutine get_gradients_ptr(self, dw_ptr, db_ptr)
class(dense_layer), intent(in), target :: self
real, pointer, intent(out) :: dw_ptr(:)
real, pointer, intent(out) :: db_ptr(:)
dw_ptr(1:size(self % dw)) => self % dw
db_ptr => self % db
end subroutine get_gradients_ptr


module subroutine set_params(self, params)
class(dense_layer), intent(in out) :: self
real, intent(in), target :: params(:)
Expand Down
19 changes: 16 additions & 3 deletions src/nf/nf_network_submodule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ module subroutine update(self, optimizer, batch_size)
integer, intent(in), optional :: batch_size
integer :: batch_size_
real, allocatable :: params(:)
real, pointer :: weights(:), biases(:), dw(:), db(:)
integer :: n

! Passing the optimizer instance is optional. If not provided, and if the
Expand Down Expand Up @@ -693,9 +694,21 @@ module subroutine update(self, optimizer, batch_size)
end do
#endif

params = self % get_params()
call self % optimizer % minimize(params, self % get_gradients() / batch_size_)
call self % set_params(params)
do n = 2, size(self % layers)
select type(this_layer => self % layers(n) % p)
type is(dense_layer)
call this_layer % get_params_ptr(weights, biases)
call this_layer % get_gradients_ptr(dw, db)
call self % optimizer % minimize(weights, dw / batch_size_)
call self % optimizer % minimize(biases, db / batch_size_)
type is(locally_connected1d_layer)
!TODO
type is(conv1d_layer)
!TODO
type is(conv2d_layer)
!TODO
end select
end do

! Flush network gradients to zero.
do n = 2, size(self % layers)
Expand Down
2 changes: 1 addition & 1 deletion src/nf/nf_optimizers.f90
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,4 @@ pure subroutine minimize_adagrad(self, param, gradient)

end subroutine minimize_adagrad

end module nf_optimizers
end module nf_optimizers
Loading