Skip to content

get_set removal #227

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 2 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
19 changes: 0 additions & 19 deletions src/nf/nf_conv1d_layer.f90
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ module nf_conv1d_layer
procedure :: backward
procedure :: get_gradients_ptr
procedure :: get_num_params
procedure :: get_params
procedure :: get_params_ptr
procedure :: init
procedure :: set_params

end type conv1d_layer

Expand Down Expand Up @@ -89,15 +87,6 @@ pure module function get_num_params(self) result(num_params)
!! Number of parameters
end function get_num_params

module function get_params(self) result(params)
!! Return the parameters (weights and biases) of this layer.
!! The parameters are ordered as weights first, biases second.
class(conv1d_layer), intent(in), target :: self
!! A `conv1d_layer` instance
real, allocatable :: params(:)
!! Parameters to get
end function get_params

module subroutine get_params_ptr(self, w_ptr, b_ptr)
!! Return pointers to the parameters (weights and biases) of this layer.
class(conv1d_layer), intent(in), target :: self
Expand All @@ -118,14 +107,6 @@ module subroutine get_gradients_ptr(self, dw_ptr, db_ptr)
!! Pointer to the bias gradients
end subroutine get_gradients_ptr

module subroutine set_params(self, params)
!! Set the parameters of the layer.
class(conv1d_layer), intent(in out) :: self
!! A `conv1d_layer` instance
real, intent(in) :: params(:)
!! Parameters to set
end subroutine set_params

end interface

end module nf_conv1d_layer
23 changes: 0 additions & 23 deletions src/nf/nf_conv1d_layer_submodule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,6 @@ pure module function get_num_params(self) result(num_params)
num_params = product(shape(self % kernel)) + size(self % biases)
end function get_num_params

module function get_params(self) result(params)
class(conv1d_layer), intent(in), target :: self
real, allocatable :: params(:)
real, pointer :: w_(:) => null()
w_(1:size(self % kernel)) => self % kernel
params = [ w_, self % biases]
end function get_params

module subroutine get_params_ptr(self, w_ptr, b_ptr)
class(conv1d_layer), intent(in), target :: self
real, pointer, intent(out) :: w_ptr(:)
Expand All @@ -168,19 +160,4 @@ module subroutine get_gradients_ptr(self, dw_ptr, db_ptr)
db_ptr => self % db
end subroutine get_gradients_ptr

module subroutine set_params(self, params)
class(conv1d_layer), intent(in out) :: self
real, intent(in) :: params(:)

if (size(params) /= self % get_num_params()) then
error stop 'conv1d_layer % set_params: Number of parameters does not match'
end if

self % kernel = reshape(params(:product(shape(self % kernel))), shape(self % kernel))
associate(n => product(shape(self % kernel)))
self % biases = params(n + 1 : n + self % filters)
end associate

end subroutine set_params

end submodule nf_conv1d_layer_submodule
19 changes: 0 additions & 19 deletions src/nf/nf_conv2d_layer.f90
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ module nf_conv2d_layer
procedure :: backward
procedure :: get_gradients_ptr
procedure :: get_num_params
procedure :: get_params
procedure :: get_params_ptr
procedure :: init
procedure :: set_params

end type conv2d_layer

Expand Down Expand Up @@ -90,15 +88,6 @@ pure module function get_num_params(self) result(num_params)
!! Number of parameters
end function get_num_params

module function get_params(self) result(params)
!! Return the parameters (weights and biases) of this layer.
!! The parameters are ordered as weights first, biases second.
class(conv2d_layer), intent(in), target :: self
!! A `conv2d_layer` instance
real, allocatable :: params(:)
!! Parameters to get
end function get_params

module subroutine get_params_ptr(self, w_ptr, b_ptr)
!! Return pointers to the parameters (weights and biases) of this layer.
class(conv2d_layer), intent(in), target :: self
Expand All @@ -119,14 +108,6 @@ module subroutine get_gradients_ptr(self, dw_ptr, db_ptr)
!! Pointer to the bias gradients
end subroutine get_gradients_ptr

module subroutine set_params(self, params)
!! Set the parameters of the layer.
class(conv2d_layer), intent(in out) :: self
!! A `conv2d_layer` instance
real, intent(in) :: params(:)
!! Parameters to set
end subroutine set_params

end interface

end module nf_conv2d_layer
39 changes: 0 additions & 39 deletions src/nf/nf_conv2d_layer_submodule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,6 @@ pure module function get_num_params(self) result(num_params)
num_params = product(shape(self % kernel)) + size(self % biases)
end function get_num_params


module function get_params(self) result(params)
class(conv2d_layer), intent(in), target :: self
real, allocatable :: params(:)

real, pointer :: w_(:) => null()

w_(1:size(self % kernel)) => self % kernel

params = [ &
w_, &
self % biases &
]

end function get_params


module subroutine get_params_ptr(self, w_ptr, b_ptr)
class(conv2d_layer), intent(in), target :: self
Expand All @@ -222,27 +206,4 @@ module subroutine get_gradients_ptr(self, dw_ptr, db_ptr)
db_ptr => self % db
end subroutine get_gradients_ptr


module subroutine set_params(self, params)
class(conv2d_layer), intent(in out) :: self
real, intent(in) :: params(:)

! Check that the number of parameters is correct.
if (size(params) /= self % get_num_params()) then
error stop 'conv2d % set_params: Number of parameters does not match'
end if

! Reshape the kernel.
self % kernel = reshape( &
params(:product(shape(self % kernel))), &
shape(self % kernel) &
)

! Reshape the biases.
associate(n => product(shape(self % kernel)))
self % biases = params(n + 1 : n + self % filters)
end associate

end subroutine set_params

end submodule nf_conv2d_layer_submodule
11 changes: 0 additions & 11 deletions src/nf/nf_dense_layer.f90
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ module nf_dense_layer
procedure :: forward
procedure :: get_gradients_ptr
procedure :: get_num_params
procedure :: get_params
procedure :: get_params_ptr
procedure :: init
procedure :: set_params

end type dense_layer

Expand Down Expand Up @@ -88,15 +86,6 @@ pure module function get_num_params(self) result(num_params)
!! Number of parameters in this layer
end function get_num_params

module function get_params(self) result(params)
!! Return the parameters (weights and biases) of this layer.
!! The parameters are ordered as weights first, biases second.
class(dense_layer), intent(in), target :: self
!! Dense layer instance
real, allocatable :: 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(:)
Expand Down
40 changes: 0 additions & 40 deletions src/nf/nf_dense_layer_submodule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,6 @@ pure module function get_num_params(self) result(num_params)
end function get_num_params


module function get_params(self) result(params)
class(dense_layer), intent(in), target :: self
real, allocatable :: params(:)

real, pointer :: w_(:) => null()

w_(1:size(self % weights)) => self % weights

params = [ &
w_, &
self % biases &
]

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(:)
Expand All @@ -94,30 +78,6 @@ module subroutine get_gradients_ptr(self, dw_ptr, db_ptr)
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(:)

real, pointer :: p_(:,:) => null()

! check if the number of parameters is correct
if (size(params) /= self % get_num_params()) then
error stop 'Error: number of parameters does not match'
end if

associate(n => self % input_size * self % output_size)
! reshape the weights
p_(1:self % input_size, 1:self % output_size) => params(1 : n)
self % weights = p_

! reshape the biases
self % biases = params(n + 1 : n + self % output_size)
end associate

end subroutine set_params


module subroutine init(self, input_shape)
class(dense_layer), intent(in out) :: self
integer, intent(in) :: input_shape(:)
Expand Down
11 changes: 11 additions & 0 deletions src/nf/nf_layer.f90
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ 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)
!! Returns the parameters of this layer as pointers.
!! This is used for layers that have weights and biases.
class(layer), intent(in) :: self
!! Layer instance
real, pointer :: w_ptr(:)
!! Pointer to weights of this layer
real, pointer :: b_ptr(:)
!! Pointer to biases of this layer
end subroutine get_params_ptr

module subroutine set_params(self, params)
!! Returns the parameters of this layer.
class(layer), intent(in out) :: self
Expand Down
Loading